callback_bundle_filter.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiAlterBundleFilter.
  5. */
  6. /**
  7. * Represents a data alteration that restricts entity indexes to some bundles.
  8. */
  9. class SearchApiAlterBundleFilter extends SearchApiAbstractAlterCallback {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function supportsIndex(SearchApiIndex $index) {
  14. if ($this->isMultiEntityIndex($index)) {
  15. $info = entity_get_info();
  16. foreach ($index->options['datasource']['types'] as $type) {
  17. if (isset($info[$type]) && self::hasBundles($info[$type])) {
  18. return TRUE;
  19. }
  20. }
  21. return FALSE;
  22. }
  23. return $index->getEntityType() && ($info = entity_get_info($index->getEntityType())) && self::hasBundles($info);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function alterItems(array &$items) {
  29. if (!$this->supportsIndex($this->index) || !isset($this->options['bundles'])) {
  30. return;
  31. }
  32. $multi_entity = $this->isMultiEntityIndex();
  33. if ($multi_entity) {
  34. $bundle_prop = 'item_bundle';
  35. }
  36. else {
  37. $info = entity_get_info($this->index->getEntityType());
  38. $bundle_prop = $info['entity keys']['bundle'];
  39. }
  40. $bundles = array_flip($this->options['bundles']);
  41. $default = (bool) $this->options['default'];
  42. foreach ($items as $id => $item) {
  43. // Ignore types that have no bundles.
  44. if ($multi_entity && !self::hasBundles(entity_get_info($item->item_type))) {
  45. continue;
  46. }
  47. if (isset($bundles[$item->$bundle_prop]) == $default) {
  48. unset($items[$id]);
  49. }
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function configurationForm() {
  56. if ($this->supportsIndex($this->index)) {
  57. $options = array();
  58. if ($this->isMultiEntityIndex()) {
  59. $info = entity_get_info();
  60. $unsupported_types = array();
  61. foreach ($this->index->options['datasource']['types'] as $type) {
  62. if (isset($info[$type]) && self::hasBundles($info[$type])) {
  63. foreach ($info[$type]['bundles'] as $bundle => $bundle_info) {
  64. $options["$type:$bundle"] = $info[$type]['label'] . ' » ' . $bundle_info['label'];
  65. }
  66. }
  67. else {
  68. $unsupported_types[] = isset($info[$type]['label']) ? $info[$type]['label'] : $type;
  69. }
  70. }
  71. if ($unsupported_types) {
  72. $form['unsupported_types']['#markup'] = '<p>' . t('The following entity types do not contain any bundles: @types. All items of those types will therefore be included in the index.', array('@types' => implode(', ', $unsupported_types))) . '</p>';
  73. }
  74. }
  75. else {
  76. $info = entity_get_info($this->index->getEntityType());
  77. foreach ($info['bundles'] as $bundle => $bundle_info) {
  78. $options[$bundle] = isset($bundle_info['label']) ? $bundle_info['label'] : $bundle;
  79. }
  80. }
  81. if (!empty($this->index->options['datasource']['bundles'])) {
  82. $form['message']['#markup'] = '<p>' . t("<strong>Note:</strong> This index is already restricted to certain bundles. If you use this data alteration, those will be reduced further. However, the index setting is better supported in the user interface and should therefore be prefered. For example, using this data alteration will not reduce the displayed total number of items to index (even though some of them will not be indexed). Consider creating a new index with appropriate bundle settings instead.") . '</p>';
  83. $included_bundles = array_flip($this->index->options['datasource']['bundles']);
  84. $options = array_intersect_key($options, $included_bundles);
  85. }
  86. $form['default'] = array(
  87. '#type' => 'radios',
  88. '#title' => t('Which items should be indexed?'),
  89. '#default_value' => isset($this->options['default']) ? $this->options['default'] : 1,
  90. '#options' => array(
  91. 1 => t('All but those from one of the selected bundles'),
  92. 0 => t('Only those from the selected bundles'),
  93. ),
  94. );
  95. $form['bundles'] = array(
  96. '#type' => 'select',
  97. '#title' => t('Bundles'),
  98. '#default_value' => isset($this->options['bundles']) ? $this->options['bundles'] : array(),
  99. '#options' => $options,
  100. '#size' => min(4, count($options)),
  101. '#multiple' => TRUE,
  102. );
  103. }
  104. else {
  105. $form = array(
  106. 'forbidden' => array(
  107. '#markup' => '<p>' . t("Items indexed by this index don't have bundles and therefore cannot be filtered here.") . '</p>',
  108. ),
  109. );
  110. }
  111. return $form;
  112. }
  113. /**
  114. * Determines whether a certain entity type has any bundles.
  115. *
  116. * @param array $entity_info
  117. * The entity type's entity_get_info() array.
  118. *
  119. * @return bool
  120. * TRUE if the entity type has bundles, FALSE otherwise.
  121. */
  122. protected static function hasBundles(array $entity_info) {
  123. return !empty($entity_info['entity keys']['bundle']) && !empty($entity_info['bundles']);
  124. }
  125. }