You are here

protected function FieldStorageConfig::preSaveNew in Drupal 8

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

Prepares saving a new field definition.

Parameters

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

Throws

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

1 call to FieldStorageConfig::preSaveNew()
FieldStorageConfig::preSave in core/modules/field/src/Entity/FieldStorageConfig.php
Overrides \Drupal\Core\Entity\Entity::preSave().

File

core/modules/field/src/Entity/FieldStorageConfig.php, line 312

Class

FieldStorageConfig
Defines the Field storage configuration entity.

Namespace

Drupal\field\Entity

Code

protected function preSaveNew(EntityStorageInterface $storage) {
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');

  // Assign the ID.
  $this->id = $this
    ->id();

  // Field name cannot be longer than FieldStorageConfig::NAME_MAX_LENGTH
  // characters. We use mb_strlen() because the DB layer assumes that column
  // widths are given in characters rather than bytes.
  if (mb_strlen($this
    ->getName()) > static::NAME_MAX_LENGTH) {
    throw new FieldException('Attempt to create a field storage with an name longer than ' . static::NAME_MAX_LENGTH . ' characters: ' . $this
      ->getName());
  }

  // Disallow reserved field names.
  $disallowed_field_names = array_keys($entity_field_manager
    ->getBaseFieldDefinitions($this
    ->getTargetEntityTypeId()));
  if (in_array($this
    ->getName(), $disallowed_field_names)) {
    throw new FieldException("Attempt to create field storage {$this->getName()} which is reserved by entity type {$this->getTargetEntityTypeId()}.");
  }

  // Check that the field type is known.
  $field_type = $field_type_manager
    ->getDefinition($this
    ->getType(), FALSE);
  if (!$field_type) {
    throw new FieldException("Attempt to create a field storage of unknown type {$this->getType()}.");
  }
  $this->module = $field_type['provider'];

  // Notify the field storage definition listener.
  \Drupal::service('field_storage_definition.listener')
    ->onFieldStorageDefinitionCreate($this);
}