You are here

public function BaseFieldOverride::preSave in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php \Drupal\Core\Field\Entity\BaseFieldOverride::preSave()

Throws

\Drupal\Core\Field\FieldException If the bundle is being changed.

Overrides ConfigEntityBase::preSave

File

core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php, line 176

Class

BaseFieldOverride
Defines the base field override entity.

Namespace

Drupal\Core\Field\Entity

Code

public function preSave(EntityStorageInterface $storage) {

  // 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.
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $default_settings = $field_type_manager
    ->getDefaultFieldSettings($this
    ->getType());
  $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

  // Call the parent's presave method to perform validate and calculate
  // dependencies.
  parent::preSave($storage);
  if ($this
    ->isNew()) {

    // @todo This assumes that the previous definition isn't some
    //   non-config-based override, but that might not be the case:
    //   https://www.drupal.org/node/2321071.
    $previous_definition = $this
      ->getBaseFieldDefinition();
  }
  else {

    // Some updates are always disallowed.
    if ($this->entity_type != $this->original->entity_type) {
      throw new FieldException("Cannot change the entity_type of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})");
    }
    if ($this->bundle != $this->original->bundle) {
      throw new FieldException("Cannot change the bundle of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})");
    }
    $previous_definition = $this->original;
  }

  // Notify the entity storage.
  $this
    ->entityTypeManager()
    ->getStorage($this
    ->getTargetEntityTypeId())
    ->onFieldDefinitionUpdate($this, $previous_definition);
}