You are here

menu_block_export.admin.inc in Menu Block 7.2

Same filename and directory in other branches
  1. 7.3 menu_block_export.admin.inc

Provides infrequently used functions and hooks for menu_block_export.

File

menu_block_export.admin.inc
View source
<?php

/**
 * @file
 * Provides infrequently used functions and hooks for menu_block_export.
 */

/**
 * Page callback to export menu blocks in bulk.
 */
function menu_block_export_export() {
  $blocks = variable_get('menu_block_ids', array());
  if (!empty($blocks)) {
    $form_state = array(
      'no_redirect' => TRUE,
    );
    $output = drupal_build_form('menu_block_export_form', $form_state);
    if (!empty($form_state['output'])) {
      $output = $form_state['output'];
    }
    return $output;
  }
  else {
    return t('There are no menu blocks to be exported at this time.');
  }
}

/**
 * Menu callback; export form.
 *
 * @return
 *   The export form used by Menu block.
 */
function menu_block_export_form($form, &$form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Module name'),
    '#default_value' => 'custom',
    '#description' => t('Enter the module name to export code to.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Export'),
  );
  $form['#action'] = url('admin/config/user-interface/menu-block/export/results');
  return $form;
}

/**
 * Submit callback for menu_block_export_form().
 */
function menu_block_export_form_submit(&$form, &$form_state) {
  $output = '';
  $module = check_plain($form_state['values']['name']);
  foreach (variable_get('menu_block_ids', array()) as $delta) {
    $config = menu_block_get_config($delta);

    // Use the constant instead of the constant's value.
    if ($config['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {
      $config['menu_name'] = 'MENU_TREE__CURRENT_PAGE_MENU';
    }
    else {

      // If it's not the constant, wrap value in quotes.
      $config['menu_name'] = "'" . $config['menu_name'] . "'";
    }
    $id = "{$module}-{$delta}";
    if (strlen($id) > 32) {
      $id = md5($id);
    }
    $output .= <<<END_OF_CONFIG

    '{<span class="php-variable">$id</span>}' => array(
      'parent'      => {<span class="php-variable">$config</span>[<span class="php-string">'parent'</span>]},
      'title_link'  => {<span class="php-variable">$config</span>[<span class="php-string">'title_link'</span>]},
      'admin_title' => '{<span class="php-variable">$config</span>[<span class="php-string">'admin_title'</span>]}',
      'level'       => {<span class="php-variable">$config</span>[<span class="php-string">'level'</span>]},
      'follow'      => '{<span class="php-variable">$config</span>[<span class="php-string">'follow'</span>]}',
      'depth'       => {<span class="php-variable">$config</span>[<span class="php-string">'depth'</span>]},
      'expanded'    => {<span class="php-variable">$config</span>[<span class="php-string">'expanded'</span>]},
      'sort'        => {<span class="php-variable">$config</span>[<span class="php-string">'sort'</span>]},
    ),
END_OF_CONFIG;
  }
  $output = <<<END_OF_CONFIG
/**
 * Implements hook_menu_block_blocks().
 */
function {<span class="php-variable">$module</span>}_menu_block_blocks() {
  // The array key is the block delta used by menu block.
  return array({<span class="php-variable">$output</span>}
  );
}

END_OF_CONFIG;
  $element = array(
    '#type' => 'textarea',
    '#title' => t('Use this in your !module.module file:', array(
      '!module' => $module,
    )),
    '#value' => $output,
    '#rows' => 20,
    // Since this isn't a real form, manually add additional required properties.
    '#id' => 'menu-block-export-textarea',
    '#name' => 'export',
    '#required' => FALSE,
    '#attributes' => array(
      'style' => 'font-family: monospace;',
    ),
    '#title_display' => 'before',
    '#parents' => array(
      'dummy',
    ),
  );
  $form_state['output'] = drupal_render($element);
}

Functions

Namesort descending Description
menu_block_export_export Page callback to export menu blocks in bulk.
menu_block_export_form Menu callback; export form.
menu_block_export_form_submit Submit callback for menu_block_export_form().