You are here

advanced_menu_blocks.module in Advanced Menu 7

Provides block customization settings for menus.

File

blocks/advanced_menu_blocks.module
View source
<?php

// $Id: advanced_menu_blocks.module $

/**
 * @file
 * Provides block customization settings for menus.
 */

/**
 * Implementation of hook_permission().
 */
function advanced_menu_blocks_permission() {
  return array(
    'administer advanced menu blocks' => array(
      'title' => t('Administer Advanced Menu Blocks'),
      'description' => t('Set Block Visibility for Menus.'),
    ),
  );
}

/**
 * Implements hook_block_info_alter().
 */
function advanced_menu_blocks_block_info_alter(&$blocks, $theme, $code_blocks) {

  //System menus are checked first.
  $system_menus = array(
    'navigation',
    'management',
    'user-menu',
    'main-menu',
  );
  foreach ($system_menus as $sys_menu) {
    if (_advanced_menu_blocks_disabled($sys_menu) == array(
      1,
    )) {
      unset($blocks['system'][$sys_menu]);
      db_delete('block')
        ->condition('module', 'system')
        ->condition('delta', $sys_menu)
        ->condition('theme', $theme)
        ->execute();
    }
  }

  //Loop through the custom menus
  foreach ($blocks['menu'] as $menu => $value) {
    if (_advanced_menu_blocks_disabled($menu) == array(
      1,
    )) {
      unset($blocks['menu'][$menu]);
      db_delete('block')
        ->condition('module', 'menu')
        ->condition('delta', $menu)
        ->condition('theme', $theme)
        ->execute();
    }
  }
  if (empty($blocks['menu'])) {
    unset($blocks['menu']);
  }
}

/**
 * Implements hook_block_view_alter().
 */
function advanced_menu_blocks_block_view_alter(&$data, $block) {
  if ($block->module == 'menu' && _advanced_menu_blocks_disabled($block->delta) == array(
    1,
  )) {
    unset($data['content']);
  }
}

/**
 * Implementation of hook_form_alter().
 */
function advanced_menu_blocks_form_alter(&$form, $form_state, $form_id) {
  $menu = $form;
  switch ($form_id) {
    case 'menu_edit_menu':
      if (user_access('administer advanced menu blocks')) {
        $form['submit']['#weight'] = 2;
        $form['blocks'] = array(
          '#type' => 'checkboxes',
          '#title' => t('Block Visibility'),
          '#default_value' => _advanced_menu_blocks_disabled($form['menu_name']['#default_value']),
          '#options' => array(
            '1' => 'Disable the block for this menu.',
          ),
          '#description' => t('Menus which have their block disabled will not be shown on the Blocks Administration page and will not be displayed.'),
          '#weight' => 1,
        );
        $form['#submit'][] = 'advanced_menu_blocks_form_submit';
      }
      break;
  }
}

/**
 * Save block visibility settings.
 */
function advanced_menu_blocks_form_submit(&$form, $form_state) {
  $menu = $form_state['values'];
  $menu_name = '';
  switch ($menu['form_id']) {
    case 'menu_edit_menu':
      if ($form['#insert']) {
        $menu['menu_name'] = 'menu-' . $menu['menu_name'];
      }
      $menu_name = $menu['menu_name'];
      db_delete('advanced_menu_blocks')
        ->condition('menu_name', $menu_name)
        ->execute();
      foreach ($menu['blocks'] as $block) {
        if ($block != 0) {
          db_insert('advanced_menu_blocks')
            ->fields(array(
            'menu_name' => $menu_name,
          ))
            ->execute();
          db_delete('block')
            ->condition('delta', $menu_name)
            ->execute();
        }
      }
      break;
  }
  menu_rebuild();
}

/**
 * Helper function to determine if the block is disabled.
 */
function _advanced_menu_blocks_disabled($menu_name) {
  $query = db_select('advanced_menu_blocks', 'b')
    ->fields('b', array(
    'menu_name',
  ))
    ->condition('menu_name', $menu_name);
  $num_rows = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  return array(
    $num_rows,
  );
}

Functions