3 namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection;
5 use Drupal\Component\Utility\Html;
8 * Defines an alternative to the default Entity Reference Selection plugin.
10 * This selection plugin uses PHP for more advanced cases when the entity query
11 * cannot filter properly, for example when the target entity type has no
12 * 'label' key provided in the entity type plugin definition.
14 * @see \Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver
16 class PhpSelection extends DefaultSelection {
21 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
22 // No input, return everything from the entity query.
23 if ($match === NULL || $match === '') {
24 return parent::getReferenceableEntities($match, $match_operator, $limit);
27 // Start with the selection results returned by the entity query. Don't use
28 // any limit because we have to apply a limit after filtering the items.
29 $options = parent::getReferenceableEntities($match, $match_operator);
31 // Always use a case-insensitive, escaped match. Entity labels returned by
32 // SelectionInterface::getReferenceableEntities() are already escaped, so
33 // the incoming $match needs to be escaped as well, making the comparison
35 // @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::getReferenceableEntities()
36 if (is_string($match)) {
37 $match = Html::escape(mb_strtolower($match));
39 elseif (is_array($match)) {
40 array_walk($match, function (&$item) {
41 $item = Html::escape(mb_strtolower($item));
47 // Filter target entities by the output of their label() method.
48 foreach ($options as $bundle => &$items) {
49 foreach ($items as $entity_id => $label) {
50 if ($this->matchLabel($match, $match_operator, $label)) {
51 $filtered[$bundle][$entity_id] = $label;
54 if ($limit && $count >= $limit) {
67 public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
69 foreach ($this->getReferenceableEntities($match, $match_operator) as &$items) {
70 $count += count($items);
77 * Matches an entity label to an input string.
80 * The value to compare. This can be any valid entity query condition value.
81 * @param string $match_operator
82 * The comparison operator.
83 * @param string $label
84 * The entity label to match against.
87 * TRUE when matches, FALSE otherwise.
89 protected function matchLabel($match, $match_operator, $label) {
90 // Always use a case-insensitive value.
91 $label = mb_strtolower($label);
93 switch ($match_operator) {
95 return $label == $match;
97 return $label > $match;
99 return $label < $match;
101 return $label >= $match;
103 return $label <= $match;
105 return $label != $match;
107 return array_search($label, $match) !== FALSE;
109 return array_search($label, $match) === FALSE;
111 return strpos($label, $match) === 0;
113 return strpos($label, $match) !== FALSE;
115 return mb_substr($label, -mb_strlen($match)) === (string) $match;
121 // Invalid match operator.