1

Is it possible to use a token to reference the alias of a taxonomy term in the pattern of an automatic alias for a node type where that term has been selected, in Drupal 8? (Let's assume a single taxonomy term is selected.)

For example: If one has a taxonomy of Names where the values are James, Margaret, Daniel and each name has a path assigned to it (/news/jim, /news/peggy, /news/dan) is it possible to construct an automatic path for "News" node types where the pattern is something like [taxonomy:Names:path]/[node:title] so that if one selects "Margaret" from the Names list and gives the node a title of "Summer Vacation 2017," it will result in the automatic node path of /news/peggy/summer-vacation-2017?

All the references to path aliases based on taxonomy that I have found use the entity value of the term, which I don't want. I don't want /news/margaret/summer-vacation-2017 as the result of the above example.

5

Using pathauto, it should be quiet easy with a pattern like :

Normal way :

news/[node:{NAME_OF_TAXONOMY_FIELD}:entity:name]/[node:title]

With hierarchy :

news/[node:{NAME_OF_TAXONOMY_FIELD}:entity:parents:join-path]/[node:{NAME_OF_TAXONOMY_FIELD}:entity:name]/[node:title]

In your case, this should work

[node:{NAME_OF_TAXONOMY_FIELD}:entity:url]/[node:title]
2

You could implement hook_pathauto_alias_alter:

/**
 * Implements hook_pathauto_alias_alter().
 */
 function MYMODULE_pathauto_alias_alter(&$alias, array &$context) {

  if ($context['module'] != 'node') {
    return;
  }

  $node = $context['data']['node'];

  // here you can write your logic to get the taxonomy term url alias
  // something like:
  //read out the taxonomy term field of your node
  // get the term $term
  // get the URL Alias of it
  $options =
    [
      'absolute' => false,
    ];
    $termUrl = \Drupal\Core\Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()], $options);

    if ($termUrl && strpos($termUrl,'/taxonomy/term/' === false)) {
      $aliasPath = str_replace('/' . $languageId . '/', '/',$termUrl->toString());
    }
    $alias = $aliasPath . $alias;

  }

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.