You are here

function token_menu_link_load_all_parents in Token 8

Same name and namespace in other branches
  1. 7 token.module \token_menu_link_load_all_parents()

Loads menu link titles for all purents of a menu link plugin ID.

Parameters

string $plugin_id: The menu link plugin ID.

string $langcode: The language code.

Return value

string[] List of menu link parent titles.

1 call to token_menu_link_load_all_parents()
token_tokens in ./token.tokens.inc
Implements hook_tokens().
2 string references to 'token_menu_link_load_all_parents'
token_clear_cache in ./token.module
Clear token caches and static variables.
token_menu_link_content_presave in ./token.module
Implements hook_ENTITY_TYPE_presave() for menu_link_content.

File

./token.module, line 497
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_menu_link_load_all_parents($plugin_id, $langcode) {
  $cache =& drupal_static(__FUNCTION__, []);
  if (!isset($cache[$plugin_id][$langcode])) {
    $cache[$plugin_id][$langcode] = [];

    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $parent_ids = $menu_link_manager
      ->getParentIds($plugin_id);

    // Remove the current plugin ID from the parents.
    unset($parent_ids[$plugin_id]);
    foreach ($parent_ids as $parent_id) {
      $parent = $menu_link_manager
        ->createInstance($parent_id);
      $cache[$plugin_id][$langcode] = [
        $parent_id => token_menu_link_translated_title($parent, $langcode),
      ] + $cache[$plugin_id][$langcode];
    }
  }
  return $cache[$plugin_id][$langcode];
}