diff --git a/entity_translation.module b/entity_translation.module index 6ef4200..df4cadf 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -2059,13 +2059,84 @@ function entity_translation_pathauto_alias_alter(&$alias, array &$context) { if ($language == $pathauto_start_language) { continue; } - pathauto_create_alias($entity_type, $context['op'], $context['source'], $context['data'], $context['type'], $language); + if (entity_translation_get_pathauto_state($entity_type, $entity, $language, $context)) { + pathauto_create_alias($entity_type, $context['op'], $context['source'], $context['data'], $context['type'], $language); + } } } } } } +/** + * Checks if a pathauto alias has to be created / refreshed. + * + * @param string $entity_type + * The entity type to handle. + * @param object $entity + * The entity to process. + * @param string $language + * The language code to use. + * @param array $context + * The pathauto context to use. + * + * @return bool + * TRUE if the alias has to be created / refreshed. + */ +function entity_translation_get_pathauto_state($entity_type, $entity, $language, $context) { + static $pathauto_entity_state_load_is_language_aware; + + // Check if we can rely on pathauto_entity_state_load(). + // If so this defines the pathauto state for this language - otherwise + // we fall back to the way how pathauto detected auto aliasing in + // previous versions. + // @TODO Make https://www.drupal.org/project/pathauto/issues/2614736 + // happening. + if (!isset($pathauto_entity_state_load_is_language_aware)) { + $pathauto_entity_state_load_is_language_aware = FALSE; + try { + $pathauto_entity_state_load = new ReflectionFunction('pathauto_entity_state_load'); + // Callback to determine if the language parameter is available. + $parameter_filter = function($carry, $item) { + return $carry || ($item->name === 'language'); + }; + $pathauto_entity_state_load_is_language_aware = array_reduce($pathauto_entity_state_load->getParameters(), $parameter_filter, FALSE); + } + catch (\ReflectionException $e){ + // The function doesn't even exist yet. + } + } + + $info = entity_get_info($entity_type); + try { + list($entity_id,,) = entity_extract_ids($entity_type, $entity); + } + catch (EntityMalformedException $e) { + return FALSE; + } + + // Check if we can rely on pathauto_entity_state_load(). + if ($pathauto_entity_state_load_is_language_aware) { + return (bool) pathauto_entity_state_load($entity_type, $entity_id, $language); + } + else { + // We need to rely on a unchanged entity to handle this. Check if we aleardy + // have the necessary property and if not load it for later re-use. + if (!isset($entity->original)) { + $entity->original = entity_load_unchanged($entity_type, $entity_id); + } + // Only trigger if path is set to generate automatically. We can't + // rely on pathauto_state because it's not language aware. So we + // re-use the approach from pathauto_field_attach_form(). + module_load_include('inc', 'pathauto'); + $data = $context['data']; + $data[$info['token type']] = $entity->original; + $pathauto_alias = pathauto_create_alias($entity_type, 'return', $context['source'], $data, $context['type'], $language); + $path = drupal_get_path_alias($context['source'], $language); + return ($path != $context['source'] && $path == $pathauto_alias); + } +} + /** * Implements hook_entity_translation_delete(). */