You are here

function entity_translation_pathauto_alias_alter in Entity Translation 7

Implements hook_pathauto_alias_alter().

When updating or bulk-updating aliases for nodes, automatically create a path for every translation, dependent on related configuration.

See also

entity_translation_admin_form()

File

./entity_translation.module, line 2060

Code

function entity_translation_pathauto_alias_alter(&$alias, array &$context) {
  $info = entity_get_info();
  $entity_type = $context['module'];
  $operations_modes = array(
    'bulkupdate' => variable_get('entity_translation_pathauto_state_mode_bulkupdate', 'generate_all_aliases_all_languages'),
    'update' => variable_get('entity_translation_pathauto_state_mode_update', 'generate_alias_active_language'),
  );
  $operation = $context['op'];

  // Nothing to do when we don't have a replacement pattern or incoming data.
  if (!in_array($operation, array_keys($operations_modes)) || empty($info[$entity_type]['token type']) || empty($context['data'][$info[$entity_type]['token type']])) {
    return;
  }

  // Do not run additional 'update' or 'bulkupdate' operations if the update
  // mode is set to generate an alias only for the active language.
  if (in_array($operation, array_keys($operations_modes)) && $operations_modes[$operation] === 'generate_alias_active_language') {
    return;
  }
  $entity = $context['data'][$info[$entity_type]['token type']];

  // Ensure that we are dealing with an entity bundle having entity translation
  // enabled.
  if (entity_translation_enabled($entity_type, $entity)) {
    $translations = entity_translation_get_handler($entity_type, $entity)
      ->getTranslations();
    foreach ($translations->data as $language => $translation) {

      // We already have an alias for the starting language, so let's not create
      // another one.
      if ($language === $context['language']) {
        continue;
      }
      $no_existing_alias = FALSE === _pathauto_existing_alias_data($context['source'], $language);
      if ($operations_modes[$operation] === 'generate_all_aliases_all_languages' || $no_existing_alias && $operations_modes[$operation] === 'generate_missing_aliases_all_languages') {

        // Modify the context['op'] by appending  '_translation', to avoid infinite
        // recursion.
        pathauto_create_alias($entity_type, $operation . '_translation', $context['source'], $context['data'], $context['type'], $language);
      }
    }
  }
}