8.3.x ConfigEntityBase.php public ConfigEntityBase::preSave(EntityStorageInterface $storage)
8.0.x ConfigEntityBase.php public ConfigEntityBase::preSave(EntityStorageInterface $storage)
8.1.x ConfigEntityBase.php public ConfigEntityBase::preSave(EntityStorageInterface $storage)
8.2.x ConfigEntityBase.php public ConfigEntityBase::preSave(EntityStorageInterface $storage)
8.4.x ConfigEntityBase.php public ConfigEntityBase::preSave(EntityStorageInterface $storage)

Acts on an entity before the presave hook is invoked.

Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

Throws

\Exception When there is a problem that should prevent saving the entity.

Overrides Entity::preSave

See also

\Drupal\Core\Field\FieldItemListInterface::preSave()

11 calls to ConfigEntityBase::preSave()
BaseFieldOverride::preSave in core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php
ConfigEntityBundleBase::preSave in core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
Acts on an entity before the presave hook is invoked.
ConfigurableLanguage::preSave in core/modules/language/src/Entity/ConfigurableLanguage.php
Acts on an entity before the presave hook is invoked.
ContentLanguageSettings::preSave in core/modules/language/src/Entity/ContentLanguageSettings.php
Acts on an entity before the presave hook is invoked.
EntityDisplayBase::preSave in core/lib/Drupal/Core/Entity/EntityDisplayBase.php
Acts on an entity before the presave hook is invoked.

... See full list

11 methods override ConfigEntityBase::preSave()
BaseFieldOverride::preSave in core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php
ConfigEntityBundleBase::preSave in core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
Acts on an entity before the presave hook is invoked.
ConfigurableLanguage::preSave in core/modules/language/src/Entity/ConfigurableLanguage.php
Acts on an entity before the presave hook is invoked.
ContentLanguageSettings::preSave in core/modules/language/src/Entity/ContentLanguageSettings.php
Acts on an entity before the presave hook is invoked.
EntityDisplayBase::preSave in core/lib/Drupal/Core/Entity/EntityDisplayBase.php
Acts on an entity before the presave hook is invoked.

... See full list

File

core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php, line 313

Class

ConfigEntityBase
Defines a base configuration entity class.

Namespace

Drupal\Core\Config\Entity

Code

public function preSave(EntityStorageInterface $storage) {
  parent::preSave($storage);

  if ($this instanceof EntityWithPluginCollectionInterface) {
    // Any changes to the plugin configuration must be saved to the entity's
    // copy as well.
    foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) {
      $this->set($plugin_config_key, $plugin_collection->getConfiguration());
    }
  }

  // Ensure this entity's UUID does not exist with a different ID, regardless
  // of whether it's new or updated.
  $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 entity is not new, load the original entity for comparison.
  if (!$this->isNew()) {
    $original = $storage->loadUnchanged($this->getOriginalId());
    // Ensure that the UUID cannot be changed for an existing entity.
    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()) {
    // Ensure the correct dependencies are present. If the configuration is
    // being written during a configuration synchronization then there is no
    // need to recalculate the dependencies.
    $this->calculateDependencies();
  }
}