You are here

public function FieldConfig::preSave in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 148
Contains \Drupal\field\Entity\FieldConfig.

Class

FieldConfig
Defines the Field entity.

Namespace

Drupal\field\Entity

Code

public function preSave(EntityStorageInterface $storage) {
  $entity_manager = \Drupal::entityManager();
  $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.
    $entity_manager
      ->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.
    $entity_manager
      ->onFieldDefinitionUpdate($this, $this->original);
  }
  parent::preSave($storage);
}