Skip to content

Commit

Permalink
FREEDOM-198 + hook to generate clean title URL alias on page save
Browse files Browse the repository at this point in the history
  • Loading branch information
camilocodes committed Jun 28, 2023
1 parent a991a08 commit 5dfed63
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions config-yml/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module:
mysql: 0
node: 0
node_edit_protection: 0
page: 0
page_cache: 0
path: 0
path_alias: 0
Expand Down
37 changes: 37 additions & 0 deletions custom/modules/aaslp_core/scripts/refresh_pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @file
* Contains refresh_nodes.php.
*
* Clears node bundles for aaslp.lib.unb.ca.
*/

use Drupal\node\Entity\Node;

refresh_nodes('page');

/**
* Loads and saves all nodes from a given bundle.
*
* @param string $bundle
* A string indicating the bundle to update.
*/
function refresh_nodes(string $bundle) {

$nids = \Drupal::entityQuery('node')
->condition('type', $bundle)
->execute();

if (!empty($nids)) {
foreach ($nids as $nid) {
$node = Node::load($nid);

if ($node) {
$title = $node->getTitle();
$node->save();
echo "[-] [$title]->[Updated]\n";
}
}
}
}
6 changes: 6 additions & 0 deletions custom/modules/page/page.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Page Module
type: module
description: Custom/extended functionality for content type page
core: 8.x
core_version_requirement: ^8 || ^9
package: AASLP
28 changes: 28 additions & 0 deletions custom/modules/page/page.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @file
* Contains page.module.
*/

use Drupal\path_alias\Entity\PathAlias;

/**
* Implements hook_ENTITY_TYPE_presave().
*/
function page_node_presave($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 5dfed63

Please sign in to comment.