Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
- 9 core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
Namespace
Drupal\Core\Config\Entity
File
core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
View source
<?php
namespace Drupal\Core\Config\Entity;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Config\ConfigDuplicateUUIDException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Entity\SynchronizableEntityTrait;
use Drupal\Core\Plugin\PluginDependencyTrait;
abstract class ConfigEntityBase extends EntityBase implements ConfigEntityInterface {
use PluginDependencyTrait {
addDependency as addDependencyTrait;
}
use SynchronizableEntityTrait;
protected $originalId;
protected $status = TRUE;
protected $uuid;
private $isUninstalling = FALSE;
protected $langcode = 'en';
protected $third_party_settings = [];
protected $_core = [];
protected $trustedData = FALSE;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$original_id = $this
->id();
if ($original_id !== NULL && $original_id !== '') {
$this
->setOriginalId($original_id);
}
}
public function getOriginalId() {
return $this->originalId;
}
public function setOriginalId($id) {
$this->originalId = $id;
return $this;
}
public function isNew() {
return !empty($this->enforceIsNew);
}
public function get($property_name) {
return $this->{$property_name} ?? NULL;
}
public function set($property_name, $value) {
if ($this instanceof EntityWithPluginCollectionInterface && !$this
->isSyncing()) {
$plugin_collections = $this
->getPluginCollections();
if (isset($plugin_collections[$property_name])) {
$plugin_collections[$property_name]
->setConfiguration($value);
}
}
$this->{$property_name} = $value;
return $this;
}
public function enable() {
return $this
->setStatus(TRUE);
}
public function disable() {
return $this
->setStatus(FALSE);
}
public function setStatus($status) {
$this->status = (bool) $status;
return $this;
}
public function status() {
return !empty($this->status);
}
public function setUninstalling($uninstalling) {
$this->isUninstalling = $uninstalling;
}
public function isUninstalling() {
return $this->isUninstalling;
}
public function createDuplicate() {
$duplicate = parent::createDuplicate();
$duplicate
->setOriginalId(NULL);
return $duplicate;
}
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
$a_weight = $a->weight ?? 0;
$b_weight = $b->weight ?? 0;
if ($a_weight == $b_weight) {
$a_label = $a
->label() ?? '';
$b_label = $b
->label() ?? '';
return strnatcasecmp($a_label, $b_label);
}
return $a_weight <=> $b_weight;
}
public function toArray() {
$properties = [];
$entity_type = $this
->getEntityType();
$id_key = $entity_type
->getKey('id');
$property_names = $entity_type
->getPropertiesToExport($this
->id());
if (empty($property_names)) {
throw new SchemaIncompleteException(sprintf("Entity type '%s' is missing 'config_export' definition in its annotation", $entity_type
->getClass()));
}
foreach ($property_names as $property_name => $export_name) {
if ($property_name == $id_key) {
$properties[$export_name] = $this
->id();
}
else {
$properties[$export_name] = $this
->get($property_name);
}
}
if (empty($this->third_party_settings)) {
unset($properties['third_party_settings']);
}
if (empty($this->_core)) {
unset($properties['_core']);
}
return $properties;
}
protected function getTypedConfig() {
return \Drupal::service('config.typed');
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this instanceof EntityWithPluginCollectionInterface && !$this
->isSyncing()) {
foreach ($this
->getPluginCollections() as $plugin_config_key => $plugin_collection) {
$this
->set($plugin_config_key, $plugin_collection
->getConfiguration());
}
}
$matching_entities = $storage
->getQuery()
->condition('uuid', $this
->uuid())
->execute();
$matched_entity = reset($matching_entities);
if (!empty($matched_entity) && $matched_entity != $this
->id() && $matched_entity != $this
->getOriginalId()) {
throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this UUID is already used for '{$matched_entity}'");
}
if (!$this
->isNew()) {
$original = $storage
->loadUnchanged($this
->getOriginalId());
if ($original && $original
->uuid() != $this
->uuid()) {
throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this entity already exists with UUID '{$original->uuid()}'");
}
}
if (!$this
->isSyncing()) {
$this
->calculateDependencies();
if ($this->trustedData) {
$mapping = [
'config' => 0,
'content' => 1,
'module' => 2,
'theme' => 3,
'enforced' => 4,
];
$dependency_sort = function ($dependencies) use ($mapping) {
$mapping_to_replace = array_intersect_key($mapping, $dependencies);
return array_replace($mapping_to_replace, $dependencies);
};
$this->dependencies = $dependency_sort($this->dependencies);
if (isset($this->dependencies['enforced'])) {
$this->dependencies['enforced'] = $dependency_sort($this->dependencies['enforced']);
}
}
}
}
public function __sleep() {
$keys_to_unset = [];
if ($this instanceof EntityWithPluginCollectionInterface) {
$plugin_collections = $this
->getPluginCollections();
$vars = get_object_vars($this);
foreach ($plugin_collections as $plugin_config_key => $plugin_collection) {
$this
->set($plugin_config_key, $plugin_collection
->getConfiguration());
$keys_to_unset += array_filter($vars, function ($value) use ($plugin_collection) {
return $plugin_collection === $value;
});
}
}
$vars = parent::__sleep();
if (!empty($keys_to_unset)) {
$vars = array_diff($vars, array_keys($keys_to_unset));
}
return $vars;
}
public function calculateDependencies() {
$this->dependencies = array_intersect_key($this->dependencies, [
'enforced' => '',
]);
if ($this instanceof EntityWithPluginCollectionInterface) {
foreach ($this
->getPluginCollections() as $plugin_collection) {
foreach ($plugin_collection as $instance) {
$this
->calculatePluginDependencies($instance);
}
}
}
if ($this instanceof ThirdPartySettingsInterface) {
foreach ($this
->getThirdPartyProviders() as $provider) {
$this
->addDependency('module', $provider);
}
}
return $this;
}
public function toUrl($rel = NULL, array $options = []) {
$options += [
'language' => NULL,
];
return parent::toUrl($rel, $options);
}
public function getCacheTagsToInvalidate() {
return [
'config:' . $this
->getConfigDependencyName(),
];
}
protected function addDependency($type, $name) {
if ($type == 'module' && ($name == $this
->getEntityType()
->getProvider() || $name == 'core')) {
return $this;
}
return $this
->addDependencyTrait($type, $name);
}
public function getDependencies() {
$dependencies = $this->dependencies;
if (isset($dependencies['enforced'])) {
$enforced_dependencies = $dependencies['enforced'];
unset($dependencies['enforced']);
$dependencies = NestedArray::mergeDeep($dependencies, $enforced_dependencies);
}
return $dependencies;
}
public function getConfigDependencyName() {
return $this
->getEntityType()
->getConfigPrefix() . '.' . $this
->id();
}
public function getConfigTarget() {
return $this
->id();
}
public function onDependencyRemoval(array $dependencies) {
$changed = FALSE;
if (!empty($this->third_party_settings)) {
$old_count = count($this->third_party_settings);
$this->third_party_settings = array_diff_key($this->third_party_settings, array_flip($dependencies['module']));
$changed = $old_count != count($this->third_party_settings);
}
return $changed;
}
protected function invalidateTagsOnSave($update) {
Cache::invalidateTags($this
->getListCacheTagsToInvalidate());
}
protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
$tags = $entity_type
->getListCacheTags();
foreach ($entities as $entity) {
$tags = Cache::mergeTags($tags, $entity
->getListCacheTagsToInvalidate());
}
Cache::invalidateTags($tags);
}
public function setThirdPartySetting($module, $key, $value) {
$this->third_party_settings[$module][$key] = $value;
return $this;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
if (isset($this->third_party_settings[$module][$key])) {
return $this->third_party_settings[$module][$key];
}
else {
return $default;
}
}
public function getThirdPartySettings($module) {
return $this->third_party_settings[$module] ?? [];
}
public function unsetThirdPartySetting($module, $key) {
unset($this->third_party_settings[$module][$key]);
if (empty($this->third_party_settings[$module])) {
unset($this->third_party_settings[$module]);
}
return $this;
}
public function getThirdPartyProviders() {
return array_keys($this->third_party_settings);
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
foreach ($entities as $entity) {
if ($entity
->isUninstalling() || $entity
->isSyncing()) {
break;
}
$config_entities = static::getConfigManager()
->getConfigEntitiesToChangeOnDependencyRemoval('config', [
$entity
->getConfigDependencyName(),
], FALSE);
foreach ($config_entities['update'] as $dependent_entity) {
$dependent_entity
->save();
}
foreach ($config_entities['delete'] as $dependent_entity) {
$dependent_entity
->delete();
}
}
}
protected static function getConfigManager() {
return \Drupal::service('config.manager');
}
public function isInstallable() {
return TRUE;
}
public function trustData() {
$this->trustedData = TRUE;
return $this;
}
public function hasTrustedData() {
return $this->trustedData;
}
public function save() {
$return = parent::save();
$this->trustedData = FALSE;
return $return;
}
}
Classes