3 namespace Drupal\block_content_test\Plugin\EntityReferenceSelection;
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
8 * Test EntityReferenceSelection with conditions on the 'reusable' field.
10 class TestSelection extends DefaultSelection {
17 protected $conditionType;
20 * Whether to set the condition for reusable or non-reusable blocks.
24 protected $isReusable;
29 * @param string $condition_type
31 * @param bool $is_reusable
32 * Whether to set the condition for reusable or non-reusable blocks.
34 public function setTestMode($condition_type = NULL, $is_reusable = NULL) {
35 $this->conditionType = $condition_type;
36 $this->isReusable = $is_reusable;
42 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
43 $query = parent::buildEntityQuery($match, $match_operator);
44 if ($this->conditionType) {
45 /** @var \Drupal\Core\Database\Query\ConditionInterface $add_condition */
46 $add_condition = NULL;
47 switch ($this->conditionType) {
49 $add_condition = $query;
53 $group = $query->andConditionGroup()
55 $add_condition = $group;
56 $query->condition($group);
60 $query->exists('type');
61 $sub_group = $query->andConditionGroup()
63 $add_condition = $sub_group;
64 $group = $query->andConditionGroup()
66 ->condition($sub_group);
67 $query->condition($group);
70 if ($this->isReusable) {
71 $add_condition->condition('reusable', 1);
74 $add_condition->condition('reusable', 0);