1

Thanks to pathauto and tokens it is very easy to generate URLs with [node:field_product_category:entity:parents:join-path]/[node:field_product_category:entity]/[node:title] \o/

But … I would like to start at the 2nd level, so is it possible to exclude the root level from [node:field_product_category:entity:parents:join-path]?

Example

What I have got: http://www.example.com/redundant_root_category/category_from_level_2/category_from_level_3/super_product

What I want: http://www.example.com/category_from_level_2/category_from_level_3/super_product

3

The default tokens won't allow you to do this, but the only thing you're missing I think is an array token that removes the first element, I'd call it "tail". See token_token_info(), which defines for example a 'reversed and other array tokens:

$info['tokens']['array']['reversed'] = array(
  'name' => t('Reversed'),
  'description' => t('The array reversed.'),
'type' => 'array',
);

And the implementation for that is in token_tokens():

// [array:reversed:*] chained tokens.
if ($reversed_tokens = \Drupal::token()->findWithPrefix($tokens, 'reversed')) {
  $replacements += \Drupal::token()->generate('array', $reversed_tokens, array('array' => array_reverse($array, TRUE)), array('array sort' => FALSE) + $options, $bubbleable_metadata);
}

Instead of array_reverse(), you'd call array_shift() on the array first and then pass that along.

Might also be a useful standard token to have in the token.module, if someone wants to provide a patch.

Your token would then look like this:

[node:field_product_category:entity:parents:tail:join-path]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.