ConfigTest.php 4.7 KB

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