I am using Drupal Commerce and would like my product url to have the following format:
/main_category/sub_category/product_name.
For this, I am using Pathauto module and have set the alias in the following format:
[commerce_product:field_categories:0]/[commerce_product:field_categories:1]/[commerce_product:title]
What I observed is that, the categories are replaced correctly but the product title does not appear in the url.
But on random shuffling of tokens, such as placing the title token in the beginning, the url appears correctly.
On debugging, I found that field_tokens() in token.tokens.inc file calculates the $delta value for a multi-valued field, like the categories field, and it does so in a for loop for all the tokens of a particular token type present in the pattern.
But the $delta value does not get reset on each iteration of the loop, thereby causing token replacement to not occur correctly.
In my case, after fetching values for [commerce_product:field_categories:0] and [commerce_product:field_categories:1], the value of $delta becomes 1. When the final token value is computed, i.e. for [commerce_product:title], as $delta=1, it is trying to get title[1] from the product entity, which does not exist, as title is only a single valued field. So, since value for replacement of [commerce_product:title] is not available, it does not appear so in the url for the product that I am trying to generate.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | token_replacement_multi_valued_fields-3093596-7.patch | 518 bytes | anton4uk |
| #3 | token-token_replacement_multi_valued_fields-3093596-3-D8.patch | 603 bytes | vivek panicker |
Comments
Comment #2
vivek panicker commentedComment #3
vivek panicker commentedHere I am initializing
$delta = NULLfor each iteration of the loop. That solves the problem for me.Comment #4
vivek panicker commentedComment #5
vivek panicker commentedComment #6
gigabates commentedI've just discovered the same issue and arrived at the same fix. I can confirm that your patch works.
Comment #7
anton4uk commentedReroll for 1.17