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]
Comment | File | Size | Author |
---|---|---|---|
#15 | Screen Shot 2018-01-24 at 5.18.04 PM.png | 124.68 KB | cameron prince |
#10 | interdiff-8-10.txt | 1.31 KB | navneet0693 |
#10 | add_path_alias_node_token-2937775-10.patch | 3.21 KB | navneet0693 |
#8 | interdiff-6-8.txt | 2.09 KB | navneet0693 |
#8 | add_path_alias_node_token-2937775-8.patch | 2.75 KB | navneet0693 |
Comments
Comment #2
cameron prince CreditAttribution: cameron prince commentedComment #3
DamienMcKennaComment #4
timmillwoodComment #5
tstoecklerThis should pass
$node->language()->getId()
to the alias manager to fetch the alias in the correct language.Comment #6
navneet0693 CreditAttribution: navneet0693 as a volunteer and at QED42 commentedComment #7
timmillwoodWouldn't this get the alias for '/node/en', rather than '/node/1'?
I think we should explicitly test it's '/fancy-node' and not call the service.
Comment #8
navneet0693 CreditAttribution: navneet0693 as a volunteer and at QED42 commentedThanks for review @timmillwood. You are right about #7.1.
Comment #9
timmillwoodInstead 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 inNode::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?
Comment #10
navneet0693 CreditAttribution: navneet0693 as a volunteer and at QED42 commentedComment #11
timmillwoodLooks 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.
Comment #12
larowlanThe 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.
Comment #13
cameron prince CreditAttribution: cameron prince commentedWhat you've linked there isn't a node token.
Comment #14
BerdirYes 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.
Comment #15
cameron prince CreditAttribution: cameron prince commentedOkay, 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.
Comment #16
BerdirYes, 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.
Comment #17
cameron prince CreditAttribution: cameron prince commentedUnderstood and thank you. I can confirm that [node:field_product:entity:url:path]/[node:title] works in my use case.
Comment #18
cameron prince CreditAttribution: cameron prince commentedComment #19
PieterDCI can also confirm what @cameronprince confirmed!
Although it's not clear from the UI (Token browser), you can chain tokens like that.
Comment #20
Kasey_MK CreditAttribution: Kasey_MK commented[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.