diff --git a/xmlsitemap.module b/xmlsitemap.module index 4f05213..118f0e0 100644 --- a/xmlsitemap.module +++ b/xmlsitemap.module @@ -602,8 +602,10 @@ function xmlsitemap_link_save(array $link, array $context = array()) { trigger_error(t('Negative changecount value. Please report this to @516928.
@link', array('@516928' => 'http://drupal.org/node/516928', '@link' => var_export($link, TRUE))), E_USER_ERROR); $link['changecount'] = 0; } - - if (module_exists('entity_translation') && !empty($link['language']) ) { + + // 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') && !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(); } else { @@ -617,7 +619,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, array('type', 'id', 'language')); module_invoke_all('xmlsitemap_link_update', $link, $context); } else { @@ -1598,3 +1600,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_node/xmlsitemap_node.module b/xmlsitemap_node/xmlsitemap_node.module index 2d41f3d..ef83ee2 100644 --- a/xmlsitemap_node/xmlsitemap_node.module +++ b/xmlsitemap_node/xmlsitemap_node.module @@ -40,7 +40,7 @@ function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) { $nodes = node_load_multiple($nids); foreach ($nodes as $node) { $link = xmlsitemap_node_create_link($node); - if (module_exists('entity_translation') && is_array($link['language']) && !empty($link['language'])) { + if (is_array($link['language']) && !empty($link['language'])) { foreach ($link['language'] as $lang) { $lang_link = $link; $lang_link['language'] = $lang; @@ -65,7 +65,7 @@ function xmlsitemap_node_node_insert(stdClass $node) { */ function xmlsitemap_node_node_update(stdClass $node) { $link = xmlsitemap_node_create_link($node); - if (module_exists('entity_translation') && is_array($link['language']) && !empty($link['language'])){ + if (is_array($link['language']) && !empty($link['language'])) { foreach ($link['language'] as $lang) { $lang_link = $link; $lang_link['language'] = $lang; @@ -222,12 +222,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; - if (module_exists('entity_translation') && isset($node->translations)) { - $node->xmlsitemap['language'] = array_keys($node->translations->data); - } - else { - $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 e8973fc..8bc7b43 100644 --- a/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module +++ b/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module @@ -50,11 +50,11 @@ 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); - if (module_exists('entity_translation') && 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)); + 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 { @@ -124,7 +124,7 @@ function xmlsitemap_taxonomy_term_insert(stdClass $term) { */ function xmlsitemap_taxonomy_term_update(stdClass $term) { $link = xmlsitemap_taxonomy_create_link($term); - if (module_exists('entity_translation') && is_array($link['language']) && !empty($link['language'])){ + if (is_array($link['language']) && !empty($link['language'])){ foreach ($link['language'] as $lang){ $lang_link = $link; $lang_link['language'] = $lang; @@ -193,14 +193,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; - if (module_exists('entity_translation') && isset($term->translations)) { - $languages = array_keys($term->translations->data); - $term->xmlsitemap['language'] = !empty($languages) ? $languages : (isset($term->language) ? $term->language : LANGUAGE_NONE); - } - else{ - $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; }