RoleFilter.php in Search API 8
File
src/Plugin/search_api/processor/RoleFilter.php
View source
<?php
namespace Drupal\search_api\Plugin\search_api\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Utility\Utility;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
class RoleFilter extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
public static function supportsIndex(IndexInterface $index) {
foreach ($index
->getDatasources() as $datasource) {
if ($datasource
->getEntityTypeId() == 'user') {
return TRUE;
}
}
return FALSE;
}
public function defaultConfiguration() {
return [
'default' => TRUE,
'roles' => [],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = array_map(function (RoleInterface $role) {
return Utility::escapeHtml($role
->label());
}, user_roles());
$form['default'] = [
'#type' => 'radios',
'#title' => $this
->t('Which users should be indexed?'),
'#options' => [
1 => $this
->t('All but those from one of the selected roles'),
0 => $this
->t('Only those from the selected roles'),
],
'#default_value' => (int) $this->configuration['default'],
];
$form['roles'] = [
'#type' => 'select',
'#title' => $this
->t('Roles'),
'#options' => $options,
'#multiple' => TRUE,
'#size' => min(4, count($options)),
'#default_value' => array_combine($this->configuration['roles'], $this->configuration['roles']),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$values['default'] = (bool) $values['default'];
$values['roles'] = array_values(array_filter($values['roles']));
$form_state
->set('values', $values);
$this
->setConfiguration($values);
}
public function alterIndexedItems(array &$items) {
$selected_roles = array_combine($this->configuration['roles'], $this->configuration['roles']);
$default = (bool) $this->configuration['default'];
foreach ($items as $item_id => $item) {
$account = $item
->getOriginalObject()
->getValue();
if (!$account instanceof UserInterface) {
continue;
}
$account_roles = $account
->getRoles();
$account_roles = array_flip($account_roles);
$has_some_roles = (bool) array_intersect_key($account_roles, $selected_roles);
if ($default == $has_some_roles) {
unset($items[$item_id]);
}
}
}
}