-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FREEDOM-198 + hook to generate clean title URL alias on page save
- Loading branch information
1 parent
a991a08
commit 5dfed63
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ module: | |
mysql: 0 | ||
node: 0 | ||
node_edit_protection: 0 | ||
page: 0 | ||
page_cache: 0 | ||
path: 0 | ||
path_alias: 0 | ||
|
File renamed without changes.
This file contains 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
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"; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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 |
This file contains 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
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(); | ||
} | ||
} | ||
|