2

In Drupal 8 you can create path aliases by going to: admin/config/search/path/patterns/add

Which allows me to, for example, say that all content of type "blog_post" can have something like this:

/blog/[category]/[title]

The problem is, I want the TOP path to be different, depending on the category. Specifically, I want any blog post that has the category = Competition, to have:

/competitions/[title]

While still being able to have

/blog/[category]/[title]

I can't seem to get this going. Is there a way to do this? I tried creating another aliases but I don't think there is any path I can specify that will give me this?

2

A solution I've implemented for a potentially similar problem in the past, however it only works if the Category taxonomy is exclusive to the blog content type:

  1. Set the pattern for 'category' taxonomy terms to /blog/[name]
  2. Override the alias for the Competition term to be /competitions
  3. Set the pattern for the blog nodes to reference their category's path.
    This should be something like [test_term_reference:entity:url:path]/[title] (or [test_term_reference:entity:0:url:path]/[title] if category is multi-value)

See https://www.drupal.org/node/2493559 for where Token module implemented tokens for referenced entities.

1

Solution 1: Without code

  • Configure a taxonomy vocabulary named Blog category to manage blog categories.
  • Configure Blog category aliases to /blog/[term:name].
  • For the Competitions term, override the URL alias to /competitions.
  • Configure a taxonomy term reference field (preferably single value) on the Article content type. Say, the field name is field_category.
  • Configure the Article path pattern as /[field_category:entity:url:path]/[node:title].

This is somewhat like what gapple has mentioned above.

Solution 2: With code

  • Configure a taxonomy vocabulary named Blog category to manage blog categories.
  • Configure a taxonomy term reference field (preferably single value) on the Article content type. Say, the field name is field_category.
  • Configure the Article path pattern as /blog/[field_category:entity:name]/[node:title]
  • Use hook_pathauto_pattern_alter() or hook_pathauto_alias_alter() to change the path pattern depending on the $context.

This is what I did for my problem. This way, you can choose whatever alias you want for your taxonomy term.

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.