FieldStorageConfig::preSaveNew

protected FieldStorageConfig::preSaveNew(EntityStorageInterface $storage)

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.

File

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

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);
}
doc_Drupal
2016-10-29 09:12:24
Comments
Leave a Comment

Please login to continue.