You are here

function menu_ui_tokens in Token 8

Implements hook_tokens() on behalf of menu_ui.module.

File

./token.tokens.inc, line 1340
Token callbacks for the token module.

Code

function menu_ui_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];

  /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {

    /** @var \Drupal\node\NodeInterface $node */
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'menu-link':

          // On node-form save we populate a calculated field with a menu_link
          // references.
          // @see token_node_menu_link_submit()
          if ($node
            ->getFieldDefinition('menu_link') && ($menu_link = $node->menu_link->entity)) {

            /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */
            $replacements[$original] = $menu_link
              ->getTitle();
          }
          else {
            $url = $node
              ->toUrl();
            if ($links = $menu_link_manager
              ->loadLinksByRoute($url
              ->getRouteName(), $url
              ->getRouteParameters())) {
              $link = _token_menu_link_best_match($node, $links);
              $replacements[$original] = token_menu_link_translated_title($link, $langcode);
            }
          }
          break;
      }

      // Chained token relationships.
      if ($menu_tokens = \Drupal::token()
        ->findWithPrefix($tokens, 'menu-link')) {
        if ($node
          ->getFieldDefinition('menu_link') && ($menu_link = $node->menu_link->entity)) {

          /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */
          $replacements += \Drupal::token()
            ->generate('menu-link', $menu_tokens, [
            'menu-link' => $menu_link,
          ], $options, $bubbleable_metadata);
        }
        else {
          $url = $node
            ->toUrl();
          if ($links = $menu_link_manager
            ->loadLinksByRoute($url
            ->getRouteName(), $url
            ->getRouteParameters())) {
            $link = _token_menu_link_best_match($node, $links);
            $replacements += \Drupal::token()
              ->generate('menu-link', $menu_tokens, [
              'menu-link' => $link,
            ], $options, $bubbleable_metadata);
          }
        }
      }
    }
  }

  // Menu link tokens.
  if ($type == 'menu-link' && !empty($data['menu-link'])) {

    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $link = $data['menu-link'];
    if ($link instanceof MenuLinkContentInterface) {
      $link = $menu_link_manager
        ->createInstance($link
        ->getPluginId());
    }
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'menu':
          if ($menu = Menu::load($link
            ->getMenuName())) {
            $replacements[$original] = $menu
              ->label();
          }
          break;
        case 'edit-url':
          $result = $link
            ->getEditRoute()
            ->setOptions($url_options)
            ->toString(TRUE);
          $bubbleable_metadata
            ->addCacheableDependency($result);
          $replacements[$original] = $result
            ->getGeneratedUrl();
          break;
      }
    }

    // Chained token relationships.
    if (($menu_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'menu')) && ($menu = Menu::load($link
      ->getMenuName()))) {
      $replacements += \Drupal::token()
        ->generate('menu', $menu_tokens, [
        'menu' => $menu,
      ], $options, $bubbleable_metadata);
    }
  }

  // Menu tokens.
  if ($type == 'menu' && !empty($data['menu'])) {

    /** @var \Drupal\system\MenuInterface $menu */
    $menu = $data['menu'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $menu
            ->label();
          break;
        case 'machine-name':
          $replacements[$original] = $menu
            ->id();
          break;
        case 'description':
          $replacements[$original] = $menu
            ->getDescription();
          break;
        case 'menu-link-count':
          $replacements[$original] = $menu_link_manager
            ->countMenuLinks($menu
            ->id());
          break;
        case 'edit-url':
          $result = Url::fromRoute('entity.menu.edit_form', [
            'menu' => $menu
              ->id(),
          ], $url_options)
            ->toString(TRUE);
          $bubbleable_metadata
            ->addCacheableDependency($result);
          $replacements[$original] = $result
            ->getGeneratedUrl();
          break;
      }
    }
  }
  return $replacements;
}