SearchAPIBaseLanguage.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Search API data alteration callback that filters out items based on their
  4. * bundle.
  5. */
  6. class SearchAPIBaseLanguage extends SearchApiAbstractAlterCallback {
  7. /**
  8. * Construct a data-alter callback.
  9. *
  10. * @param SearchApiIndex $index
  11. * The index whose items will be altered.
  12. * @param array $options
  13. * The callback options set for this index.
  14. */
  15. public function __construct(SearchApiIndex $index, array $options = array()) {
  16. $options += array(
  17. 'lang_field' => '',
  18. 'languages' => array(),
  19. );
  20. parent::__construct($index, $options);
  21. }
  22. /**
  23. * Check whether this data-alter callback is applicable for a certain index.
  24. *
  25. * Only returns TRUE if the system is multilingual.
  26. *
  27. * @param SearchApiIndex $index
  28. * The index to check for.
  29. *
  30. * @return boolean
  31. * TRUE if the callback can run on the given index; FALSE otherwise.
  32. *
  33. * @see drupal_multilingual()
  34. */
  35. public function supportsIndex(SearchApiIndex $index) {
  36. return drupal_multilingual();
  37. }
  38. /**
  39. * Display a form for configuring this data alteration.
  40. *
  41. * @return array
  42. * A form array for configuring this data alteration.
  43. */
  44. public function configurationForm() {
  45. $form = array();
  46. $wrapper = $this->index->entityWrapper();
  47. $fields[''] = t('- Use default -');
  48. foreach ($wrapper as $key => $property) {
  49. if ($key == 'search_api_language') {
  50. continue;
  51. }
  52. $type = $property->type();
  53. // Only single-valued string properties make sense here. Also, nested
  54. // properties probably don't make sense.
  55. if ($type == 'text' || $type == 'token') {
  56. $info = $property->info();
  57. $fields[$key] = $info['label'];
  58. }
  59. }
  60. if (count($fields) > 1) {
  61. $form['lang_field'] = array(
  62. '#type' => 'select',
  63. '#title' => t('Language field'),
  64. '#description' => t("Select the field which should be used to determine an item's language."),
  65. '#options' => $fields,
  66. '#default_value' => $this->options['lang_field'],
  67. );
  68. }
  69. $languages[LANGUAGE_NONE] = t('Language neutral');
  70. $list = language_list('enabled') + array(array(), array());
  71. foreach (array($list[1], $list[0]) as $list) {
  72. foreach ($list as $lang) {
  73. $name = t($lang->name);
  74. $native = $lang->native;
  75. $languages[$lang->language] = ($name == $native) ? $name : "$name ($native)";
  76. if (!$lang->enabled) {
  77. $languages[$lang->language] .= ' [' . t('disabled') . ']';
  78. }
  79. }
  80. }
  81. $form['language'] = array(
  82. '#type' => 'radios',
  83. '#title' => t('Index language'),
  84. '#description' => t('Index items in this language.'),
  85. '#options' => $languages,
  86. '#default_value' => $this->options['language'],
  87. );
  88. return $form;
  89. }
  90. /**
  91. * Submit callback for the form returned by configurationForm().
  92. *
  93. * This method should both return the new options and set them internally.
  94. *
  95. * @param array $form
  96. * The form returned by configurationForm().
  97. * @param array $values
  98. * The part of the $form_state['values'] array corresponding to this form.
  99. * @param array $form_state
  100. * The complete form state.
  101. *
  102. * @return array
  103. * The new options array for this callback.
  104. */
  105. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  106. $values['language'] = $values['language'];
  107. return parent::configurationFormSubmit($form, $values, $form_state);
  108. }
  109. /**
  110. * Alter items before indexing.
  111. *
  112. * Items which are removed from the array won't be indexed, but will be marked
  113. * as clean for future indexing. This could for instance be used to implement
  114. * some sort of access filter for security purposes (e.g., don't index
  115. * unpublished nodes or comments).
  116. *
  117. * @param array $items
  118. * An array of items to be altered, keyed by item IDs.
  119. */
  120. public function alterItems(array &$items) {
  121. foreach ($items as $i => &$item) {
  122. $handler = entity_translation_get_handler('node', $item);
  123. $translations = array_keys($handler->getTranslations()->data);
  124. if ($field = $this->options['lang_field']) {
  125. $wrapper = $this->index->entityWrapper($item);
  126. if (isset($wrapper->$field)) {
  127. try {
  128. $item->search_api_language = $wrapper->$field->value();
  129. }
  130. catch (EntityMetadataWrapperException $e) {
  131. // Something went wrong while accessing the language field. Probably
  132. // doesn't really matter.
  133. }
  134. }
  135. }
  136. if($item->$field != $this->options['language'] && !in_array($this->options['language'], $translations)){
  137. unset($items[$i]);
  138. }
  139. else {
  140. $item->search_api_language = $this->options['language'];
  141. $item->$field = $this->options['language'];
  142. }
  143. }
  144. }
  145. }