Site builders may have a need to use the path of a referenced entity in the path of an entity. The existing URL node token returns the full URL to a node and isn't suitable for use as a pathauto replacement pattern token to meet this need.

The attached patch adds a new node token called "Path alias" which returns only the node's path alias rather than the full URL.

This allows a pathauto pattern such as: [node:field_reference:entity:path-alias]/[node:title]

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cameronbprince created an issue. See original summary.

cameron prince’s picture

DamienMcKenna’s picture

Status: Active » Needs review
timmillwood’s picture

Issue tags: +Needs tests
tstoeckler’s picture

Status: Needs review » Needs work
Issue tags: +D8MI, +language-content

This should pass $node->language()->getId() to the alias manager to fetch the alias in the correct language.

navneet0693’s picture

Status: Needs work » Needs review
FileSize
2.76 KB
2.38 KB
timmillwood’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
  1. +++ b/core/modules/node/node.tokens.inc
    @@ -173,6 +177,10 @@ function node_tokens($type, $tokens, array $data, array $options, BubbleableMeta
    +          $replacements[$original] = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $node->language()->getId());
    

    Wouldn't this get the alias for '/node/en', rather than '/node/1'?

  2. +++ b/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php
    @@ -69,6 +70,7 @@ public function testNodeTokenReplacement() {
    +    $tests['[node:path-alias]'] = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $node->language()->getId());
    

    I think we should explicitly test it's '/fancy-node' and not call the service.

navneet0693’s picture

Status: Needs work » Needs review
FileSize
2.75 KB
2.09 KB

Thanks for review @timmillwood. You are right about #7.1.

timmillwood’s picture

Status: Needs review » Needs work
+++ b/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php
@@ -56,6 +56,7 @@ public function testNodeTokenReplacement() {
+    \Drupal::service('path.alias_storage')->save($node->toUrl()->toString(), '/fancy-node', $node->language()->getId());

Instead of calling the alias_storage directly we could add 'path' to the list of $modules for the test. Then add 'path' => ['alias' => '/fancy-node'], to the array in Node::create().

Although because we're adding the token in node.tokens.inc, and not in path module, maybe we shouldn't depend on path in the test?

navneet0693’s picture

Status: Needs work » Needs review
FileSize
3.21 KB
1.31 KB
timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, only concern now is the use case when path modules is not installed is untested, but I think that is such an edge case because path modules is just calling the core services anyway.

larowlan’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

The original summary here states that this is needed for path auto.

However path auto module relies on token module.

And token module already provides this functionality

So I'm not sure why we need this?

I think this should be `Closed (Works as designed)` unless I'm missing something.

FWIW I've been using the path tokens from token module with pathauto in Drupal 8 without any issues since 8.0.

cameron prince’s picture

What you've linked there isn't a node token.

Berdir’s picture

Yes it is with token module: http://cgit.drupalcode.org/token/tree/token.tokens.inc#n62. So with token module, which as @larowlan said is a dependency of pathauto, you can use node:url:path.

What would be useful is moving the url token type into core (and most other tokens, if we want to continue promoting that API and not replace it entirely with twig or something) and use it everywhere, not just node.

cameron prince’s picture

Okay, I see what you are saying... I suppose it's a limitation of the token browser. See the screenshot shows only the url token when you expand the reference field.

Berdir’s picture

Yes, it doesn't go down more than 4 levels, that is already enough to kill large sites with lots of fields :)

The only solution is to rewrite that UI to do lazy-loading on expand so you can expand as much as you want.

cameron prince’s picture

Issue summary: View changes

Understood and thank you. I can confirm that [node:field_product:entity:url:path]/[node:title] works in my use case.

cameron prince’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
PieterDC’s picture

I can also confirm what @cameronprince confirmed!
Although it's not clear from the UI (Token browser), you can chain tokens like that.

Kasey_MK’s picture

[node:book:parent:url.path]/[node:title] works for me on Drupal 8.6.15 to use the book parent's path as the base of my pathauto pattern for a new book page. [node:book:parent:url:path] (with a colon instead of a period) did not work.

Thank you, larowlan, for your pointer to the Token documentation, where I found that use of a period.