1bd1d8dee9b7b732c70b0522fec3e14d346630b7
[yaffs-website] / web / core / modules / config / tests / config_test / src / Entity / ConfigTest.php
1 <?php
2
3 namespace Drupal\config_test\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\config_test\ConfigTestInterface;
7 use Drupal\Core\Config\Entity\ConfigEntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9
10 /**
11  * Defines the ConfigTest configuration entity.
12  *
13  * @ConfigEntityType(
14  *   id = "config_test",
15  *   label = @Translation("Test configuration"),
16  *   handlers = {
17  *     "storage" = "Drupal\config_test\ConfigTestStorage",
18  *     "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
19  *     "form" = {
20  *       "default" = "Drupal\config_test\ConfigTestForm",
21  *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
22  *     },
23  *     "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
24  *   },
25  *   config_prefix = "dynamic",
26  *   entity_keys = {
27  *     "id" = "id",
28  *     "label" = "label",
29  *     "status" = "status"
30  *   },
31  *   links = {
32  *     "edit-form" = "/admin/structure/config_test/manage/{config_test}",
33  *     "delete-form" = "/admin/structure/config_test/manage/{config_test}/delete",
34  *     "enable" = "/admin/structure/config_test/manage/{config_test}/enable",
35  *     "disable" = "/admin/structure/config_test/manage/{config_test}/disable",
36  *     "collection" = "/admin/structure/config_test",
37  *   }
38  * )
39  */
40 class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
41
42   /**
43    * The machine name for the configuration entity.
44    *
45    * @var string
46    */
47   protected $id;
48
49   /**
50    * The human-readable name of the configuration entity.
51    *
52    * @var string
53    */
54   public $label;
55
56   /**
57    * The weight of the configuration entity.
58    *
59    * @var int
60    */
61   public $weight = 0;
62
63   /**
64    * The image style to use.
65    *
66    * @var string
67    */
68   public $style;
69
70   /**
71    * A protected property of the configuration entity.
72    *
73    * @var string
74    */
75   protected $protected_property;
76
77   /**
78    * {@inheritdoc}
79    */
80   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
81     \Drupal::state()->set('config_entity_sort', TRUE);
82     return parent::sort($a, $b);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
89     // Used to test secondary writes during config sync.
90     if ($this->id() == 'primary') {
91       $secondary = $storage->create([
92         'id' => 'secondary',
93         'label' => 'Secondary Default',
94       ]);
95       $secondary->save();
96     }
97     if ($this->id() == 'deleter') {
98       $deletee = $storage->load('deletee');
99       $deletee->delete();
100     }
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function postDelete(EntityStorageInterface $storage, array $entities) {
107     parent::postDelete($storage, $entities);
108     foreach ($entities as $entity) {
109       if ($entity->id() == 'deleter') {
110         $deletee = $storage->load('deletee');
111         $deletee->delete();
112       }
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function onDependencyRemoval(array $dependencies) {
120     // Record which entities have this method called on.
121     $called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
122     $called[] = $this->id();
123     \Drupal::state()->set('config_test.on_dependency_removal_called', $called);
124
125     $changed = parent::onDependencyRemoval($dependencies);
126     if (!isset($this->dependencies['enforced']['config'])) {
127       return $changed;
128     }
129     $fix_deps = \Drupal::state()->get('config_test.fix_dependencies', []);
130     foreach ($dependencies['config'] as $entity) {
131       if (in_array($entity->getConfigDependencyName(), $fix_deps)) {
132         $key = array_search($entity->getConfigDependencyName(), $this->dependencies['enforced']['config']);
133         if ($key !== FALSE) {
134           $changed = TRUE;
135           unset($this->dependencies['enforced']['config'][$key]);
136         }
137       }
138     }
139     // If any of the dependencies removed still exists, return FALSE.
140     if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
141       return FALSE;
142     }
143     return $changed;
144   }
145
146   /**
147    * Sets the enforced dependencies.
148    *
149    * @param array $dependencies
150    *   A config dependency array.
151    *
152    * @return $this
153    *
154    * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
155    */
156   public function setEnforcedDependencies(array $dependencies) {
157     $this->dependencies['enforced'] = $dependencies;
158     return $this;
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function isInstallable() {
165     return $this->id != 'isinstallable' || \Drupal::state()->get('config_test.isinstallable');
166   }
167
168 }