View source
<?php
namespace HookUpdateDeployTools;
class Paths {
public static function modifyAlias($original_alias, $new_alias, $language) {
try {
$return = self::checkSame($original_alias, $new_alias);
if (empty($return)) {
$alias = self::getAliasObject($original_alias, $new_alias, $language);
if (self::checkAliasesExist($alias)) {
$return = self::changeAlias($alias);
}
}
return $return;
} catch (\Exception $e) {
$vars = array(
'!error' => method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage(),
'@file' => $e
->getFile(),
'@line' => $e
->getLine(),
);
if (!method_exists($e, 'logMessage')) {
$message = 'Paths::modifyAlias failed in "@file" on line @line. Message: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR);
}
}
private static function changeAlias($alias) {
$alias->original->path = _pathauto_existing_alias_data($alias->original->source, $alias->original->language);
$variables = array(
'!new_alias' => $alias->new->alias,
'!old_alias' => $alias->original->alias,
);
if (!empty($alias->original->path)) {
$alias->new->path = $alias->original->path;
$alias->new->path['alias'] = $alias->new->alias;
}
else {
$message = "The original alias of could not be loaded. This is most likely due to specifying the wrong language in the call to modifyAlias. Adjust your update hook and try again.";
Message::make($message, $variables, WATCHDOG_ERROR);
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
$saved_alias = _pathauto_set_alias($alias->new->path, $alias->original->path);
if (!empty($saved_alias)) {
$message = "'!new_alias' has been set as the new alias for what used to be '!old_alias'.";
$return = Message::make($message, $variables, WATCHDOG_INFO);
}
else {
$message = "Alias save of '!new_alias' was not successful in replacing '!old_alias'. Sorry, unable to say why. Adjust your update hook and try again.";
Message::make($message, $variables, WATCHDOG_ERROR);
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
return $return;
}
private static function checkSame($original_alias, $new_alias) {
if ($original_alias == $new_alias) {
$message = "The requested alias change of '!original' to '!new' is the same. No alteration required.";
$variables = array(
'!original' => $original_alias,
'!new' => $new_alias,
);
$return = Message::make($message, $variables, WATCHDOG_INFO);
$return = !empty($return) ? $return : TRUE;
}
else {
$return = FALSE;
}
return $return;
}
public static function checkAliasesExist($alias) {
$variables = array(
'!original_alias' => $alias->original->alias,
'!new_alias' => $alias->new->alias,
);
if (empty($alias->original->source) && empty($alias->new->source)) {
$message = "Alias '!original_alias' does not exist so could not be altered, and '!new_alias' does not already exist. Adjust your update hook and try again.";
Message::make($message, $variables, WATCHDOG_ERROR);
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
elseif (empty($alias->original->source) && !empty($alias->new->source)) {
$message = "'!new_alias' already exists and '!original_alias' does not. Assuming this change has already been made.";
$return = Message::make($message, $variables, WATCHDOG_INFO);
$return = FALSE;
}
elseif (!empty($alias->original->source) && !empty($alias->new->source)) {
$message = "'!new_alias' already exists and '!original_alias' does too. The pre-existance of the new alias blocks the change.";
Message::make($message, $variables, WATCHDOG_ERROR);
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
else {
$return = TRUE;
}
return $return;
}
private static function getAliasObject($original_alias, $new_alias, $language) {
$alias = new \stdClass();
Check::canUse('pathauto');
$alias->original = new \stdClass();
$alias->new = new \stdClass();
$alias->original->alias = $original_alias;
module_load_include('inc', 'pathauto', 'pathauto');
Check::canCall('pathauto_clean_alias');
$alias->new->alias = pathauto_clean_alias($new_alias);
$alias->original->language = $language;
$alias->new->language = $language;
$alias->original->source = drupal_lookup_path('source', $original_alias, $language);
$alias->new->source = drupal_lookup_path('source', $new_alias, $language);
return $alias;
}
}