Problem/Motivation

There is a token returning names of all parents of a menu link. In some situations (eg. pathauto + easy_breadcrumb) it is required to get paths of parent menu items and there is no such token.

Proposed resolution

Add [menu-link:parents-paths] token and write tests.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

blazey created an issue. See original summary.

blazey’s picture

blazey’s picture

Status: Active » Needs review

The last submitted patch, 2: 2847694-2-test-only.patch, failed testing.

The last submitted patch, 2: 2847694-2-test-only.patch, failed testing.

blazey’s picture

Attaching combined patch.

Leksat’s picture

@blazey,

I tested your patch with simplytest.

1. Somehow, the new token did not work for me.
I set pathauto pattern for content like "[node:menu-link:parents-paths]/[node:title]" and created 3 nodes with menu links one under another. Here is the result: https://www.drupal.org/files/issues/2850780-2-test.png

But if it worked, I guess, it would produce something like this:
- /test-1
-- /test-1/test-2
--- /test-1/test-1/test-2/test-3
Correct?
If yes - this does not really make sense.

2. I tried to use "[node:menu-link:parent:url:path]/[node:title]" pattern instead.
The result looks like something SEO friendly: https://www.drupal.org/files/issues/2850780-node--menu-link--parent--url...
Maybe this is what you tried to achieve?

blazey’s picture

@leksat [node:menu-link:parents-paths] is an array type token so separator is needed in order to use it as a string. [node:menu-link:parents-paths:join:/] would work in this scenario.

I agree navigation can be done this way. In fact, I ended up using [node:menu-link-from-menu:main:parent:url:path] from #2850780: Can't reference node's menu link from a specific menu. Maybe at some point someone will need combined paths too :)

Berdir’s picture

Status: Needs review » Needs work

Thanks, started reviewing the test but the last point might make the others irrelevant.

  1. +++ b/tests/src/Kernel/MenuTest.php
    @@ -0,0 +1,161 @@
    +/**
    + * Test the node and content type tokens.
    + *
    

    wrong comment.

  2. +++ b/tests/src/Kernel/MenuTest.php
    @@ -0,0 +1,161 @@
    +
    +  /**
    +   * Creates a node.
    +   */
    +  protected function addNode($title) {
    +    Node::create([
    +      'title' => $title,
    +      'type' => 'page',
    +    ])->save();
    +  }
    

    Should be possible to use \Drupal\simpletest\NodeCreationTrait for this..

  3. +++ b/tests/src/Kernel/MenuTest.php
    @@ -0,0 +1,161 @@
    +    // First remove all the menu links in the menu.
    +    $this->menuLinkManager->deleteLinksInMenu('token_test');
    

    why would there be menu links in a menu that we just created?

  4. +++ b/tests/src/Kernel/MenuTest.php
    @@ -0,0 +1,161 @@
    +    $parent = $base_options + array(
    +        'link' => ['uri' => 'entity:node/1'],
    +      );
    ...
    +   * Tests menu tokens.
    +   */
    +  public function testMenuTokens() {
    +    $links = $this->createLinkHierarchy();
    +
    

    Actually, wondering if we can't integrate this into \Drupal\token\Tests\TokenMenuTest, that already creates menu links, so we just need to add the additional tokens?