Révision b3ab3446
Ajouté par Assos Assos il y a plus de 7 ans
drupal7/sites/all/modules/i18n/i18n_menu/i18n_menu.module | ||
---|---|---|
53 | 53 |
$items['admin/structure/menu/item/%menu_link'] = $items['admin/structure/menu/item/%menu_link/edit']; |
54 | 54 |
$items['admin/structure/menu/item/%menu_link']['type'] = MENU_CALLBACK; |
55 | 55 |
$items['admin/structure/menu/item/%menu_link/edit']['type'] = MENU_DEFAULT_LOCAL_TASK; |
56 |
$items['admin/structure/menu/manage/%menu']['title callback'] = 'i18n_menu_menu_overview_title'; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Preprocess theme_menu_admin_overview to translate menu name and description |
|
61 |
* |
|
62 |
* @param $variables |
|
63 |
*/ |
|
64 |
function i18n_menu_preprocess_menu_admin_overview(&$variables) { |
|
65 |
$variables['title'] = i18n_string(array('menu', 'menu', $variables['name'], 'title'), $variables['title']); |
|
66 |
$variables['description'] = i18n_string(array('menu', 'menu', $variables['name'], 'description'), $variables['description']); |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* Title callback for the menu overview page and links. |
|
71 |
*/ |
|
72 |
function i18n_menu_menu_overview_title($menu) { |
|
73 |
return i18n_string(array('menu', 'menu', $menu['menu_name'], 'title'), $menu['title']); |
|
56 | 74 |
} |
57 | 75 |
|
58 | 76 |
/** |
... | ... | |
726 | 744 |
* Add a "translate" link in operations column for each menu item. |
727 | 745 |
*/ |
728 | 746 |
function i18n_menu_form_menu_overview_form_alter(&$form, &$form_state) { |
729 |
foreach (element_children($form) as $element) { |
|
730 |
if (substr($element, 0, 5) == 'mlid:') { |
|
731 |
$mlid = $form[$element]['#item']['mlid']; |
|
732 |
if (i18n_get_object('menu', $mlid)->get_translate_access()) { |
|
733 |
$form[$element]['operations']['translate'] = array( |
|
734 |
'#type' => 'link', |
|
735 |
'#title' => t('translate'), |
|
736 |
'#href' => "admin/structure/menu/item/{$mlid}/translate", |
|
737 |
); |
|
747 |
if (i18n_menu_mode($form['#menu']['menu_name'], I18N_MODE_MULTIPLE)) { |
|
748 |
foreach (element_children($form) as $element) { |
|
749 |
if (substr($element, 0, 5) == 'mlid:') { |
|
750 |
$item = $form[$element]["#item"]; |
|
751 |
$mlid = $form[$element]['#item']['mlid']; |
|
752 |
if (i18n_get_object('menu', $mlid)->get_translate_access()) { |
|
753 |
$form[$element]['operations']['translate'] = array( |
|
754 |
'#type' => 'link', |
|
755 |
'#title' => t('translate'), |
|
756 |
'#href' => "admin/structure/menu/item/{$mlid}/translate", |
|
757 |
); |
|
758 |
$form[$element]['title']['#markup'] = l(_i18n_menu_link_title($item), $item['href'], $item['localized_options']); |
|
759 |
} |
|
738 | 760 |
} |
739 | 761 |
} |
740 | 762 |
} |
... | ... | |
901 | 923 |
} |
902 | 924 |
|
903 | 925 |
/** |
904 |
* Implements hook_init(). |
|
905 |
*/ |
|
906 |
function i18n_menu_init() { |
|
907 |
|
|
908 |
// The only way to override the default preferred menu link for a path is to |
|
909 |
// inject it into the static cache of the function menu_link_get_preferred(). |
|
910 |
|
|
911 |
// The problem with the default implementation is that it does not take the |
|
912 |
// language of a menu link into account. Whe having different menu trees for |
|
913 |
// different menus, this means that the active trail will not work for all but |
|
914 |
// one language. |
|
915 |
|
|
916 |
// The code below is identical to the mentioned function except the added |
|
917 |
// language condition on the query. |
|
918 |
|
|
919 |
// TODO: Adding an alter tag to the query would allow to do this with a simple |
|
920 |
// hook_query_alter() implementation. |
|
921 |
|
|
922 |
$preferred_links = &drupal_static('menu_link_get_preferred'); |
|
923 |
|
|
924 |
$path = $_GET['q']; |
|
925 |
|
|
926 |
// Look for the correct menu link by building a list of candidate paths, |
|
927 |
// which are ordered by priority (translated hrefs are preferred over |
|
928 |
// untranslated paths). Afterwards, the most relevant path is picked from |
|
929 |
// the menus, ordered by menu preference. |
|
930 |
$item = menu_get_item($path); |
|
931 |
$path_candidates = array(); |
|
932 |
// 1. The current item href. |
|
933 |
$path_candidates[$item['href']] = $item['href']; |
|
934 |
// 2. The tab root href of the current item (if any). |
|
935 |
if ($item['tab_parent'] && ($tab_root = menu_get_item($item['tab_root_href']))) { |
|
936 |
$path_candidates[$tab_root['href']] = $tab_root['href']; |
|
937 |
} |
|
938 |
// 3. The current item path (with wildcards). |
|
939 |
$path_candidates[$item['path']] = $item['path']; |
|
940 |
// 4. The tab root path of the current item (if any). |
|
941 |
if (!empty($tab_root)) { |
|
942 |
$path_candidates[$tab_root['path']] = $tab_root['path']; |
|
943 |
} |
|
944 |
// Retrieve a list of menu names, ordered by preference. |
|
945 |
$menu_names = menu_get_active_menu_names(); |
|
946 |
// Use an illegal menu name as the key for the preferred menu link. |
|
947 |
$selected_menu = MENU_PREFERRED_LINK; |
|
948 |
// Put the selected menu at the front of the list. |
|
949 |
array_unshift($menu_names, $selected_menu); |
|
950 |
|
|
951 |
$query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC)); |
|
952 |
$query->leftJoin('menu_router', 'm', 'm.path = ml.router_path'); |
|
953 |
$query->fields('ml'); |
|
954 |
// Weight must be taken from {menu_links}, not {menu_router}. |
|
955 |
$query->addField('ml', 'weight', 'link_weight'); |
|
956 |
$query->fields('m'); |
|
957 |
$query->condition('ml.link_path', $path_candidates, 'IN'); |
|
958 |
|
|
959 |
// Only look menu links with none or the current language. |
|
960 |
$query->condition('ml.language', array(LANGUAGE_NONE, i18n_language_interface()->language), 'IN'); |
|
961 |
|
|
962 |
// Sort candidates by link path and menu name. |
|
963 |
$candidates = array(); |
|
964 |
foreach ($query->execute() as $candidate) { |
|
965 |
$candidate['weight'] = $candidate['link_weight']; |
|
966 |
$candidates[$candidate['link_path']][$candidate['menu_name']] = $candidate; |
|
967 |
// Add any menus not already in the menu name search list. |
|
968 |
if (!in_array($candidate['menu_name'], $menu_names)) { |
|
969 |
$menu_names[] = $candidate['menu_name']; |
|
970 |
} |
|
971 |
} |
|
972 |
|
|
973 |
// Store the most specific link for each menu. Also save the most specific |
|
974 |
// link of the most preferred menu in $preferred_link. |
|
975 |
foreach ($path_candidates as $link_path) { |
|
976 |
if (isset($candidates[$link_path])) { |
|
977 |
foreach ($menu_names as $menu_name) { |
|
978 |
if (empty($preferred_links[$path][$menu_name]) && isset($candidates[$link_path][$menu_name])) { |
|
979 |
$candidate_item = $candidates[$link_path][$menu_name]; |
|
980 |
$map = explode('/', $path); |
|
981 |
_menu_translate($candidate_item, $map); |
|
982 |
if ($candidate_item['access']) { |
|
983 |
$preferred_links[$path][$menu_name] = $candidate_item; |
|
984 |
if (empty($preferred_links[$path][MENU_PREFERRED_LINK])) { |
|
985 |
// Store the most specific link. |
|
986 |
$preferred_links[$path][MENU_PREFERRED_LINK] = $candidate_item; |
|
987 |
} |
|
988 |
} |
|
989 |
} |
|
926 |
* Implements hook_query_TAG_alter() |
|
927 |
* |
|
928 |
* Using tag 'preferred_menu_links' added in menu_link_get_preferred(). |
|
929 |
* See http://drupal.org/node/1854134 |
|
930 |
*/ |
|
931 |
function i18n_menu_query_preferred_menu_links_alter(QueryAlterableInterface $query) { |
|
932 |
global $language; |
|
933 |
// Get queried tables. |
|
934 |
$tables = $query->getTables(); |
|
935 |
|
|
936 |
foreach ($tables as $alias => $table) { |
|
937 |
if ($table['table'] == 'menu_links') { |
|
938 |
// Add language filter, ensuring that we don't have any collision when |
|
939 |
// determining the active menu trail when there are multiple menu items |
|
940 |
// with same link path but different languages. |
|
941 |
if ($language) { |
|
942 |
$query->condition('language', array($language->language, LANGUAGE_NONE), 'IN'); |
|
990 | 943 |
} |
944 |
break; |
|
991 | 945 |
} |
992 | 946 |
} |
993 | 947 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules