Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / tests / modules / block_content_test / src / Plugin / EntityReferenceSelection / TestSelection.php
1 <?php
2
3 namespace Drupal\block_content_test\Plugin\EntityReferenceSelection;
4
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
6
7 /**
8  * Test EntityReferenceSelection with conditions on the 'reusable' field.
9  */
10 class TestSelection extends DefaultSelection {
11
12   /**
13    * The condition type.
14    *
15    * @var string
16    */
17   protected $conditionType;
18
19   /**
20    * Whether to set the condition for reusable or non-reusable blocks.
21    *
22    * @var bool
23    */
24   protected $isReusable;
25
26   /**
27    * Sets the test mode.
28    *
29    * @param string $condition_type
30    *   The condition type.
31    * @param bool $is_reusable
32    *   Whether to set the condition for reusable or non-reusable blocks.
33    */
34   public function setTestMode($condition_type = NULL, $is_reusable = NULL) {
35     $this->conditionType = $condition_type;
36     $this->isReusable = $is_reusable;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
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) {
48         case 'base':
49           $add_condition = $query;
50           break;
51
52         case 'group':
53           $group = $query->andConditionGroup()
54             ->exists('type');
55           $add_condition = $group;
56           $query->condition($group);
57           break;
58
59         case "nested_group":
60           $query->exists('type');
61           $sub_group = $query->andConditionGroup()
62             ->exists('type');
63           $add_condition = $sub_group;
64           $group = $query->andConditionGroup()
65             ->exists('type')
66             ->condition($sub_group);
67           $query->condition($group);
68           break;
69       }
70       if ($this->isReusable) {
71         $add_condition->condition('reusable', 1);
72       }
73       else {
74         $add_condition->condition('reusable', 0);
75       }
76     }
77     return $query;
78   }
79
80 }