You are here

function menu_token_menu_links_discovered_alter in Menu Token 8

Same name and namespace in other branches
  1. 9.1.x menu_token.module \menu_token_menu_links_discovered_alter()

Replace links with tokens.

File

./menu_token.module, line 422
Contains menu_token.module.

Code

function menu_token_menu_links_discovered_alter(&$links) {
  $context_manager = \Drupal::service('menu_token.context_manager');
  $context_manager
    ->clear();
  $undiscoveredDef = $context_manager
    ->getUndiscoveredMenuDefinitions();
  $links = array_merge($links, $undiscoveredDef);

  // Load configuration from entity.
  $relevant_links = array_filter($links, function ($k) {
    if (!isset($k['id'])) {
      $k['id'] = 0;
    }
    return strpos($k['id'], 'menu_link_content:') === 0;
  });
  $token_service = \Drupal::token();
  $bubbleable_metadata = new BubbleableMetadata();
  if (!empty($relevant_links) && is_array($relevant_links)) {
    foreach ($relevant_links as $relevant_link) {
      $uuId_from_link = substr($relevant_link['id'], strpos($relevant_link['id'], ":") + 1, strlen($relevant_link['id']));
      $configMenu = \Drupal::entityTypeManager()
        ->getStorage('link_configuration_storage')
        ->load($uuId_from_link);
      if (!empty($configMenu)) {
        $config = unserialize($configMenu->configurationSerialized);
        $context_manager
          ->prepareContextualLinks($relevant_link, $config);
        replace_links_with_tokens($token_service, "url", $relevant_link, $links, $bubbleable_metadata);
        replace_links_with_tokens($token_service, "title", $relevant_link, $links, $bubbleable_metadata);
      }
    }
  }
}