2015-08-18 00:00:26 +00:00
< ? php
/**
* @ file
* API for the Drupal menu system .
*/
/**
* @ addtogroup menu
* @ {
*/
2018-11-23 12:29:20 +00:00
use Drupal\Component\Render\FormattableMarkup ;
2015-08-18 00:00:26 +00:00
use Drupal\Core\Render\Element ;
/**
* Prepares variables for single local task link templates .
*
* Default template : menu - local - task . html . twig .
*
* @ param array $variables
* An associative array containing :
* - element : A render element containing :
* - #link: A menu link array with 'title', 'url', and (optionally)
* 'localized_options' keys .
* - #active: A boolean indicating whether the local task is active.
*/
function template_preprocess_menu_local_task ( & $variables ) {
$link = $variables [ 'element' ][ '#link' ];
2017-04-13 14:53:35 +00:00
$link += [
'localized_options' => [],
];
2015-08-18 00:00:26 +00:00
$link_text = $link [ 'title' ];
if ( ! empty ( $variables [ 'element' ][ '#active' ])) {
$variables [ 'is_active' ] = TRUE ;
// Add text to indicate active tab for non-visual users.
2018-11-23 12:29:20 +00:00
$active = new FormattableMarkup ( '<span class="visually-hidden">@label</span>' , [ '@label' => t ( '(active tab)' )]);
2017-04-13 14:53:35 +00:00
$link_text = t ( '@local-task-title@active' , [ '@local-task-title' => $link_text , '@active' => $active ]);
2015-08-18 00:00:26 +00:00
}
$link [ 'localized_options' ][ 'set_active_class' ] = TRUE ;
2017-04-13 14:53:35 +00:00
$variables [ 'link' ] = [
2015-08-18 00:00:26 +00:00
'#type' => 'link' ,
'#title' => $link_text ,
'#url' => $link [ 'url' ],
'#options' => $link [ 'localized_options' ],
2017-04-13 14:53:35 +00:00
];
2015-08-18 00:00:26 +00:00
}
/**
* Prepares variables for single local action link templates .
*
* Default template : menu - local - action . html . twig .
*
* @ param array $variables
* An associative array containing :
* - element : A render element containing :
* - #link: A menu link array with 'title', 'url', and (optionally)
* 'localized_options' keys .
*/
function template_preprocess_menu_local_action ( & $variables ) {
$link = $variables [ 'element' ][ '#link' ];
2017-04-13 14:53:35 +00:00
$link += [
'localized_options' => [],
];
2015-08-18 00:00:26 +00:00
$link [ 'localized_options' ][ 'attributes' ][ 'class' ][] = 'button' ;
$link [ 'localized_options' ][ 'attributes' ][ 'class' ][] = 'button-action' ;
$link [ 'localized_options' ][ 'set_active_class' ] = TRUE ;
2017-04-13 14:53:35 +00:00
$variables [ 'link' ] = [
2015-08-18 00:00:26 +00:00
'#type' => 'link' ,
'#title' => $link [ 'title' ],
'#options' => $link [ 'localized_options' ],
'#url' => $link [ 'url' ],
2017-04-13 14:53:35 +00:00
];
2015-08-18 00:00:26 +00:00
}
/**
* Returns an array containing the names of system - defined ( default ) menus .
*/
function menu_list_system_menus () {
2017-04-13 14:53:35 +00:00
return [
2015-08-18 00:00:26 +00:00
'tools' => 'Tools' ,
'admin' => 'Administration' ,
'account' => 'User account menu' ,
'main' => 'Main navigation' ,
'footer' => 'Footer menu' ,
2017-04-13 14:53:35 +00:00
];
2015-08-18 00:00:26 +00:00
}
/**
2015-09-04 20:20:09 +00:00
* Collects the local tasks ( tabs ) for the current route .
2015-08-18 00:00:26 +00:00
*
* @ param int $level
* The level of tasks you ask for . Primary tasks are 0 , secondary are 1.
*
* @ return array
* An array containing
* - tabs : Local tasks for the requested level .
2015-09-04 20:20:09 +00:00
* - route_name : The route name for the current page used to collect the local
* tasks .
2015-08-18 00:00:26 +00:00
*
* @ see hook_menu_local_tasks_alter ()
2018-11-23 12:29:20 +00:00
* @ see https :// www . drupal . org / node / 2544940
2015-09-04 20:20:09 +00:00
*
* @ deprecated in Drupal 8.0 . 0 , will be removed before Drupal 9.0 . 0.
2015-08-18 00:00:26 +00:00
*/
function menu_local_tasks ( $level = 0 ) {
2015-09-04 20:20:09 +00:00
/** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
$manager = \Drupal :: service ( 'plugin.manager.menu.local_task' );
return $manager -> getLocalTasks ( \Drupal :: routeMatch () -> getRouteName (), $level );
2015-08-18 00:00:26 +00:00
}
/**
* Returns the rendered local tasks at the top level .
2015-09-04 20:20:09 +00:00
*
2018-11-23 12:29:20 +00:00
* @ see https :// www . drupal . org / node / 2874695
*
2015-09-04 20:20:09 +00:00
* @ deprecated in Drupal 8.0 . 0 , will be removed before Drupal 9.0 . 0.
2015-08-18 00:00:26 +00:00
*/
function menu_primary_local_tasks () {
2015-09-04 20:20:09 +00:00
/** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
$manager = \Drupal :: service ( 'plugin.manager.menu.local_task' );
$links = $manager -> getLocalTasks ( \Drupal :: routeMatch () -> getRouteName (), 0 );
2015-08-18 00:00:26 +00:00
// Do not display single tabs.
return count ( Element :: getVisibleChildren ( $links [ 'tabs' ])) > 1 ? $links [ 'tabs' ] : '' ;
}
/**
* Returns the rendered local tasks at the second level .
2015-09-04 20:20:09 +00:00
*
2018-11-23 12:29:20 +00:00
* @ see https :// www . drupal . org / node / 2874695
*
2015-09-04 20:20:09 +00:00
* @ deprecated in Drupal 8.0 . 0 , will be removed before Drupal 9.0 . 0.
2015-08-18 00:00:26 +00:00
*/
function menu_secondary_local_tasks () {
2015-09-04 20:20:09 +00:00
/** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
$manager = \Drupal :: service ( 'plugin.manager.menu.local_task' );
$links = $manager -> getLocalTasks ( \Drupal :: routeMatch () -> getRouteName (), 1 );
2015-08-18 00:00:26 +00:00
// Do not display single tabs.
return count ( Element :: getVisibleChildren ( $links [ 'tabs' ])) > 1 ? $links [ 'tabs' ] : '' ;
}
/**
* Returns a renderable element for the primary and secondary tabs .
*/
function menu_local_tabs () {
2017-04-13 14:53:35 +00:00
$build = [
2015-08-18 00:00:26 +00:00
'#theme' => 'menu_local_tasks' ,
'#primary' => menu_primary_local_tasks (),
'#secondary' => menu_secondary_local_tasks (),
2017-04-13 14:53:35 +00:00
];
return ! empty ( $build [ '#primary' ]) || ! empty ( $build [ '#secondary' ]) ? $build : [];
2015-08-18 00:00:26 +00:00
}
/**
* Clears all cached menu data .
*
* This should be called any time broad changes
* might have been made to the router items or menu links .
2018-11-23 12:29:20 +00:00
*
* @ deprecated in Drupal 8.6 . 0 , will be removed before Drupal 9.0 . 0. Use
* \Drupal :: cache ( 'menu' ) -> invalidateAll () instead .
*
* @ see https :// www . drupal . org / node / 2989138
2015-08-18 00:00:26 +00:00
*/
function menu_cache_clear_all () {
2018-11-23 12:29:20 +00:00
@ trigger_error ( " menu_cache_clear_all() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \ Drupal::cache('menu')->invalidateAll() instead. See https://www.drupal.org/node/2989138 " , E_USER_DEPRECATED );
2015-08-18 00:00:26 +00:00
\Drupal :: cache ( 'menu' ) -> invalidateAll ();
}
/**
* @ } End of " addtogroup menu " .
*/