1

Using the pathauto module, we could have created custom URL patterns using the hook_pathauto(). I could not find any such way to achieve the same in D8.

ref. https://envisioninteractive.com/drupal/using-the-pathauto-api-drupal-module-a-simple-example/

Can someone please explain is this possible in D8 and if so how.

Thanks

3

In Drupal 8, pathauto patterns are configuration entities. You have at least two choices to provide them with your custom module:

(1) Provide your pattern as YAML configurations in your module's config/install folder. They will be considered and installed to your Drupal, when your module will be installed. Easiest to create using the UI and doing a configuration export afterwards. Just remove the generated UUID of the pattern.

(2) On run-time or in your install/update hooks, you can use Drupal\pathauto\Entity\PathautoPattern::create() or the \Drupal::entityTypeManager() to create them dynamically:

$data = [
  'id' => 'my_pattern_machine_name',
  'type' => 'canonical_entities:node',
  'pattern' => 'my-path/[node:title]',
  'weight' => 0,
];
$pattern = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->create($data);

You can then use the returned pattern entity in $pattern to add selection conditions (e.g. bundles, language, ...) as required. Don't forget to save the entity afterwards, and - of course - check, if the pathauto module has been installed before or make it a dependency of your custom module.

  • As an aside, @Mario, when might the generated UUID cause an issue? I have lazily left UUIDs in place when exporting config and not seen any problems (yet). Or is it merely a matter of tidiness? – Alfred Armstrong Aug 8 '18 at 14:50
  • It IMHO shouldn't matter for the import/installation, as the probability for collisions with existing elements is far too small. Yet, where the code is publicly available for review (as within contrib modules), I always prefer to tidy up any such IDs so they can be generated uniquely for the target system. It just feels like leaving less room for any kind of malicious actions to third parties. – Mario Steinitz Aug 8 '18 at 15:00
  • Understood. But this will simply create another pathauto pattern. I was more curious about the customization that we had in hook_pathauto() like the patterndefault, patternitems, batch operations etc. Actually what I am trying to achieve here is create a separate customized pattern group for one of my vocabulary which will have separate pattern for the parent items and another for the child items. Sorry I was not precise about my requirement before. However, thanks for you input nevertheless. I didnt knew this. :) – Pratip Ghosh Aug 9 '18 at 7:06
  • You can do this by mixing up the above mentioned custom pattern for your vocabulary/bundle and implementing hook_pathauto_pattern_alter(), which is invoked just after a pattern has been applied for an entity and before tokens will be replaced. – Mario Steinitz Aug 9 '18 at 7:12

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.