1 path.inc | path_generate_entity_alias(Entity $entity, $source = NULL, $langcode = NULL) |
Apply patterns to create an alias for an entity.
Parameters
Entity $entity: A node, user, taxonomy term, or other entity for which patterns exist.
string $source: An internal Backdrop path to be aliased. If not specified, the path of the provided entity will be used, e.g. node/1.
string $langcode: A string specify the path's language. If not specified, the language of the given entity will be used.
Return value
string|FALSE: The automatically generated alias or FALSE if no automatically generated alias should be created.
See also
File
- core/
modules/ path/ path.inc, line 412 - Miscellaneous functions for Path module.
Code
function path_generate_entity_alias(Entity $entity, $source = NULL, $langcode = NULL) {
$config = config('path.settings');
$entity_type_name = $entity->entityType();
$entity_type = entity_get_info($entity_type_name);
$token_type = isset($entity_type['token type']) ? $entity_type['token type'] : NULL;
$bundle = $entity->bundle();
if (!isset($source)) {
$uri = $entity->uri();
$source = $uri['path'];
}
if (!isset($langcode)) {
$langcode = isset($entity->langcode) ? $entity->langcode : LANGUAGE_NONE;
}
// Retrieve and apply the pattern for this content type.
$pattern = path_get_pattern_by_entity_type($entity_type_name, $bundle, $langcode);
// Allow other modules to alter the pattern.
$context = array(
'entity' => $entity,
'source' => $source,
'langcode' => $langcode,
);
backdrop_alter('path_pattern', $pattern, $context);
if (empty($pattern)) {
// No pattern? Do nothing (otherwise we may blow away existing aliases...)
return FALSE;
}
// Special handling when updating an item which is already aliased.
$existing_path = NULL;
if (!$entity->isNew()) {
if ($existing_path = _path_load_loosely_by_source($source, $langcode)) {
switch ($config->get('update_action')) {
case PATH_UPDATE_ACTION_NO_NEW:
// If an alias already exists, and the update action is set to do
// nothing, then return.
return FALSE;
}
}
}
// Replace any tokens in the pattern. Uses callback option to clean
// replacements. No sanitization.
$data = array();
if ($token_type) {
$data[$token_type] = $entity;
}
$alias = token_replace($pattern, $data, array(
'sanitize' => FALSE,
'clear' => TRUE,
'callback' => 'path_clean_token_values',
'langcode' => $langcode,
));
// Check if the token replacement has not actually replaced any values. If
// that is the case, then stop because we should not generate an alias.
// @see token_scan()
$pattern_tokens_removed = preg_replace('/\[[^\s\]:]*:[^\s\]]*\]/', '', $pattern);
if ($alias === $pattern_tokens_removed) {
return FALSE;
}
$alias = path_clean_alias($alias);
// Allow other modules to alter the alias.
$context['source'] = &$source;
$context['pattern'] = $pattern;
backdrop_alter('path_alias', $alias, $context);
// If we have arrived at an empty string, discontinue.
if (!backdrop_strlen($alias)) {
return FALSE;
}
// If the alias already exists, generate a new, hopefully unique, variant.
$original_alias = $alias;
path_alias_uniquify($alias, $source, $langcode);
if ($original_alias != $alias) {
// Alert the user why this happened.
path_verbose_message(t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', array(
'%original_alias' => $original_alias,
'%alias' => $alias,
)), $entity->isNew() ? 'insert' : 'update');
}
return $alias;
}