Created
January 9, 2014 14:44
-
-
Save MKorostoff/8335164 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* @file | |
* Code for the GFC Pathauto Settings feature. | |
*/ | |
include_once 'gfc_pathauto_settings.features.inc'; | |
/** | |
* Alter Pathauto-generated aliases before saving. | |
* | |
* @param string $alias | |
* The automatic alias after token replacement and strings cleaned. | |
* @param array $context | |
* An associative array of additional options, with the following elements: | |
* - 'module': The module or entity type being aliased. | |
* - 'op': A string with the operation being performed on the object being | |
* aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'. | |
* - 'source': A string of the source path for the alias (e.g. 'node/1'). | |
* This can be altered by reference. | |
* - 'data': An array of keyed objects to pass to token_replace(). | |
* - 'type': The sub-type or bundle of the object being aliased. | |
* - 'language': A string of the language code for the alias (e.g. 'en'). | |
* This can be altered by reference. | |
* - 'pattern': A string of the pattern used for aliasing the object. | |
*/ | |
function gfc_pathauto_settings_pathauto_alias_alter(&$alias, array &$context) { | |
$node = $context['data']['node']; | |
if ($node && isset($node->type) && $node->type == 'article') { | |
$blog_reference = $node->field_blog_reference['und'][0]['nid']; | |
$author_reference = $node->field_article_author_reference['und'][0]['nid']; | |
//this pathauto_alias_alter hook is called twice on node save operations | |
//this static variable is used to keep the setting from getting overridden on the second call | |
static $original_pathauto_setting = 1; | |
if($node->original->path['pathauto'] == 0) { | |
$original_pathauto_setting = 0; | |
} | |
$last_part_of_url = strrchr('/'.$node->path['alias'], '/'); | |
if(empty($last_part_of_url) || $last_part_of_url == '/' || $original_pathauto_setting == 0) { | |
$last_part_of_url = $node->title; | |
} | |
//if pathauto is set automatically generate alias | |
if($node->path['pathauto'] == 1) { | |
//if there is an author but no blog | |
if(($author_reference && !$blog_reference) || ($author_reference && $blog_reference == 124594)) { | |
//load author node | |
$author_node = node_load($author_reference); | |
$middle_part_of_url = $author_node->title; | |
// else if there is both an author and blog | |
} | |
elseif($author_reference && $blog_reference && $blog_reference <> 124594) { | |
//load the blog node | |
$blog_node = node_load($blog_reference); | |
$middle_part_of_url = $blog_node->title; | |
} | |
} | |
//set article path | |
$alias = 'news/'.pathauto_cleanstring($middle_part_of_url).'/'.pathauto_cleanstring($last_part_of_url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment