ConfigEntityAdapter.php 3.85 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Entity\Plugin\DataType;

5
use Drupal\Core\Config\TypedConfigManagerInterface;
6
use Drupal\Core\TypedData\Exception\MissingDataException;
7
use Drupal\Core\TypedData\TypedDataManagerInterface;
8 9 10 11 12 13 14 15 16 17 18 19 20

/**
 * Enhances EntityAdapter for config entities.
 */
class ConfigEntityAdapter extends EntityAdapter {

  /**
   * The wrapped entity object.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
   */
  protected $entity;

21 22 23 24 25 26 27
  /**
   * The typed config manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  /**
   * {@inheritdoc}
   */
  public function get($property_name) {
    if (!isset($this->entity)) {
      throw new MissingDataException("Unable to get property $property_name as no entity has been provided.");
    }
    return $this->getConfigTypedData()->get($property_name);
  }

  /**
   * {@inheritdoc}
   */
  public function set($property_name, $value, $notify = TRUE) {
    if (!isset($this->entity)) {
      throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
    }
    $this->entity->set($property_name, $value, $notify);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getProperties($include_computed = FALSE) {
    if (!isset($this->entity)) {
      throw new MissingDataException('Unable to get properties as no entity has been provided.');
    }
    return $this->getConfigTypedData()->getProperties($include_computed);
  }

  /**
   * {@inheritdoc}
   */
  public function onChange($property_name) {
    if (isset($this->entity)) {
      // Let the entity know of any changes.
      $this->getConfigTypedData()->onChange($property_name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    if (isset($this->entity)) {
      return $this->getConfigTypedData()->getIterator();
    }
    return new \ArrayIterator([]);
  }

  /**
80
   * Gets the typed config manager.
81 82
   *
   * @return \Drupal\Core\Config\TypedConfigManagerInterface
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
   *   The typed config manager.
   */
  protected function getTypedConfigManager() {
    if (empty($this->typedConfigManager)) {
      // Use the typed data manager if it is also the typed config manager.
      // @todo Remove this in https://www.drupal.org/node/3011137.
      $typed_data_manager = $this->getTypedDataManager();
      if ($typed_data_manager instanceof TypedConfigManagerInterface) {
        $this->typedConfigManager = $typed_data_manager;
      }
      else {
        $this->typedConfigManager = \Drupal::service('config.typed');
      }
    }

    return $this->typedConfigManager;
  }

  /**
   * {@inheritdoc}
   *
   * @todo Remove this in https://www.drupal.org/node/3011137.
105 106 107 108 109 110 111 112 113
   */
  public function getTypedDataManager() {
    if (empty($this->typedDataManager)) {
      $this->typedDataManager = \Drupal::service('config.typed');
    }

    return $this->typedDataManager;
  }

114 115 116 117 118 119 120 121 122 123 124 125 126
  /**
   * {@inheritdoc}
   *
   * @todo Remove this in https://www.drupal.org/node/3011137.
   */
  public function setTypedDataManager(TypedDataManagerInterface $typed_data_manager) {
    $this->typedDataManager = $typed_data_manager;
    if ($typed_data_manager instanceof TypedConfigManagerInterface) {
      $this->typedConfigManager = $typed_data_manager;
    }
    return $this;
  }

127 128 129 130 131 132 133 134 135 136 137 138
  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
    // @todo Figure out what to do for this method, see
    //   https://www.drupal.org/project/drupal/issues/2945635.
    throw new \BadMethodCallException('Method not supported');
  }

  /**
   * Gets typed data for config entity.
   *
139
   * @return \Drupal\Core\TypedData\TraversableTypedDataInterface
140 141 142
   *   The typed data.
   */
  protected function getConfigTypedData() {
143
    return $this->getTypedConfigManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
144 145 146
  }

}