You are here

public function FieldConfig::preSave in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::preSave()

Overrides \Drupal\Core\Entity\Entity::preSave().

Throws

\Drupal\Core\Field\FieldException If the field definition is invalid.

\Drupal\Core\Entity\EntityStorageException In case of failures at the configuration storage level.

Overrides ConfigEntityBase::preSave

File

core/modules/field/src/Entity/FieldConfig.php, line 149

Class

FieldConfig
Defines the Field entity.

Namespace

Drupal\field\Entity

Code

public function preSave(EntityStorageInterface $storage) {
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $storage_definition = $this
    ->getFieldStorageDefinition();

  // Filter out unknown settings and make sure all settings are present, so
  // that a complete field definition is passed to the various hooks and
  // written to config.
  $default_settings = $field_type_manager
    ->getDefaultFieldSettings($storage_definition
    ->getType());
  $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
  if ($this
    ->isNew()) {

    // Notify the entity storage.
    \Drupal::service('field_definition.listener')
      ->onFieldDefinitionCreate($this);
  }
  else {

    // Some updates are always disallowed.
    if ($this->entity_type != $this->original->entity_type) {
      throw new FieldException("Cannot change an existing field's entity_type.");
    }
    if ($this->bundle != $this->original->bundle) {
      throw new FieldException("Cannot change an existing field's bundle.");
    }
    if ($storage_definition
      ->uuid() != $this->original
      ->getFieldStorageDefinition()
      ->uuid()) {
      throw new FieldException("Cannot change an existing field's storage.");
    }

    // Notify the entity storage.
    \Drupal::service('field_definition.listener')
      ->onFieldDefinitionUpdate($this, $this->original);
  }
  parent::preSave($storage);
}