1.20.x system.menu.inc system_menu_block_build(array $config)

Build a menu tree based on the provided configuration.

Parameters

array $config: An array of configuration options that specifies how to build the menu tree and its title.

  • menu_name: (string) The machine name of the requested menu. Can also be set to MENU_TREE__CURRENT_PAGE_MENU to use the menu selected by the page.
  • style: (string) The menu style used to render this menu.
  • level: (int) The starting level of the tree. Some styles may ignore this setting.
  • depth: (int) The maximum depth the tree should contain, relative to the starting level. Some styles may ignore this setting.

Return value

array: An associative array containing several pieces of data.

  • content: The tree as a renderable array.
  • subject: The title rendered as HTML.
  • subject_array: The title as a renderable array.

File

modules/system/system.menu.inc, line 167

Code

function system_menu_block_build(array $config) {
  if (module_exists('menu')) {
    $menu_names = menu_get_menus();
  }
  else {
    $menu_names = menu_list_system_menus();
  }

  // Set defaults.
  $menu_name = str_replace('_', '-', $config['menu_name']);
  $config += system_menu_block_defaults($menu_name);
  if (empty($config['style'])) {
    $config['style'] = 'tree';
  }

  // Always expand dropdown style output.
  if ($config['style'] === 'dropdown') {
    $config['expand_all'] = TRUE;
  }

  // Get the default block name.
  backdrop_static_reset('menu_block_set_title');
  system_menu_block_set_title($menu_names[$menu_name]);

  // Get the raw menu tree data.
  $tree = system_menu_tree_block_data($config);
  $title = system_menu_block_get_title();

  $data = array();
  $data['subject'] = $title;
  $data['content'] = array();
  // Create a renderable tree.
  if (!empty($tree) && $output = menu_tree_output($tree)) {
    $data['content'] = $output;
    // Add any menu style (currently always "dropdown" if any).
    if (!empty($config['style'])) {
      $data['content']['#wrapper_attributes']['class'][] = 'menu-' . str_replace('_', '-', $config['style']);
      $data['content']['#wrapper_attributes']['data-menu-style'] = $config['style'];
      $data['content']['#wrapper_attributes']['data-clickdown'] = $config['clickdown'];
      $data['content']['#attached']['library'][] = array('system', 'backdrop.menus');
      if ($config['style'] === 'dropdown') {
        $data['content']['#attached']['library'][] = array('system', 'smartmenus');
      }
    }
    if (!empty($config['toggle']) && $config['toggle'] == TRUE) {
      $id = backdrop_html_id('menu-toggle-state');
      $data['content']['#wrapper_attributes']['data-menu-toggle-id'] = $id;
      $data['content']['#prefix'] = theme('menu_toggle', array('enabled' => $config['toggle'], 'id' => $id, 'text' => t('Menu')));
      $data['content']['#attached']['library'][] = array('system', 'backdrop.menu-toggle');
    }
    if (!empty($config['style_settings'])) {
      $data['content']['#wrapper_attributes']['data-menu-style'] = backdrop_json_encode($config['style_settings']);
    }
  }

  return $data;
}