From 294763a2675ef81dac29d537900f4113150dcdf8 Mon Sep 17 00:00:00 2001 From: Colan Schwartz Date: Sun, 2 Aug 2015 22:31:11 -0400 Subject: [PATCH] Issue #1481798 by das-peter, DeFr, e.escribano, romualdbrisson, colan: Add Entity Translation support. --- xmlsitemap.install | 10 ++++- xmlsitemap.module | 53 +++++++++++++++++++++++++- xmlsitemap_i18n/xmlsitemap_i18n.install | 27 +++++++++++++ xmlsitemap_node/xmlsitemap_node.module | 39 +++++++++++++++++-- xmlsitemap_taxonomy/xmlsitemap_taxonomy.module | 40 +++++++++++++++++-- 5 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 xmlsitemap_i18n/xmlsitemap_i18n.install diff --git a/xmlsitemap.install b/xmlsitemap.install index 916ebec..f89a030 100644 --- a/xmlsitemap.install +++ b/xmlsitemap.install @@ -239,7 +239,7 @@ function xmlsitemap_schema() { 'default' => 0, ), ), - 'primary key' => array('id', 'type'), + 'primary key' => array('id', 'type', 'language'), 'indexes' => array( 'loc' => array('loc'), 'access_status_loc' => array('access', 'status', 'loc'), @@ -568,3 +568,11 @@ function _xmlsitemap_sitemap_rehash_all() { } } } + +/** + * Add new primary key including the language + */ +function xmlsitemap_update_7204() { + db_drop_primary_key('xmlsitemap'); + db_add_primary_key('xmlsitemap', array('id', 'type', 'language')); +} diff --git a/xmlsitemap.module b/xmlsitemap.module index 09d02ea..442f511 100644 --- a/xmlsitemap.module +++ b/xmlsitemap.module @@ -603,7 +603,17 @@ function xmlsitemap_link_save(array $link, array $context = array()) { $link['changecount'] = 0; } - $existing = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id", 0, 1, array(':type' => $link['type'], ':id' => $link['id']))->fetchAssoc(); + $link_keys = array('type', 'id'); + + // If entity translation exists and the link has a language we need to add the + // language to the query to fetch the unique / language specific link. + if (module_exists('entity_translation') && entity_get_info($link['type']) && entity_translation_enabled($link['type']) && !empty($link['language'])) { + $existing = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id AND language = :language", 0, 1, array(':type' => $link['type'], ':id' => $link['id'], ':language' => $link['language']))->fetchAssoc(); + $link_keys[] = 'language'; + } + else { + $existing = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id", 0, 1, array(':type' => $link['type'], ':id' => $link['id']))->fetchAssoc(); + } // Check if this is a changed link and set the regenerate flag if necessary. if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) { @@ -612,7 +622,7 @@ function xmlsitemap_link_save(array $link, array $context = array()) { // Save the link and allow other modules to respond to the link being saved. if ($existing) { - drupal_write_record('xmlsitemap', $link, array('type', 'id')); + drupal_write_record('xmlsitemap', $link, $link_keys); module_invoke_all('xmlsitemap_link_update', $link, $context); } else { @@ -1593,3 +1603,42 @@ function xmlsitemap_link_enqueue($type, $ids) { $queue = DrupalQueue::get('xmlsitemap_link_process'); $queue->createItem($data); } + +/** + * Determines the languages to process for an entity. + * + * If entity_translation is available the languages are determined by using the + * translation handler, otherwise it falls back to the default language. + * Only the language of published translations are returned. + * + * @param string $entity_type + * The type of entity. + * @param object $entity + * The entity. + * @param string $default_language + * Set the default language to use if no translations / entity languages where + * found. + * + * @return array + * Array with the language codes of available languages. + * Returns only languages of published translations. If entity isn't handled + * by entity translation the default language is used. + */ +function xmlsitemap_get_entity_languages($entity_type, $entity, $default_language = LANGUAGE_NONE) { + if (module_exists('entity_translation')) { + if (entity_translation_enabled($entity_type, $entity)) { + $handler = entity_translation_get_handler($entity_type, $entity); + if (!empty($handler)) { + $languages = array(); + $translations = $handler->getTranslations(); + foreach ($translations->data as $translation_lang => $translation_data) { + if (!empty($translation_data['status'])) { + $languages[$translation_lang] = $translation_lang; + } + } + return $languages; + } + } + } + return array($default_language); +} diff --git a/xmlsitemap_i18n/xmlsitemap_i18n.install b/xmlsitemap_i18n/xmlsitemap_i18n.install new file mode 100644 index 0000000..32e14b8 --- /dev/null +++ b/xmlsitemap_i18n/xmlsitemap_i18n.install @@ -0,0 +1,27 @@ + $language); + db_insert('xmlsitemap_sitemap') + ->fields(array( + 'smid' => xmlsitemap_sitemap_get_context_hash($context), + 'context' => serialize($context), + )) + ->execute(); + } + } +} + diff --git a/xmlsitemap_node/xmlsitemap_node.module b/xmlsitemap_node/xmlsitemap_node.module index 3d0e534..1da2086 100644 --- a/xmlsitemap_node/xmlsitemap_node.module +++ b/xmlsitemap_node/xmlsitemap_node.module @@ -40,7 +40,16 @@ function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) { $nodes = node_load_multiple($nids); foreach ($nodes as $node) { $link = xmlsitemap_node_create_link($node); - xmlsitemap_link_save($link, array($link['type'] => $node)); + if (is_array($link['language']) && !empty($link['language'])) { + foreach ($link['language'] as $lang) { + $lang_link = $link; + $lang_link['language'] = $lang; + xmlsitemap_link_save($lang_link, array($link['type'] => $node)); + } + } + else { + xmlsitemap_link_save($link, array($link['type'] => $node)); + } } } @@ -56,7 +65,16 @@ function xmlsitemap_node_node_insert(stdClass $node) { */ function xmlsitemap_node_node_update(stdClass $node) { $link = xmlsitemap_node_create_link($node); - xmlsitemap_link_save($link, array($link['type'] => $node)); + if (is_array($link['language']) && !empty($link['language'])) { + foreach ($link['language'] as $lang) { + $lang_link = $link; + $lang_link['language'] = $lang; + xmlsitemap_link_save($lang_link, array($link['type'] => $node)); + } + } + else { + xmlsitemap_link_save($link, array($link['type'] => $node)); + } } /** @@ -67,6 +85,20 @@ function xmlsitemap_node_node_delete(stdClass $node) { } /** + * Implements hook_entity_translation_delete(). + */ +function xmlsitemap_node_entity_translation_delete($entity_type, $entity, $langcode) { + if ($entity_type == 'node') { + $conditions = array( + 'type' => 'node', + 'id' => $entity->nid, + 'language' => $langcode, + ); + xmlsitemap_link_delete_multiple($conditions); + } +} + +/** * Implements hook_comment_update(). */ function xmlsitemap_node_comment_update(stdClass $comment) { @@ -204,7 +236,8 @@ function xmlsitemap_node_create_link(stdClass $node) { $node->xmlsitemap['loc'] = $uri['path']; $node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0; $node->xmlsitemap['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1; - $node->xmlsitemap['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE; + // Fetches the languages for the sitemap link / links. + $node->xmlsitemap['language'] = xmlsitemap_get_entity_languages('node', $node, isset($node->language) ? $node->language : LANGUAGE_NONE); return $node->xmlsitemap; } diff --git a/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module b/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module index 88f6a60..7c23d63 100644 --- a/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module +++ b/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module @@ -50,7 +50,16 @@ function xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links(array $tids) $terms = taxonomy_term_load_multiple($tids); foreach ($terms as $term) { $link = xmlsitemap_taxonomy_create_link($term); - xmlsitemap_link_save($link, array($link['type'] => $term)); + if (is_array($link['language']) && !empty($link['language'])) { + foreach ($link['language'] as $lang) { + $lang_link = $link; + $lang_link['language'] = $lang; + xmlsitemap_link_save($lang_link, array($link['type'] => $term)); + } + } + else { + xmlsitemap_link_save($link, array($link['type'] => $term)); + } } } @@ -115,7 +124,16 @@ function xmlsitemap_taxonomy_term_insert(stdClass $term) { */ function xmlsitemap_taxonomy_term_update(stdClass $term) { $link = xmlsitemap_taxonomy_create_link($term); - xmlsitemap_link_save($link, array($link['type'] => $term)); + if (is_array($link['language']) && !empty($link['language'])){ + foreach ($link['language'] as $lang){ + $lang_link = $link; + $lang_link['language'] = $lang; + xmlsitemap_link_save($lang_link, array($link['type'] => $term)); + } + } + else { + xmlsitemap_link_save($link, array($link['type'] => $term)); + } } /** @@ -126,6 +144,21 @@ function xmlsitemap_taxonomy_term_delete(stdClass $term) { } /** + * Implements hook_entity_translation_delete(). + */ +function xmlsitemap_taxonomy_entity_translation_delete($entity_type, $entity, $langcode) { + if ($entity_type == 'taxonomy_term') { + $conditions = array( + 'type' => $entity_type, + 'id' => $entity->tid, + 'language' => $langcode, + ); + xmlsitemap_link_delete_multiple($conditions); + } +} + + +/** * Implements hook_field_extra_fields(). */ function xmlsitemap_taxonomy_field_extra_fields() { @@ -175,7 +208,8 @@ function xmlsitemap_taxonomy_create_link(stdClass &$term) { // @todo How can/should we check taxonomy term access? $term->xmlsitemap['loc'] = $uri['path']; $term->xmlsitemap['access'] = 1; - $term->xmlsitemap['language'] = isset($term->language) ? $term->language : LANGUAGE_NONE; + // Fetches the languages for the sitemap link / links. + $term->xmlsitemap['language'] = xmlsitemap_get_entity_languages('taxonomy_term', $term, isset($term->language) ? $term->language : LANGUAGE_NONE); return $term->xmlsitemap; } -- 2.1.4