Add a custom callback to search_api (eg. to exclude certain nodes from indexing)

Public

Implement hook_search_api_alter_callback().

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_search_api_alter_callback().
  3.  */
  4. function MYMODULE_search_api_alter_callback_info() {
  5. $callbacks['callback_nid_filter'] = array(
  6. 'name' => t('NID filter'),
  7. 'description' => t('Exclude NIDs from being indexed.'),
  8. 'class' => 'SearchApiAlterNIDFilter',
  9. 'weight' => 100,
  10. );
  11. return $callbacks;
  12. }

Implement the Callback Class

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Contains SearchApiAlterNIDFilter.
  6.  */
  7.  
  8. /**
  9.  * Represents a data alteration that skip NIDs from indexing.
  10.  */
  11. class SearchApiAlterNIDFilter extends SearchApiAbstractAlterCallback {
  12.  
  13. /**
  14.   * {@inheritdoc}
  15.   */
  16. public function supportsIndex(SearchApiIndex $index) {
  17. return $index->getEntityType() === 'node';
  18. }
  19.  
  20. public function configurationForm() {
  21. $form['nids'] = array(
  22. '#type' => 'textfield',
  23. '#title' => 'Node IDs',
  24. '#description' => 'Specify which nodes should not be indexed.',
  25. '#default_value' => isset($this->options['nids']) ? $this->options['nids'] : '',
  26. );
  27.  
  28. return $form;
  29. }
  30.  
  31. /**
  32.   * {@inheritdoc}
  33.   */
  34. public function alterItems(array &$items) {
  35. $nids = drupal_explode_tags($this->options['nids']);
  36.  
  37. foreach($nids as $nid) {
  38. if (isset($items[$nid])) {
  39. unset($items[$nid]);
  40. }
  41. }
  42.  
  43. }
  44.  
  45. }

Add file the .info file

</> CopyGet raw version
text
  1. files[] = includes/callback_nid_filter.inc