You are here

menu_seo_title.module in Panels Extras 6

Same filename and directory in other branches
  1. 7 menu_seo_title/menu_seo_title.module

menu_seo_title.module Add a menu block that uses the menu attribute seo title instead of the normal menu item title

File

menu_seo_title/menu_seo_title.module
View source
<?php

// $Id$ menu_seo_title.module,v 1.1 2011/03/02 11:58:48 chriscalip Exp $

/**
 * @file menu_seo_title.module
 * Add a menu block that uses the menu attribute seo title instead of the normal menu item title
 */

/**
 * Denotes that the tree should use the menu picked by the curent page.
 */
define('MENUSEO_TREE__CURRENT_PAGE_MENU', '_active');

/**
 * Implements hook_ctools_plugin_directory().
 */
function menu_seo_title_ctools_plugin_directory($owner, $plugin_type) {
  $allowed_owners = array(
    'ctools',
    'page_manager',
  );
  if (!in_array($owner, $allowed_owners)) {
    return;
  }
  $allowed_plugin_types = array(
    'content_types',
    'tasks',
  );
  if (!in_array($plugin_type, $allowed_plugin_types)) {
    return;
  }
  return "plugins/{$plugin_type}";
}

/**
 * Build a menu tree based on the provided configuration.
 *
 * @param $config
 *   array An array of configuration options that specifies how to build the
 *   menu tree and its title.
 *   - delta: (string) The menu_block's block delta.
 *   - 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.
 *   - parent_mlid: (int) The mlid of the item that should root the tree. Use 0
 *     to use the menu's root.
 *   - title_link: (boolean) Specifies if the title should be rendered as a link
 *     or a simple string.
 *   - admin_title: (string) An optional title to uniquely identify the block on
 *     the administer blocks page.
 *   - level: (int) The starting level of the tree.
 *   - follow: (string) Specifies if the starting level should follow the
 *     active menu item. Should be set to 0, 'active' or 'child'.
 *   - depth: (int) The maximum depth the tree should contain, relative to the
 *     starting level.
 *   - expanded: (boolean) Specifies if the entire tree be expanded or not.
 *   - sort: (boolean) Specifies if the tree should be sorted with the active
 *     trail at the top of the tree.
 * @return
 *   array An array containing the rendered tree in the 'content' key and the
 *   rendered title in the 'subject' key.
 */
function menu_seo_title_tree_build($config) {

  // Retrieve the active menu item from the database.
  if ($config['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {

    // Retrieve the list of available menus.
    $menu_order = variable_get('menu_block_menu_order', array(
      'primary-links' => '',
      'secondary-links' => '',
    ));

    // Retrieve all the menus containing a link to the current page.
    $result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = '%s'", $_GET['q'] ? $_GET['q'] : '<front>');
    while ($item = db_fetch_array($result)) {

      // Check if the menu is in the list of available menus.
      if (isset($menu_order[$item['menu_name']])) {

        // Mark the menu.
        $menu_order[$item['menu_name']] = MENU_TREE__CURRENT_PAGE_MENU;
      }
    }

    // Find the first marked menu.
    $config['menu_name'] = array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);
    $config['parent_mlid'] = 0;

    // If no menu link was found, don't display the block.
    if (empty($config['menu_name'])) {
      return array();
    }
  }

  // Get the default block name.
  $menu_names = menu_block_get_all_menus();
  menu_block_set_title(t($menu_names[$config['menu_name']]));
  if ($config['expanded'] || $config['parent_mlid']) {

    // Get the full, un-pruned tree.
    $tree = menu_tree_all_data($config['menu_name']);

    // And add the active trail data back to the full tree.
    menu_tree_add_active_path($tree);
  }
  else {

    // Get the tree pruned for just the active trail.
    $tree = menu_tree_page_data($config['menu_name']);
  }

  // do magic here, replace title of link with seo title and unset seo title
  $orig_tree = $tree;
  $tree = _menu_seo_title_update_tree_to_seo($tree);

  //dpm($orig_tree);

  //dpm($tree);

  // Allow other modules to alter the tree before we begin operations on it.
  $alter_data =& $tree;

  // Also allow modules to alter the config.
  $alter_data['__drupal_alter_by_ref'] = array(
    &$config,
  );
  drupal_alter('menu_block_tree', $alter_data);

  // Localize the tree.
  if (module_exists('i18nmenu')) {
    i18nmenu_localize_tree($tree);
  }

  // Prune the tree along the active trail to the specified level.
  if ($config['level'] > 1 || $config['parent_mlid']) {
    if ($config['parent_mlid']) {
      $parent_item = menu_link_load($config['parent_mlid']);
      menu_tree_prune_tree($tree, $config['level'], $parent_item);
    }
    else {
      menu_tree_prune_tree($tree, $config['level']);
    }
  }

  // Prune the tree to the active menu item.
  if ($config['follow']) {
    menu_tree_prune_active_tree($tree, $config['follow']);
  }

  // If the menu-item-based tree is not "expanded", trim the tree to the active path.
  if ($config['parent_mlid'] && !$config['expanded']) {
    menu_tree_trim_active_path($tree);
  }

  // Trim the branches that extend beyond the specified depth.
  if ($config['depth'] > 0) {
    menu_tree_depth_trim($tree, $config['depth']);
  }

  // Sort the active path to the top of the tree.
  if ($config['sort']) {
    menu_tree_sort_active_path($tree);
  }

  // Render the tree.
  $data = array();
  $data['subject'] = menu_block_get_title($config['title_link'], $config);
  $data['content'] = menu_block_tree_output($tree, $config);
  if ($data['content']) {
    $hooks = array();
    $hooks[] = 'menu_block_wrapper__' . $config['delta'];
    $hooks[] = 'menu_block_wrapper__' . str_replace('-', '_', $config['menu_name']);
    $hooks[] = 'menu_block_wrapper';
    $data['content'] = theme($hooks, $data['content'], $config, $config['delta']);
  }
  return $data;
}

/**
 * Implements hook_menu_attribute_info().
 */
function menu_seo_title_menu_attribute_info() {
  $info['seo_title'] = array(
    'label' => t('Seo Title'),
    'description' => t("use this seo title instead of the normal menu item title in the seo menu block"),
  );
  return $info;
}

/**
 * Utility function to replace menu link title with the seo title set in menu attribute
 *  ** menu_attribute added seo title will be unset after the seo title got replaced
 * 
 * @param array $tree
 * @return array
 */
function _menu_seo_title_update_tree_to_seo(&$tree = array()) {
  foreach ($tree as &$value) {
    if (isset($value['link']['options']['attributes']['seo_title'])) {
      $value['link']['title'] = $value['link']['options']['attributes']['seo_title'];
      unset($value['link']['options']['attributes']['seo_title']);
    }
    if (!empty($value['below'])) {
      _menu_seo_title_update_tree_to_seo($value['below']);
    }
  }
  return $tree;
}

Functions

Namesort descending Description
menu_seo_title_ctools_plugin_directory Implements hook_ctools_plugin_directory().
menu_seo_title_menu_attribute_info Implements hook_menu_attribute_info().
menu_seo_title_tree_build Build a menu tree based on the provided configuration.
_menu_seo_title_update_tree_to_seo Utility function to replace menu link title with the seo title set in menu attribute ** menu_attribute added seo title will be unset after the seo title got replaced

Constants

Namesort descending Description
MENUSEO_TREE__CURRENT_PAGE_MENU Denotes that the tree should use the menu picked by the curent page.