Last active
December 25, 2015 18:49
-
-
Save joakim/7023001 to your computer and use it in GitHub Desktop.
Drupal: Save additional path aliases for subpages. Could be used with nodes to make node/123/edit (with alias "foo") look like foo/edit. Prettier.
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
/** | |
* Implements hook_node_presave(). | |
*/ | |
function custom_node_presave($node) { | |
// Get the source of the "base" path which has subpages. | |
$source = 'node/' . $node->nid; | |
// If the node's alias is "foo", foo/edit will be an alias for node/123/edit. | |
custom_save_subpage_alias($source, 'edit'); | |
// If the node's alias is "foo", foo/debug will be an alias for node/123/devel. | |
custom_save_subpage_alias($source, 'devel', 'debug'); | |
} | |
function custom_save_subpage_alias($base_source, $source_suffix, $alias_suffix = NULL) { | |
$base_alias = drupal_lookup_path('alias', $base_source); | |
if ($base_alias) { | |
// If no alias suffix, the source's suffix will be used for the alias too. | |
if (empty($alias_suffix)) { | |
$alias_suffix = $source_suffix; | |
} | |
$path = array( | |
'source' => "$base_source/$source_suffix", | |
'alias' => "$base_alias/$alias_suffix", | |
); | |
// Only save the subpage alias if it doesn't already exist. | |
$alias_exists = (bool) drupal_lookup_path('source', $path['alias']); | |
if (!$alias_exists) { | |
path_save($path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment