Last active
March 28, 2023 02:11
-
-
Save paboden/ceaf9d6656a725f1400951fb7f08e42d to your computer and use it in GitHub Desktop.
Drupal 8/9: Enforce Pathauto url generation, even if unchecked on node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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