Skip to content

Instantly share code, notes, and snippets.

@paboden
Last active March 28, 2023 02:11
Show Gist options
  • Select an option

  • Save paboden/ceaf9d6656a725f1400951fb7f08e42d to your computer and use it in GitHub Desktop.

Select an option

Save paboden/ceaf9d6656a725f1400951fb7f08e42d to your computer and use it in GitHub Desktop.
Drupal 8/9: Enforce Pathauto url generation, even if unchecked on node.
//dependencies:
// - pathauto:pathauto
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\pathauto\PathautoState;
use Drupal\Core\Render\Element;
/**
* Implements hook_entity_insert().
*/
function sfgov_utilities_entity_insert(EntityInterface $entity) {
// Enforcing URL generation even when "Generate automatic URL alias" is unchecked.
// This will make sure to apply transliteration cleanup and appropirate URL patterns.
if ($entity->path->pathauto == PathautoState::SKIP) {
\Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'insert');
}
}
/**
* Implements hook_entity_update().
*/
function sfgov_utilities_entity_update(EntityInterface $entity) {
// Enforcing URL generation even when "Generate automatic URL alias" is unchecked.
// This will make sure to apply transliteration cleanup and appropirate URL patterns.
if ($entity->path->pathauto == PathautoState::SKIP) {
\Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'update');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment