TestSelection.php in Drupal 10
File
core/modules/block_content/tests/modules/block_content_test/src/Plugin/EntityReferenceSelection/TestSelection.php
View source
<?php
namespace Drupal\block_content_test\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
class TestSelection extends DefaultSelection {
protected $conditionType;
protected $isReusable;
public function setTestMode($condition_type = NULL, $is_reusable = NULL) {
$this->conditionType = $condition_type;
$this->isReusable = $is_reusable;
}
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
if ($this->conditionType) {
$add_condition = NULL;
switch ($this->conditionType) {
case 'base':
$add_condition = $query;
break;
case 'group':
$group = $query
->andConditionGroup()
->exists('type');
$add_condition = $group;
$query
->condition($group);
break;
case "nested_group":
$query
->exists('type');
$sub_group = $query
->andConditionGroup()
->exists('type');
$add_condition = $sub_group;
$group = $query
->andConditionGroup()
->exists('type')
->condition($sub_group);
$query
->condition($group);
break;
}
if ($this->isReusable) {
$add_condition
->condition('reusable', 1);
}
else {
$add_condition
->condition('reusable', 0);
}
}
return $query;
}
}
Classes
Name |
Description |
TestSelection |
Test EntityReferenceSelection with conditions on the 'reusable' field. |