1$form['my_element'] = [
2 '#type' => 'entity_autocomplete',
3 '#target_type' => 'node',
4 '#selection_handler' => 'default:node_by_field',
5 '#selection_settings' => [
6 'target_bundles' => ['article'],
7 'filter' => ['field_dummy_filter' => $dummy_field_value],
8 ],
9];
10
1<?php
2
3namespace Drupal\test_module_name\Plugin\EntityReferenceSelection;
4
5use Drupal\node\Plugin\EntityReferenceSelection\NodeSelection;
6
7/**
8 * Provides specific access control for the node entity type.
9 *
10 * @EntityReferenceSelection(
11 * id = "default:node_by_field",
12 * label = @Translation("Node by field selection"),
13 * entity_types = {"node"},
14 * group = "default",
15 * weight = 3
16 * )
17 */
18class NodeByFieldSelection extends NodeSelection {
19
20 /**
21 * {@inheritdoc}
22 */
23 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
24 $query = parent::buildEntityQuery($match, $match_operator);
25 $handler_settings = $this->configuration['handler_settings'];
26 if (!isset($handler_settings['filter'])) {
27 return $query;
28 }
29 $filter_settings = $handler_settings['filter'];
30 foreach ($filter_settings as $field_name => $value) {
31 $query->condition($field_name, $value, '=');
32 }
33 return $query;
34 }
35
36}
37