3 namespace Drupal\config_test\Entity;
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;
11 * Defines the ConfigTest configuration entity.
15 * label = @Translation("Test configuration"),
17 * "storage" = "Drupal\config_test\ConfigTestStorage",
18 * "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
20 * "default" = "Drupal\config_test\ConfigTestForm",
21 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
23 * "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
25 * config_prefix = "dynamic",
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",
40 class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
43 * The machine name for the configuration entity.
50 * The human-readable name of the configuration entity.
57 * The weight of the configuration entity.
64 * The image style to use.
71 * A protected property of the configuration entity.
75 protected $protected_property;
80 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
81 \Drupal::state()->set('config_entity_sort', TRUE);
82 return parent::sort($a, $b);
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([
93 'label' => 'Secondary Default',
97 if ($this->id() == 'deleter') {
98 $deletee = $storage->load('deletee');
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');
119 public function calculateDependencies() {
120 parent::calculateDependencies();
121 if ($module = \Drupal::state()->get('config_test_new_dependency', FALSE)) {
122 $this->addDependency('module', $module);
130 public function onDependencyRemoval(array $dependencies) {
131 // Record which entities have this method called on and what dependencies
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);
139 $changed = parent::onDependencyRemoval($dependencies);
140 if (!isset($this->dependencies['enforced']['config'])) {
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) {
149 unset($this->dependencies['enforced']['config'][$key]);
153 // If any of the dependencies removed still exists, return FALSE.
154 if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
161 * Sets the enforced dependencies.
163 * @param array $dependencies
164 * A config dependency array.
168 * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
170 public function setEnforcedDependencies(array $dependencies) {
171 $this->dependencies['enforced'] = $dependencies;
178 public function isInstallable() {
179 return $this->id != 'isinstallable' || \Drupal::state()->get('config_test.isinstallable');