callback_language_control.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Search API data alteration callback that filters out items based on their
  4. * bundle.
  5. */
  6. class SearchApiAlterLanguageControl 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['languages'] = array(
  82. '#type' => 'checkboxes',
  83. '#title' => t('Indexed languages'),
  84. '#description' => t('Index only items in the selected languages. ' .
  85. 'When no language is selected, there will be no language-related restrictions.'),
  86. '#options' => $languages,
  87. '#default_value' => $this->options['languages'],
  88. );
  89. return $form;
  90. }
  91. /**
  92. * Submit callback for the form returned by configurationForm().
  93. *
  94. * This method should both return the new options and set them internally.
  95. *
  96. * @param array $form
  97. * The form returned by configurationForm().
  98. * @param array $values
  99. * The part of the $form_state['values'] array corresponding to this form.
  100. * @param array $form_state
  101. * The complete form state.
  102. *
  103. * @return array
  104. * The new options array for this callback.
  105. */
  106. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  107. $values['languages'] = array_filter($values['languages']);
  108. return parent::configurationFormSubmit($form, $values, $form_state);
  109. }
  110. /**
  111. * Alter items before indexing.
  112. *
  113. * Items which are removed from the array won't be indexed, but will be marked
  114. * as clean for future indexing. This could for instance be used to implement
  115. * some sort of access filter for security purposes (e.g., don't index
  116. * unpublished nodes or comments).
  117. *
  118. * @param array $items
  119. * An array of items to be altered, keyed by item IDs.
  120. */
  121. public function alterItems(array &$items) {
  122. foreach ($items as $i => &$item) {
  123. // Set item language, if a custom field was selected.
  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. // Filter out items according to language, if any were selected.
  137. if ($languages = $this->options['languages']) {
  138. if (empty($languages[$item->search_api_language])) {
  139. unset($items[$i]);
  140. }
  141. }
  142. }
  143. }
  144. }