Skip to content

Commit

Permalink
FREEDOM-198 Use insert/update hooks to guarantee nid available for URL
Browse files Browse the repository at this point in the history
  • Loading branch information
camilocodes committed Jun 28, 2023
1 parent 5dfed63 commit 6e77554
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions custom/modules/page/page.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Drupal\path_alias\Entity\PathAlias;

/**
* Implements hook_ENTITY_TYPE_presave().
* Implements hook_ENTITY_TYPE_insert().
*/
function page_node_presave($entity) {
function page_node_insert($entity) {
// Only apply to pages.
if ($entity->bundle() == 'page') {
// Auto-generate alias from title.
Expand All @@ -26,3 +26,21 @@ function page_node_presave($entity) {
}
}

/**
* Implements hook_ENTITY_TYPE_update().
*/
function page_node_update($entity) {
// Only apply to pages.
if ($entity->bundle() == 'page') {
// Auto-generate alias from title.
$nid = $entity->id();
$alias = \Drupal::service('pathauto.alias_cleaner')->cleanString(strToLower(trim($entity->title->getValue()[0]['value'])));

$path_alias = PathAlias::create([
'path' => "/node/$nid",
'alias' => "/$alias",
]);

$path_alias->save();
}
}

0 comments on commit 6e77554

Please sign in to comment.