Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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 calculateDependencies() {
120     parent::calculateDependencies();
121     if ($module = \Drupal::state()->get('config_test_new_dependency', FALSE)) {
122       $this->addDependency('module', $module);
123     }
124     return $this;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function onDependencyRemoval(array $dependencies) {
131     // Record which entities have this method called on and what dependencies
132     // are passed.
133     $called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
134     $called[$this->id()] = $dependencies;
135     $called[$this->id()]['config'] = array_keys($called[$this->id()]['config']);
136     $called[$this->id()]['content'] = array_keys($called[$this->id()]['content']);
137     \Drupal::state()->set('config_test.on_dependency_removal_called', $called);
138
139     $changed = parent::onDependencyRemoval($dependencies);
140     if (!isset($this->dependencies['enforced']['config'])) {
141       return $changed;
142     }
143     $fix_deps = \Drupal::state()->get('config_test.fix_dependencies', []);
144     foreach ($dependencies['config'] as $entity) {
145       if (in_array($entity->getConfigDependencyName(), $fix_deps)) {
146         $key = array_search($entity->getConfigDependencyName(), $this->dependencies['enforced']['config']);
147         if ($key !== FALSE) {
148           $changed = TRUE;
149           unset($this->dependencies['enforced']['config'][$key]);
150         }
151       }
152     }
153     // If any of the dependencies removed still exists, return FALSE.
154     if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
155       return FALSE;
156     }
157     return $changed;
158   }
159
160   /**
161    * Sets the enforced dependencies.
162    *
163    * @param array $dependencies
164    *   A config dependency array.
165    *
166    * @return $this
167    *
168    * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
169    */
170   public function setEnforcedDependencies(array $dependencies) {
171     $this->dependencies['enforced'] = $dependencies;
172     return $this;
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function isInstallable() {
179     return $this->id != 'isinstallable' || \Drupal::state()->get('config_test.isinstallable');
180   }
181
182 }