diff --git pathauto.module pathauto.module index 0efb7cc..a292bcc 100644 --- pathauto.module +++ pathauto.module @@ -300,9 +300,16 @@ function pathauto_form_alter(&$form, &$form_state, $form_id) { // Process only node forms. if (!empty($form['#node_edit_form'])) { $node = $form['#node']; + $language = isset($node->language) ? $node->language : LANGUAGE_NONE; + } + // Or entity translation forms. + if ($form_id == 'entity_translation_edit_form' && $form['#entity_type'] == 'node') { + $node = $form['#entity']; + $language =$form['#language']; + } + if (isset($node)) { // Find if there is an automatic alias pattern for this content type. - $language = isset($node->language) ? $node->language : LANGUAGE_NONE; $pattern = pathauto_pattern_load_by_entity('node', $node->type, $language); // If there is a pattern, show the automatic alias checkbox. @@ -315,7 +322,7 @@ function pathauto_form_alter(&$form, &$form_state, $form_id) { module_load_include('inc', 'pathauto'); $uri = entity_uri('node', $node); $path = drupal_get_path_alias($uri['path'], $language); - $pathauto_alias = pathauto_create_alias('node', 'return', $uri['path'], array('node' => $node), $node->type, $node->language); + $pathauto_alias = pathauto_create_alias('node', 'return', $uri['path'], array('node' => $node), $node->type, $language); $node->path['pathauto'] = $path != $uri['path'] && $path == $pathauto_alias; } else { @@ -389,15 +396,14 @@ function pathauto_node_update_alias(stdClass $node, $op, array $options = array( return; } - // Skip processing if the node has no pattern. - $language = isset($node->language) ? $node->language : LANGUAGE_NONE; - if (!pathauto_pattern_load_by_entity('node', $node->type, $language)) { - return; + $options += array( + 'language' => ((isset($node->language) ? $node->language : LANGUAGE_NONE)), + ); + if (pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) { + module_load_include('inc', 'pathauto'); + $uri = entity_uri('node', $node); + pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']); } - - module_load_include('inc', 'pathauto'); - $uri = entity_uri('node', $node); - pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $language); } /** @@ -464,7 +470,10 @@ function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options return; } - $options += array('alias children' => FALSE); + $options += array( + 'alias children' => FALSE, + 'language' => LANGUAGE_NONE, + ); $module = 'taxonomy_term'; if ($term->vid == variable_get('forum_nav_vocabulary', '')) { @@ -489,7 +498,7 @@ function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options module_load_include('inc', 'pathauto'); $uri = entity_uri('taxonomy_term', $term); - pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name); + pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']); if (!empty($options['alias children'])) { // For all children generate new alias. @@ -578,7 +587,10 @@ function pathauto_user_update_alias(stdClass $account, $op, array $options = arr return; } - $options += array('alias blog' => module_exists('blog')); + $options += array( + 'alias blog' => module_exists('blog'), + 'language' => LANGUAGE_NONE, + ); // Skip processing if the account has no pattern. if (!pathauto_pattern_load_by_entity('user')) { @@ -587,11 +599,11 @@ function pathauto_user_update_alias(stdClass $account, $op, array $options = arr module_load_include('inc', 'pathauto'); $uri = entity_uri('user', $account); - pathauto_create_alias('user', $op, $uri['path'], array('user' => $account)); + pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']); // Because blogs are also associated with users, also generate the blog paths. if (!empty($options['alias blog'])) { - pathauto_blog_update_alias($account, $op); + pathauto_blog_update_alias($account, $op, array('language' => $options['language'])); } } @@ -636,11 +648,37 @@ function pathauto_blog_update_alias(stdClass $account, $op, array $options = arr return; } + $options += array( + 'language' => LANGUAGE_NONE, + ); + module_load_include('inc', 'pathauto'); if (node_access('create', 'blog', $account)) { - pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account)); + pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']); } else { pathauto_path_delete_all("blog/{$account->uid}"); } } + +/** + * Implements hook_entity_translation_save(). + * + * @param string $entity_type + * @param stdClass $entity + * @param string $langcode + */ +function pathauto_entity_translation_save($entity_type, $entity, $langcode) { + $options = array('language' => $langcode); + switch ($entity_type) { + case 'node': + pathauto_node_update_alias($entity, 'update', $options); + break; + case 'taxonomy_term': + pathauto_taxonomy_term_update_alias($entity, 'update',$options); + break; + case 'user': + pathauto_user_update_alias($entity, 'update', $options); + break; + } +} diff --git pathauto.pathauto.inc pathauto.pathauto.inc index 6f9f282..0345488 100644 --- pathauto.pathauto.inc +++ pathauto.pathauto.inc @@ -90,7 +90,20 @@ function node_pathauto_bulk_update_batch_process(&$context) { } $query = db_select('node', 'n'); - $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source"); + + // Support for entity translation + if (module_exists('entity_translation') && function_exists('entity_translation_enabled') && entity_translation_enabled('node')) { + // Make sure that every translation of a node has an alias. + $query->leftJoin('entity_translation', 't', "t.entity_type = 'node' AND t.entity_id = n.nid"); + $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) AND ua.`language` = t.`language`"); + $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source"); + $query->addField('t', 'language'); + } + else { + $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source"); + $query->addField('n', 'language'); + } + $query->addField('n', 'nid'); $query->isNull('ua.source'); $query->condition('n.nid', $context['sandbox']['current'], '>'); @@ -110,11 +123,19 @@ function node_pathauto_bulk_update_batch_process(&$context) { } $query->range(0, 25); - $nids = $query->execute()->fetchCol(); + $items = $query->execute()->fetchAll(); - pathauto_node_update_alias_multiple($nids, 'bulkupdate'); - $context['sandbox']['count'] += count($nids); - $context['sandbox']['current'] = max($nids); + // Group by language + $language_nids = array(); + foreach ($items as $item) { + $language_nids[$item->language][] = $item->nid; + } + + foreach ($language_nids as $language => $nids) { + pathauto_node_update_alias_multiple($nids, 'bulkupdate', array('language' => $language)); + $context['sandbox']['count'] += count($nids); + $context['sandbox']['current'] = max($nids); + } $context['message'] = t('Updated alias for node @nid.', array('@nid' => end($nids))); if ($context['sandbox']['count'] != $context['sandbox']['total']) {