Тут важливо налаштувати ваш файл router.php компонентів (який слід знайти у кореневій папці вашого компонента на передньому кінці) з логікою, яка шукатиме та вибирає відповідний пункт меню. Я хотів би, щоб це відбувалося автоматично, але наскільки я знаю, це не так.
Напевно, найкраще буде опрацювати цей блок коду в якомусь типі допоміжної функції, який можна використовувати для автоматичного пошуку найкращого пункту меню для вмісту.
Ось код, який я використав у кількох моїх користувацьких компонентах, щоб отримати найкращий варіант меню:
// I use this first empty array to avoid having unset properties in my query
$base_array = array('Itemid'=>'', 'option'=>'', 'view'=>'', 'layout'=>'', 'id'=>'');
$app =& JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
// hack to protect the actual current item as well as the search module or other places that use JRoute::_('index.php');
if (count($query)==2 && isset($query['option']) && isset($query['Itemid']) && $query['option'] && $query['Itemid']) {
return $segments;
}
// start with no match found
$match = false;
$match_level = 0;
$query += $base_array;
// we want to find a menu item for this if possible. If the active menu item is the current menu item then we should see if there is a better match.
if (empty($query['Itemid']) || ($query['Itemid'] == $active->id && empty($query['task']))) {
// load all menu items
$items = $menu->getMenu();
// use the current item over others if it ties for the best match
if ($active->query['option'] == $query['option']) {
$match_level = 1;
$match = $active;
if ($active->query['view'] == $query['view']) {
$match_level = 2;
if ($active->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$active->query['layout'])) {
$match_level = 3;
if ($active->query['id'] == $query['id']) {
$match_level = 4;
}
}
}
}
// loop through each menu item in order
foreach ($items as $item) {
$item->query += $base_array;
// base check is that it is for this component
// then cycle through each possibility finding it's match level
if ($item->query['option'] == $query['option']) {
$item_match = 1;
if ($item->query['view'] == $query['view']) {
$item_match = 2;
if (!$query['layout'] && $item->query['layout']) {
$query['layout'] = 'default';
}
if ($item->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$item->query['layout'])) {
$item_match = 3;
if ($item->query['id'] == $query['id']) {
$item_match = 4;
}
}
}
}
// if this item is a better match than our current match, set it as the best match
if ($item_match > $match_level) {
$match = $item;
$match_level = $item_match;
}
}
// if there is a match update Itemid to match that menu item
if ($match) {
$query['Itemid'] = $match->id;
$menuItem = $menu->getItem($match->id);
} else {
$menuItem = $menu->getActive();
}
}
Все це є свого роду безлад (і я б хотів покращити, якщо хтось їх має!), Але це робить роботу. Якщо поточний пункт меню найкраще відповідає, він завжди буде дотримуватися цього.
В іншому випадку він повинен знайти найкращу відповідність на основі назви компонента -> ім'я перегляду -> назва макета -> значення id. Чим далі справа відповідає, тим краще я вважаю відповідність!