callback_role_filter.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiAlterRoleFilter class.
  5. */
  6. /**
  7. * Data alteration that filters out users based on their role.
  8. */
  9. class SearchApiAlterRoleFilter extends SearchApiAbstractAlterCallback {
  10. /**
  11. * Overrides SearchApiAbstractAlterCallback::supportsIndex().
  12. *
  13. * This plugin only supports indexes containing users.
  14. */
  15. public function supportsIndex(SearchApiIndex $index) {
  16. return $index->getEntityType() == 'user';
  17. }
  18. /**
  19. * Implements SearchApiAlterCallbackInterface::alterItems().
  20. */
  21. public function alterItems(array &$items) {
  22. $roles = $this->options['roles'];
  23. $default = (bool) $this->options['default'];
  24. foreach ($items as $id => $account) {
  25. $role_match = (count(array_diff_key($account->roles, $roles)) !== count($account->roles));
  26. if ($role_match === $default) {
  27. unset($items[$id]);
  28. }
  29. }
  30. }
  31. /**
  32. * Overrides SearchApiAbstractAlterCallback::configurationForm().
  33. *
  34. * Add option for the roles to include/exclude.
  35. */
  36. public function configurationForm() {
  37. $options = array_map('check_plain', user_roles());
  38. $form = array(
  39. 'default' => array(
  40. '#type' => 'radios',
  41. '#title' => t('Which users should be indexed?'),
  42. '#default_value' => isset($this->options['default']) ? $this->options['default'] : 1,
  43. '#options' => array(
  44. 1 => t('All but those from one of the selected roles'),
  45. 0 => t('Only those from the selected roles'),
  46. ),
  47. ),
  48. 'roles' => array(
  49. '#type' => 'select',
  50. '#title' => t('Roles'),
  51. '#default_value' => isset($this->options['roles']) ? $this->options['roles'] : array(),
  52. '#options' => $options,
  53. '#size' => min(4, count($options)),
  54. '#multiple' => TRUE,
  55. ),
  56. );
  57. return $form;
  58. }
  59. }