You are here

function _jump_menu_render_block in Better Jump Menus 8

Same name and namespace in other branches
  1. 7 jump_menu.module \_jump_menu_render_block()

Abstract block rendering to be more flexible about when/how this happens.

File

./jump_menu.module, line 129
Make use of the CTools jump menu and grabs from an existing menu. See: modules/ctools/includes/jump-menu.inc NOTE: Menu items must be checked as "expanded" for traversing to work.

Code

function _jump_menu_render_block($delta, $options = array()) {

  // Strip off jump_menu.
  $block_name = str_replace('jump_menu-', '', $delta);

  // Cache menu list.
  static $menus;
  if (!isset($menus)) {
    $menus = menu_get_menus(TRUE);
  }

  // Options are always available.
  $options['hide'] = isset($options['hide']) ? $options['hide'] : TRUE;

  // Allow setting active item.
  $settings = variable_get('jump_menu_block_settings_show_current', array());
  $current = isset($settings[$delta]) ? $settings[$delta] : JUMP_MENU_DEFAULT_BLOCK_SETTINGS_SHOW_CURRENT;

  // If a menu block.
  if (substr($block_name, 0, 2) == 'm_') {
    foreach ($menus as $k => $v) {

      // Block delta prefix length is 12 chars, leaves 20 for menu name.
      // Compare as much fits against the menu portion of the delta.
      if (substr($k, 0, 20) == substr($block_name, 2)) {

        // Set the title.
        $data['subject'] = check_plain($menus[$k]);

        // Set default 'choose' text to menu name.
        $options['choose'] = isset($options['choose']) ? $options['choose'] : check_plain($menus[$k]);
        $data['content'] = jump_menu($k, 0, FALSE, 0, $options['choose'], $current);
      }
    }
  }
  elseif (substr($block_name, 0, 11) == 'local-tasks') {

    // Collect the local tasks.
    $links = menu_local_tasks(0);
    $links_secondary = menu_local_tasks(1);

    // Are there any real secondary tasks?
    $secondary = count($links_secondary['tabs']['output']) != 0 ? TRUE : FALSE;
    if ($links['tabs']['count'] > 0) {
      $targets = array();

      // Create select list targets.
      foreach ($links['tabs']['output'] as $l) {
        if ($l['#link']['access'] == TRUE) {

          // Set active.
          $classes = $l['#active'] ? 'active' : '';
          $targets[] = array(
            'value' => url($l['#link']['href']),
            'title' => t($l['#link']['title']),
            '#attributes' => array(
              'class' => $classes,
            ),
          );

          // Do secondary tabs fit with this item?
          if ($secondary && $links_secondary['tabs']['output'][0]['#link']['tab_parent_href'] == $l['#link']['href']) {
            foreach ($links_secondary['tabs']['output'] as $sl) {

              // Set active.
              $classes = $l['#active'] ? 'active' : '';
              $targets[] = array(
                'value' => url($sl['#link']['href']),
                'title' => '- ' . t($sl['#link']['title']),
                '#attributes' => array(
                  'class' => $classes,
                ),
              );
            }
          }
        }
      }

      // Take options and place defaults.
      $options['choose'] = isset($options['choose']) ? $options['choose'] : t(JUMP_MENU_DEFAULT_CHOOSE);

      // Process setting active item.
      if ($current) {
        $current_path = base_path() . request_path();
        if (!empty($current_path)) {
          $options['default_value'] = $current_path;
        }
      }

      // Populate block.
      $data['subject'] = t('Local Tasks');
      $data['content'] = drupal_render(drupal_get_form('ctools_jump_menu', $targets, $options));
    }
    else {
      $data = FALSE;
    }
  }
  else {
    $notice = t('Something is wrong with the Jump Menu module, please report.');
    if (variable_get('error_level') > 0) {
      drupal_set_message($notice);
    }
    else {
      $message = 'Unable to render Jump Menu block. Likely due to a bad menu delta in the database.';
      watchdog('jump_menu', $message, array(), WATCHDOG_ERROR);
      drupal_set_message($notice);
    }
    $data = FALSE;
  }
  return $data;
}