function FieldConfig::__construct

Same name in other branches
  1. 9 core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::__construct()
  2. 10 core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::__construct()
  3. 11.x core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::__construct()

Constructs a FieldConfig object.

In most cases, Field entities are created via FieldConfig::create($values), where $values is the same parameter as in this constructor.

Parameters

array $values: An array of field properties, keyed by property name. The storage associated with the field can be specified either with:

or by referring to an existing field storage in the current configuration with:

  • field_name: The field name.
  • entity_type: The entity type.

Additionally, a 'bundle' property is required to indicate the entity bundle to which the field is attached to. Other array elements will be used to set the corresponding properties on the class; see the class property documentation for details.

Overrides ConfigEntityBase::__construct

File

core/modules/field/src/Entity/FieldConfig.php, line 94

Class

FieldConfig
Defines the Field entity.

Namespace

Drupal\field\Entity

Code

public function __construct(array $values, $entity_type = 'field_config') {
    // Allow either an injected FieldStorageConfig object, or a field_name and
    // entity_type.
    if (isset($values['field_storage'])) {
        if (!$values['field_storage'] instanceof FieldStorageConfigInterface) {
            throw new FieldException('Attempt to create a configurable field for a non-configurable field storage.');
        }
        $field_storage = $values['field_storage'];
        $values['field_name'] = $field_storage->getName();
        $values['entity_type'] = $field_storage->getTargetEntityTypeId();
        // The internal property is fieldStorage, not field_storage.
        unset($values['field_storage']);
        $values['fieldStorage'] = $field_storage;
    }
    else {
        if (empty($values['field_name'])) {
            throw new FieldException('Attempt to create a field without a field_name.');
        }
        if (empty($values['entity_type'])) {
            throw new FieldException("Attempt to create a field '{$values['field_name']}' without an entity_type.");
        }
    }
    // 'bundle' is required in either case.
    if (empty($values['bundle'])) {
        throw new FieldException("Attempt to create a field '{$values['field_name']}' without a bundle.");
    }
    parent::__construct($values, $entity_type);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.