menu_seo_title.module in Panels Extras 7
Same filename and directory in other branches
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.moduleView 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($module, $plugin) {
if ($module == 'ctools' && $plugin == 'content_types') {
return "plugins/content_types";
}
}
/**
* 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 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.
*/
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(
'main-menu' => '',
'user-menu' => '',
));
// Check for regular expressions as menu keys.
$patterns = array();
foreach (array_keys($menu_order) as $pattern) {
if ($pattern[0] == '/') {
$patterns[$pattern] = NULL;
}
}
// Retrieve all the menus containing a link to the current page.
$result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = :link_path", array(
':link_path' => $_GET['q'] ? $_GET['q'] : '<front>',
));
foreach ($result as $item) {
// 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;
}
else {
// Check if the menu matches one of the available patterns.
foreach (array_keys($patterns) as $pattern) {
if (preg_match($pattern, $item->menu_name)) {
// Mark the menu.
$menu_order[$pattern] = MENU_TREE__CURRENT_PAGE_MENU;
// Store the actual menu name.
$patterns[$pattern] = $item->menu_name;
}
}
}
}
// Find the first marked menu.
$config['menu_name'] = array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);
// If a pattern was matched, use the actual menu name instead of the pattern.
if (!empty($patterns[$config['menu_name']])) {
$config['menu_name'] = $patterns[$config['menu_name']];
}
$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']);
}
$orig_tree = $tree;
$tree = _menu_seo_title_update_tree_to_seo($tree);
// Allow alteration of the tree and config before we begin operations on it.
drupal_alter('menu_block_tree', $tree, $config);
// Localize the tree.
if (module_exists('i18n_menu')) {
$tree = i18n_menu_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();
$title = menu_block_get_title($config['title_link'], $config);
$data['subject_array'] = $title;
$data['subject'] = drupal_render($title);
$data['content']['#content'] = menu_block_tree_output($tree, $config);
if (!empty($data['content']['#content'])) {
$data['content']['#theme'] = array(
'menu_block_wrapper__' . str_replace('-', '_', $config['delta']),
'menu_block_wrapper__' . str_replace('-', '_', $config['menu_name']),
'menu_block_wrapper',
);
$data['content']['#config'] = $config;
$data['content']['#delta'] = $config['delta'];
}
else {
$data['content'] = '';
}
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
Name![]() |
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
Name![]() |
Description |
---|---|
MENUSEO_TREE__CURRENT_PAGE_MENU | Denotes that the tree should use the menu picked by the curent page. |