ConfigEntityAdapter.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\DataType;
  3. use Drupal\Core\TypedData\Exception\MissingDataException;
  4. /**
  5. * Enhances EntityAdapter for config entities.
  6. */
  7. class ConfigEntityAdapter extends EntityAdapter {
  8. /**
  9. * The wrapped entity object.
  10. *
  11. * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
  12. */
  13. protected $entity;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function get($property_name) {
  18. if (!isset($this->entity)) {
  19. throw new MissingDataException("Unable to get property $property_name as no entity has been provided.");
  20. }
  21. return $this->getConfigTypedData()->get($property_name);
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function set($property_name, $value, $notify = TRUE) {
  27. if (!isset($this->entity)) {
  28. throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
  29. }
  30. $this->entity->set($property_name, $value, $notify);
  31. return $this;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getProperties($include_computed = FALSE) {
  37. if (!isset($this->entity)) {
  38. throw new MissingDataException('Unable to get properties as no entity has been provided.');
  39. }
  40. return $this->getConfigTypedData()->getProperties($include_computed);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function onChange($property_name) {
  46. if (isset($this->entity)) {
  47. // Let the entity know of any changes.
  48. $this->getConfigTypedData()->onChange($property_name);
  49. }
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getIterator() {
  55. if (isset($this->entity)) {
  56. return $this->getConfigTypedData()->getIterator();
  57. }
  58. return new \ArrayIterator([]);
  59. }
  60. /**
  61. * Gets the typed data manager.
  62. *
  63. * @return \Drupal\Core\Config\TypedConfigManagerInterface
  64. * The typed data manager.
  65. */
  66. public function getTypedDataManager() {
  67. if (empty($this->typedDataManager)) {
  68. $this->typedDataManager = \Drupal::service('config.typed');
  69. }
  70. return $this->typedDataManager;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function applyDefaultValue($notify = TRUE) {
  76. // @todo Figure out what to do for this method, see
  77. // https://www.drupal.org/project/drupal/issues/2945635.
  78. throw new \BadMethodCallException('Method not supported');
  79. }
  80. /**
  81. * Gets typed data for config entity.
  82. *
  83. * @return \Drupal\Core\TypedData\ComplexDataInterface
  84. * The typed data.
  85. */
  86. protected function getConfigTypedData() {
  87. return $this->getTypedDataManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
  88. }
  89. }