protected function Field::saveUpdated

Saves an updated field definition.

Return value

int SAVED_UPDATED if the definition was saved.

Throws

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

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

1 call to Field::saveUpdated()
Field::save in drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php
Overrides \Drupal\Core\Entity\Entity::save().

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php, line 401
Contains \Drupal\field\Plugin\Core\Entity\Field.

Class

Field
Defines the Field entity.

Namespace

Drupal\field\Plugin\Core\Entity

Code

protected function saveUpdated() {
  $module_handler = \Drupal::moduleHandler();
  $storage_controller = \Drupal::entityManager()
    ->getStorageController($this->entityType);
  $original = $storage_controller
    ->loadUnchanged($this
    ->id());

  // Some updates are always disallowed.
  if ($this->type != $original->type) {
    throw new FieldException("Cannot change an existing field's type.");
  }
  if ($this->entity_types != $original->entity_types) {
    throw new FieldException("Cannot change an existing field's entity_types property.");
  }
  if ($this->storage['type'] != $original->storage['type']) {
    throw new FieldException("Cannot change an existing field's storage type.");
  }

  // Make sure all settings are present, so that a complete field definition
  // is saved. This allows calling code to perform partial updates on field
  // objects.
  $this->settings += $original->settings;
  $has_data = field_has_data($this);

  // See if any module forbids the update by throwing an exception. This
  // invokes hook_field_update_forbid().
  $module_handler
    ->invokeAll('field_update_forbid', array(
    $this,
    $original,
    $has_data,
  ));

  // Tell the storage engine to update the field by invoking the
  // hook_field_storage_update_field(). The storage engine can reject the
  // definition update as invalid by raising an exception, which stops
  // execution before the definition is written to config.
  $module_handler
    ->invoke($this->storage['module'], 'field_storage_update_field', array(
    $this,
    $original,
    $has_data,
  ));

  // Save the configuration.
  $result = parent::save();
  field_cache_clear();

  // Invoke hook_field_update_field() after the cache is cleared for API
  // consistency.
  $module_handler
    ->invokeAll('field_update_field', array(
    $this,
    $original,
    $has_data,
  ));
  return $result;
}