You are here

protected function FieldStorageConfig::preSaveNew in Zircon Profile 8

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

Class

FieldStorageConfig
Defines the Field storage configuration entity.

Namespace

Drupal\field\Entity

Code

protected function preSaveNew(EntityStorageInterface $storage) {
  $entity_manager = \Drupal::entityManager();
  $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 Unicode::strlen() because the DB layer assumes that column widths
  // are given in characters rather than bytes.
  if (Unicode::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_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 entity manager.
  $entity_manager
    ->onFieldStorageDefinitionCreate($this);
}