callback_language_control.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiAlterLanguageControl.
  5. */
  6. /**
  7. * Search API data alteration callback that filters out items based on their
  8. * bundle.
  9. */
  10. class SearchApiAlterLanguageControl extends SearchApiAbstractAlterCallback {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function __construct(SearchApiIndex $index, array $options = array()) {
  15. $options += array(
  16. 'lang_field' => '',
  17. 'languages' => array(),
  18. );
  19. parent::__construct($index, $options);
  20. }
  21. /**
  22. * Overrides SearchApiAbstractAlterCallback::supportsIndex().
  23. *
  24. * Only returns TRUE if the system is multilingual.
  25. *
  26. * @see drupal_multilingual()
  27. */
  28. public function supportsIndex(SearchApiIndex $index) {
  29. return drupal_multilingual();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function configurationForm() {
  35. $form = array();
  36. $wrapper = $this->index->entityWrapper();
  37. $fields[''] = t('- Use default -');
  38. foreach ($wrapper as $key => $property) {
  39. if ($key == 'search_api_language') {
  40. continue;
  41. }
  42. $type = $property->type();
  43. // Only single-valued string properties make sense here. Also, nested
  44. // properties probably don't make sense.
  45. if ($type == 'text' || $type == 'token') {
  46. $info = $property->info();
  47. $fields[$key] = $info['label'];
  48. }
  49. }
  50. if (count($fields) > 1) {
  51. $form['lang_field'] = array(
  52. '#type' => 'select',
  53. '#title' => t('Language field'),
  54. '#description' => t("Select the field which should be used to determine an item's language."),
  55. '#options' => $fields,
  56. '#default_value' => $this->options['lang_field'],
  57. );
  58. }
  59. $languages[LANGUAGE_NONE] = t('Language neutral');
  60. $list = language_list('enabled') + array(array(), array());
  61. foreach (array($list[1], $list[0]) as $list) {
  62. foreach ($list as $lang) {
  63. $name = t($lang->name);
  64. $native = $lang->native;
  65. $languages[$lang->language] = check_plain(($name == $native) ? $name : "$name ($native)");
  66. if (!$lang->enabled) {
  67. $languages[$lang->language] .= ' [' . t('disabled') . ']';
  68. }
  69. }
  70. }
  71. $form['languages'] = array(
  72. '#type' => 'checkboxes',
  73. '#title' => t('Indexed languages'),
  74. '#description' => t('Index only items in the selected languages. ' .
  75. 'When no language is selected, there will be no language-related restrictions.'),
  76. '#options' => $languages,
  77. '#default_value' => $this->options['languages'],
  78. );
  79. return $form;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  85. $values['languages'] = array_filter($values['languages']);
  86. return parent::configurationFormSubmit($form, $values, $form_state);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function alterItems(array &$items) {
  92. foreach ($items as $i => &$item) {
  93. // Set item language, if a custom field was selected.
  94. if ($field = $this->options['lang_field']) {
  95. $wrapper = $this->index->entityWrapper($item);
  96. if (isset($wrapper->$field)) {
  97. try {
  98. $item->search_api_language = $wrapper->$field->value();
  99. }
  100. catch (EntityMetadataWrapperException $e) {
  101. // Something went wrong while accessing the language field. Probably
  102. // doesn't really matter.
  103. }
  104. }
  105. }
  106. // Filter out items according to language, if any were selected.
  107. if ($languages = $this->options['languages']) {
  108. if (empty($languages[$item->search_api_language])) {
  109. unset($items[$i]);
  110. }
  111. }
  112. }
  113. }
  114. }