You are here

menu_block.admin.inc in Menu Block 5.2

Provides infrequently used functions for menu_block.

File

menu_block.admin.inc
View source
<?php

/**
 * @file
 * Provides infrequently used functions for menu_block.
 */

/**
 * Menu callback: display the menu block addition form.
 */
function _menu_block_add_block_form() {
  include_once './' . drupal_get_path('module', 'block') . '/block.module';
  return block_admin_configure('menu_block', NULL);
}

/**
 * Save the new menu block.
 */
function menu_block_add_block_form_submit($form_id, $form_values) {

  // Determine the delta of the new block.
  $block_ids = variable_get('menu_block_ids', array());
  $delta = empty($block_ids) ? 1 : max($block_ids) + 1;

  // Save the new array of blocks IDs.
  $block_ids[] = $delta;
  variable_set('menu_block_ids', $block_ids);

  // Save the block configuration.
  _menu_block_block_save($delta, $form_values);

  // Run the normal new block submission (borrowed from block_add_block_form_submit).
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d)", $form_values['visibility'], trim($form_values['pages']), $form_values['custom'], $form_values['title'], $form_values['module'], $theme->name, 0, 0, $delta);
    }
  }
  foreach (array_filter($form_values['roles']) as $rid) {
    db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $delta);
  }
  drupal_set_message(t('The block has been created.'));
  cache_clear_all();
  return 'admin/build/block';
}

/**
 * Alters the block admin form to add delete links next to menu blocks.
 */
function _menu_block_form_block_admin_display_alter(&$form) {
  for ($i = 0; !empty($form[$i]); $i++) {
    if ($form[$i]['module']['#value'] == 'menu_block') {
      $form[$i]['delete'] = array(
        '#value' => l(t('delete'), 'admin/build/block/delete-menu-block/' . $form[$i]['delta']['#value']),
      );
    }
  }
}

/**
 * Alters the add new menu block page to set the title properly.
 */
function _menu_block_form_menu_block_add_block_form_alter(&$form) {
  drupal_set_title(t('New menu block'));
}

/**
 * Menu callback: confirm deletion of menu blocks.
 */
function _menu_block_delete($delta = 0) {
  $title = _menu_block_format_title($delta);
  $form['block_title'] = array(
    '#type' => 'hidden',
    '#value' => $title,
  );
  $form['delta'] = array(
    '#type' => 'hidden',
    '#value' => $delta,
  );
  return confirm_form($form, t('Are you sure you want to delete the "%name" block?', array(
    '%name' => $title,
  )), 'admin/build/block', NULL, t('Delete'), t('Cancel'));
}

/**
 * Deletion of menu blocks.
 */
function menu_block_delete_submit($form_id, $form_values) {

  // Remove the menu block configuration variables.
  $delta = $form_values['delta'];
  $block_ids = variable_get('menu_block_ids', array());
  unset($block_ids[array_search($delta, $block_ids)]);
  sort($block_ids);
  variable_set('menu_block_ids', $block_ids);
  variable_del("menu_block_{$delta}_menu_name");
  variable_del("menu_block_{$delta}_title");
  variable_del("menu_block_{$delta}_level");
  variable_del("menu_block_{$delta}_depth");
  variable_del("menu_block_{$delta}_expanded");
  db_query("DELETE FROM {blocks} WHERE module = 'menu_block' AND delta = %d", $delta);
  db_query("DELETE FROM {blocks_roles} WHERE module = 'menu_block' AND delta = %d", $delta);
  drupal_set_message(t('The "%name" block has been removed.', array(
    '%name' => $form_values['block_title'],
  )));
  cache_clear_all();
  return 'admin/build/block';
}

/**
 * Returns the 'list' $op info for hook_block().
 */
function _menu_block_block_list() {
  $blocks = array();
  foreach (variable_get('menu_block_ids', array()) as $delta) {
    $blocks[$delta]['info'] = _menu_block_format_title($delta);
  }
  return $blocks;
}

/**
 * Return the title of the block.
 *
 * @param $delta
 *   int The delta of the menu block
 * @return
 *   string The title of the block
 */
function _menu_block_format_title($delta) {
  $menu_name = variable_get("menu_block_{$delta}_menu_name", NULL);
  if (is_null($menu_name)) {
    $title = t('Unconfigured menu block');
  }
  else {
    $level = variable_get("menu_block_{$delta}_level", 1);
    $depth = variable_get("menu_block_{$delta}_depth", 0);
    $expanded = variable_get("menu_block_{$delta}_expanded", 0);
    $menus = menu_get_root_menus();

    // Show the configured levels in the block info
    $replacements = array(
      '@menu_name' => $menus[$menu_name],
      '@level1' => $level,
      '@level2' => $level + $depth - 1,
    );
    if ($depth == 1) {
      $title = t('@menu_name (level @level1)', $replacements);
    }
    elseif ($depth) {
      if ($expanded) {
        $title = t('@menu_name (expanded levels @level1-@level2)', $replacements);
      }
      else {
        $title = t('@menu_name (levels @level1-@level2)', $replacements);
      }
    }
    else {
      if ($expanded) {
        $title = t('@menu_name (expanded levels @level1+)', $replacements);
      }
      else {
        $title = t('@menu_name (levels @level1+)', $replacements);
      }
    }
  }
  return $title;
}

/**
 * Returns the 'configure' $op info for hook_block().
 */
function _menu_block_block_configure($delta) {
  $form['help'] = array(
    '#value' => '<p>' . t('To learn more about configuring menu blocks, see <a href="@url">menu block’s detailed help</a>.', array(
      '@url' => url('admin/help/menu_block'),
    )) . '</p>',
    '#weight' => -18,
  );
  $form['menu_name'] = array(
    '#type' => 'select',
    '#title' => t('Menu'),
    '#default_value' => variable_get("menu_block_{$delta}_menu_name", 1),
    '#options' => menu_get_root_menus(),
    '#description' => t('The tree (or list) of links will be taken from this menu.'),
  );
  $form['level'] = array(
    '#type' => 'select',
    '#title' => t('Starting level of menu tree'),
    '#default_value' => variable_get("menu_block_{$delta}_level", 1),
    '#options' => array(
      '1' => t('1st level (primary)'),
      '2' => t('2nd level (secondary)'),
      '3' => t('3rd level (tertiary)'),
      '4' => t('4th level'),
      '5' => t('5th level'),
      '6' => t('6th level'),
      '7' => t('7th level'),
      '8' => t('8th level'),
      '9' => t('9th level'),
      '10' => t('10th level'),
    ),
    '#description' => t('Blocks that start with the 1st level will always be visible. Blocks that start with the 2nd level or deeper will only be visible when the trail to the active menu item is in the block’s tree.'),
  );
  $form['depth'] = array(
    '#type' => 'select',
    '#title' => t('Maximum depth of menu tree'),
    '#default_value' => variable_get("menu_block_{$delta}_depth", 0),
    '#options' => array(
      '1' => '1',
      '2' => '2',
      '3' => '3',
      '4' => '4',
      '5' => '5',
      '6' => '6',
      '7' => '7',
      '8' => '8',
      '9' => '9',
      '10' => '10',
      '0' => 'Unlimited',
    ),
    '#description' => t('From the starting level, specify the maximum depth of the tree.'),
  );
  $form['expanded'] = array(
    '#type' => 'checkbox',
    '#title' => t('Expand all children'),
    '#default_value' => variable_get("menu_block_{$delta}_expanded", 0),
    '#description' => t('All sub-menus of this menu will be expanded.'),
  );
  return $form;
}

/**
 * Returns the 'save' $op info for hook_block().
 */
function _menu_block_block_save($delta, $edit) {
  $menus = menu_get_root_menus();
  variable_set("menu_block_{$delta}_menu_name", $edit['menu_name']);
  variable_set("menu_block_{$delta}_title", $menus[$edit['menu_name']]);
  variable_set("menu_block_{$delta}_level", $edit['level']);
  variable_set("menu_block_{$delta}_depth", $edit['depth']);
  variable_set("menu_block_{$delta}_expanded", $edit['expanded']);
}

Functions

Namesort descending Description
menu_block_add_block_form_submit Save the new menu block.
menu_block_delete_submit Deletion of menu blocks.
_menu_block_add_block_form Menu callback: display the menu block addition form.
_menu_block_block_configure Returns the 'configure' $op info for hook_block().
_menu_block_block_list Returns the 'list' $op info for hook_block().
_menu_block_block_save Returns the 'save' $op info for hook_block().
_menu_block_delete Menu callback: confirm deletion of menu blocks.
_menu_block_format_title Return the title of the block.
_menu_block_form_block_admin_display_alter Alters the block admin form to add delete links next to menu blocks.
_menu_block_form_menu_block_add_block_form_alter Alters the add new menu block page to set the title properly.