my_module.schema.yml
views.filter.my_module_entity_options:
type: views.filter.many_to_one
label: 'Entity options'EntityOptions.php
<?php
namespace Drupal\my_module\Plugin\views\filter;
use Drupal\views\Attribute\ViewsFilter;
use Drupal\views\Plugin\views\filter\ManyToOne;
/**
* Filters a multi-value field by entities of a configurable entity type.
*/
#[ViewsFilter("my_module_entity_options")]
class EntityOptions extends ManyToOne {
/**
* {@inheritdoc}
*/
public function getValueOptions() {
if (!isset($this->valueOptions)) {
$this->valueOptions = [];
$entity_type_id = $this->definition['options entity type'] ?? NULL;
if ($entity_type_id) {
$entities = \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->loadMultiple();
foreach ($entities as $id => $entity) {
$this->valueOptions[$id] = $entity->label();
}
}
}
return $this->valueOptions;
}
}MyModuleViewsHooks.php
<?php
namespace Drupal\my_module\Hook;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Views hook implementations for my_module.
*/
class MyModuleViewsHooks {
/**
* Implements hook_views_data_alter().
*/
#[Hook('views_data_alter')]
public function viewsDataAlter(array &$data): void {
// Node field storing user role IDs.
$data['node__field_roles']['field_roles_value']['filter']['id'] = 'my_module_entity_options';
// Entity type whose entities are offered as filter options.
// Change it to reuse the filter, e.g. 'profile_type' or 'group_type'.
$data['node__field_roles']['field_roles_value']['filter']['options entity type'] = 'user_role';
}
}
05.11.2026 | Michael Ebert