xmlsitemap_i18n.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Main file for XML sitemap i18n.
  5. */
  6. /**
  7. * Implements hook_xmlsitemap_context_info().
  8. */
  9. function xmlsitemap_i18n_xmlsitemap_context_info() {
  10. $context['language'] = array(
  11. 'label' => t('Language'),
  12. 'summary callback' => 'locale_language_name',
  13. 'default' => language_default('language'),
  14. );
  15. return $context;
  16. }
  17. /**
  18. * Implements hook_xmlsitemap_context().
  19. */
  20. function xmlsitemap_i18n_xmlsitemap_context() {
  21. $context['language'] = $GLOBALS['language']->language;
  22. return $context;
  23. }
  24. /**
  25. * Implements xmlsitemap_context_url_options().
  26. */
  27. function xmlsitemap_i18n_xmlsitemap_context_url_options(array $context) {
  28. $options = array();
  29. if (isset($context['language'])) {
  30. $options['language'] = xmlsitemap_language_load($context['language']);
  31. }
  32. return $options;
  33. }
  34. /**
  35. * Implements hook_form_FORM_ID_alter().
  36. */
  37. function xmlsitemap_i18n_form_xmlsitemap_sitemap_edit_form_alter(&$form, $form_state) {
  38. $form['context']['language'] = array(
  39. '#type' => 'select',
  40. '#title' => t('Language'),
  41. '#options' => locale_language_list(),
  42. '#default_value' => isset($form['#sitemap']->context['language']) ? $form['#sitemap']->context['language'] : LANGUAGE_NONE,
  43. );
  44. }
  45. /**
  46. * Implements hook_form_FORM_ID_alter().
  47. *
  48. * Set the regeneration needed flag if settings are changed.
  49. */
  50. function xmlsitemap_form_locale_languages_overview_form_alter(&$form, $form_state) {
  51. array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
  52. }
  53. /**
  54. * Implements hook_form_FORM_ID_alter().
  55. *
  56. * Set the regeneration needed flag if multilingual settings are changed.
  57. */
  58. function xmlsitemap_i18n_form_i18n_admin_settings_alter(&$form, $form_state) {
  59. array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
  60. }
  61. /**
  62. * Implements hook_query_TAG_alter().
  63. *
  64. * @see i18n_db_rewrite_where()
  65. */
  66. function xmlsitemap_i18n_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
  67. $mode = variable_get('i18n_selection_mode', 'simple');
  68. $sitemap = $query->getMetaData('sitemap');
  69. if (!isset($sitemap->context['language']) || $mode == 'off') {
  70. return;
  71. }
  72. // Get languages to simplify query building.
  73. $current = $sitemap->context['language'];
  74. $default = language_default();
  75. if ($mode == 'mixed' && $current == $default) {
  76. // If mode is mixed but current = default, is the same as 'simple'.
  77. $mode = 'simple';
  78. }
  79. switch ($mode) {
  80. case 'simple':
  81. // Current language and language neutral.
  82. $query->condition('x.language', array($current, LANGUAGE_NONE));
  83. break;
  84. case 'mixed':
  85. // Mixed current language (if available) or default language (if not) and
  86. // language neutral.
  87. $query->condition('x.language', array($current, $default, LANGUAGE_NONE));
  88. break;
  89. case 'default':
  90. // Only default language and language neutral.
  91. $query->condition('x.language', array($default, LANGUAGE_NONE));
  92. break;
  93. case 'strict':
  94. // Only current language (for nodes), simple for all other types.
  95. $node_condition = db_and();
  96. $node_condition->condition('x.type', 'node');
  97. $node_condition->condition('x.language', $current);
  98. $normal_condition = db_and();
  99. $normal_condition->condition('x.type', 'node', '<>');
  100. $normal_condition->condition('x.language', array($current, LANGUAGE_NONE));
  101. $condition = db_or();
  102. $condition->condition($node_condition);
  103. $condition->condition($normal_condition);
  104. $query->condition($condition);
  105. break;
  106. case 'off':
  107. // All content. No language conditions apply.
  108. break;
  109. }
  110. }