PhpSelection.php in Zircon Profile 8
File
core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/PhpSelection.php
View source
<?php
namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
class PhpSelection extends DefaultSelection {
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
if ($match === NULL || $match === '') {
return parent::getReferenceableEntities($match, $match_operator, $limit);
}
$options = parent::getReferenceableEntities($match, $match_operator);
if (is_string($match)) {
$match = Html::escape(Unicode::strtolower($match));
}
elseif (is_array($match)) {
array_walk($match, function (&$item) {
$item = Html::escape(Unicode::strtolower($item));
});
}
$filtered = [];
$count = 0;
foreach ($options as $bundle => &$items) {
foreach ($items as $entity_id => $label) {
if ($this
->matchLabel($match, $match_operator, $label)) {
$filtered[$bundle][$entity_id] = $label;
$count++;
if ($limit && $count >= $limit) {
break 2;
}
}
}
}
return $filtered;
}
public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
$count = 0;
foreach ($this
->getReferenceableEntities($match, $match_operator) as &$items) {
$count += count($items);
}
return $count;
}
protected function matchLabel($match, $match_operator, $label) {
$label = Unicode::strtolower($label);
switch ($match_operator) {
case '=':
return $label == $match;
case '>':
return $label > $match;
case '<':
return $label < $match;
case '>=':
return $label >= $match;
case '<=':
return $label <= $match;
case '<>':
return $label != $match;
case 'IN':
return array_search($label, $match) !== FALSE;
case 'NOT IN':
return array_search($label, $match) === FALSE;
case 'STARTS_WITH':
return strpos($label, $match) === 0;
case 'CONTAINS':
return strpos($label, $match) !== FALSE;
case 'ENDS_WITH':
return Unicode::substr($label, -Unicode::strlen($match)) === (string) $match;
case 'IS NOT NULL':
return TRUE;
case 'IS NULL':
return FALSE;
default:
return FALSE;
}
}
}
Classes
Name |
Description |
PhpSelection |
Defines an alternative to the default Entity Reference Selection plugin. |