View source
<?php
function menu_crumbs_plugins($api) {
$api
->multiPlugin('hierarchy');
$api
->multiPlugin('link_title');
$api
->disabledByDefault('hierarchy.*');
$api
->disabledByDefault('link_title.*');
$api
->routeParentCallback('admin/structure/menu/item/%/edit', 'item_edit', function ($path, array $item) {
if (!empty($item['map'][4]['menu_name'])) {
return 'admin/structure/menu/manage/' . $item['map'][4]['menu_name'];
}
return NULL;
});
$api
->routeTitleCallback('admin/structure/menu/item/%/edit', 'item_edit_title', function ($path, array $item) {
if (!empty($item['map'][4]['mlid'])) {
return t('Edit menu link') . ' ' . $item['map'][4]['mlid'];
}
return NULL;
});
}
class menu_CrumbsMultiPlugin_hierarchy implements crumbs_MultiPlugin_FindParentInterface {
function describe($api) {
foreach (menu_get_menus() as $key => $title) {
$api
->ruleWithLabel($key, $title, t('Menu'));
}
$api
->descWithLabel(t('The parent item\'s path'), t('Parent'));
}
function findParent($path, $item) {
if ('crumbs/special-menu-item/%' === $item['route']) {
return $this
->specialMenuItemFindParent($item);
}
$q = db_select('menu_links', 'child');
$q
->leftJoin('menu_links', 'parent', 'parent.mlid = child.plid');
$q
->addExpression('parent.link_path', 'parent_path');
$q
->addExpression('child.menu_name', 'menu_name');
$q
->addExpression('child.plid', 'plid');
$q
->condition('child.link_path', $path);
if (module_exists('i18n_menu')) {
$language = LANGUAGE_NONE;
if (isset($GLOBALS['language'])) {
$language = array(
$language,
$GLOBALS['language']->language,
);
}
$q
->condition('child.language', $language);
}
$q
->orderBy('child.depth', 'ASC');
$candidates = array();
foreach ($q
->execute() as $row) {
if (!array_key_exists($row->menu_name, $candidates)) {
if ('<separator>' === $row->parent_path) {
continue;
}
if ('<nolink>' === $row->parent_path) {
$candidates[$row->menu_name] = 'crumbs/special-menu-item/' . $row->plid;
}
else {
$candidates[$row->menu_name] = $row->parent_path;
}
}
}
return array_filter($candidates);
}
protected function specialMenuItemFindParent(array $item) {
if (empty($item['map'][2]['menu_name']) || empty($item['map'][2]['plid'])) {
return array();
}
$menu_name = $item['map'][2]['menu_name'];
$parent_link = menu_link_load($item['map'][2]['plid']);
if (empty($parent_link['link_path'])) {
return array();
}
$parent_path = $parent_link['link_path'];
if ('<separator>' === $parent_path) {
return array();
}
if ('<nolink>' === $parent_path) {
$parent_path = 'crumbs/special-menu-item/' . $parent_link['mlid'];
}
return array(
$menu_name => $parent_path,
);
}
}
class menu_CrumbsMultiPlugin_link_title implements crumbs_MultiPlugin_FindTitleInterface {
function describe($api) {
foreach (menu_get_menus() as $key => $title) {
$api
->ruleWithLabel($key, $title, t('Menu'));
}
$api
->descWithLabel(t('Menu link title'), t('Title'));
}
function findTitle($path, $item) {
$q = db_select('menu_router', 'mr');
$q
->condition('path', $item['path']);
$q
->fields('mr', array(
'title',
));
$router_title = $q
->execute()
->fetchField();
$q = db_select('menu_links', 'ml');
$q
->fields('ml');
$q
->condition('link_path', $path);
$q
->condition('router_path', $item['path']);
$q
->orderBy('ml.depth', 'ASC');
if (module_exists('i18n_menu')) {
$language = LANGUAGE_NONE;
if (isset($GLOBALS['language'])) {
$language = array(
$language,
$GLOBALS['language']->language,
);
$q
->addExpression('case ml.language when :language then 1 else 0 end', 'has_language', array(
':language' => LANGUAGE_NONE,
));
$q
->orderBy('has_language');
}
$q
->condition('language', $language);
}
$result = $q
->execute();
$titles = array();
while ($row = $result
->fetchAssoc()) {
if (!isset($titles[$row['menu_name']])) {
$link = $row + $item;
if ($row['link_title'] == $router_title) {
$link['title'] = $item['title'];
}
else {
$link['title'] = $row['link_title'];
}
if (!is_array($link['options'])) {
$link['options'] = unserialize($link['options']);
}
if (1 && module_exists('i18n_menu') && !function_exists('_i18n_menu_link_process')) {
if (1 && isset($link['language']) && $link['language'] === LANGUAGE_NONE) {
unset($link['language']);
}
drupal_alter('translated_menu_link', $link, $item['map']);
$titles[$row['menu_name']] = $link['link_title'];
}
else {
drupal_alter('translated_menu_link', $link, $item['map']);
$titles[$row['menu_name']] = $link['title'];
}
}
}
return $titles;
}
}