Problem/Motivation

The token module introduces some pretty helpful tokens for the type menu-link. But it also provides tokens for the entity type menu_link_content which leads to 2 different sections in the token tree. And if the TokenEntityMapper service comes into play for the menu-link token type, this can get confusing quite easily.

Proposed resolution

Update the entity type info for menu_link_content just like for taxonomy terms.

Issue fork token-3314427

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jurgenhaas created an issue. See original summary.

jurgenhaas’s picture

Status: Active » Needs review
berdir’s picture

Doing that now is a BC change and I don't agree with it. menu links can be many things, not just menu_link_content and merging those two together is not correct and will not work as it won't be the entity object being passed. Beside, it will likely also stop generating base field tokens as there are already tokens for this.

jurgenhaas’s picture

Doing that now is a BC change

Agreed.

will not work as it won't be the entity object being passed

Well, "sometimes" it is. I came across the problem described in this issue in a pathauto scenario. When editing a node and providing a menu link, the token menu-link is passed in with data being \Drupal\menu_link_content\Entity\MenuLinkContent. However, when bulk updating pathauto with drush, the data being passed for menu-link is an \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent object.

I think, receiving a plugin rather than a content entity makes more sense, but I wasn't able to figure out why pathauto behaves differently here. Any ideas?

berdir’s picture

token.module has a special computed field that supports changing the menu link as you save a node, that's probably why that behaves different. I wasn't aware that it passes in the entity.

token_tokens() has code that unifies it to the plugin object, you just just copy that as a quickfix if you have your own tokens.

jurgenhaas’s picture

OK, found the code where the different data types get passed. It happens in menu_ui_tokens contained in token.tokens.inc:

      // 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);
          }
        }
      }

When editing the node in the UI, the if-statement ($node->getFieldDefinition('menu_link') && $menu_link = $node->menu_link->entity) returns true and therefore uses the MenuLinkContentIniterface, i.e. the content entity.

When using drush with bulk update, this if-statement returns FALSE and it uses the link from the menu_link_manager, i.e. a plugin.

Any idea, why the first version doesn't also load a plugin but the content entity instead?

berdir’s picture

> Any idea, why the first version doesn't also load a plugin but the content entity instead?

No, and that is a change that I can get behind, so we would basically move/copy the logic in token_tokens() to that place.

jurgenhaas’s picture

OK, I've changed the MR accordingly. Removed the original change and added the new one.

jurgenhaas’s picture

I have updated the MR to work with the OO hooks.

berdir’s picture

Title: Token type for menu_link_content entities should be set to menu-link » Convert menu_link_content entities to a menu plugin when replacing menu_link tokens

  • berdir committed d1396340 on 8.x-1.x authored by jurgenhaas
    [#3314427] feat: Convert menu_link_content entities to a menu plugin...
berdir’s picture

Status: Needs review » Fixed

Merged.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.