function token_menu_link_translated_title in Token 8
Returns the translated link of a menu title.
If the underlying entity is a content menu item, load it to get the translated menu item title.
@todo Remove this when there is a better way to get a translated menu item title in core: https://www.drupal.org/node/2795143
Parameters
\Drupal\Core\Menu\MenuLinkInterface $menu_link: The menu link.
string|null $langcode: (optional) The langcode, defaults to the current language.
Return value
string The menu link title.
3 calls to token_menu_link_translated_title()
- menu_ui_tokens in ./
token.tokens.inc - Implements hook_tokens() on behalf of menu_ui.module.
- token_menu_link_load_all_parents in ./
token.module - Loads menu link titles for all purents of a menu link plugin ID.
- token_tokens in ./
token.tokens.inc - Implements hook_tokens().
File
- ./
token.module, line 533 - Enhances the token API in core: adds a browseable UI, missing tokens, etc.
Code
function token_menu_link_translated_title(MenuLinkInterface $menu_link, $langcode = NULL) {
$metadata = $menu_link
->getMetaData();
if (isset($metadata['entity_id']) && $menu_link
->getProvider() == 'menu_link_content') {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
$entity = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->load($metadata['entity_id']);
if (!empty($entity)) {
$entity = \Drupal::service('entity.repository')
->getTranslationFromContext($entity, $langcode);
return $entity
->getTitle();
}
}
return $menu_link
->getTitle();
}