public function Field::__construct

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Entity/Field/Type/Field.php \Drupal\Core\Entity\Field\Type\Field::__construct()
  2. 8.x drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php \Drupal\field\Plugin\views\field\Field::__construct()
  3. 8.x drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php \Drupal\field\Plugin\Core\Entity\Field::__construct()

Constructs a Field object.

Parameters

array $values: An array of field properties, keyed by property name. Most array elements will be used to set the corresponding properties on the class; see the class property documentation for details. Some array elements have special meanings and a few are required. Special elements are:

  • id: required. As a temporary Backwards Compatibility layer right now, a 'field_name' property can be accepted in place of 'id'.
  • type: required.

In most cases, Field entities are created via entity_create('field_entity', $values)), where $values is the same parameter as in this constructor.

Overrides ConfigEntityBase::__construct

See also

entity_create()

Related topics

1 call to Field::__construct()
Field::unserialize in drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php

File

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

Class

Field
Defines the Field entity.

Namespace

Drupal\field\Plugin\Core\Entity

Code

public function __construct(array $values, $entity_type = 'field_entity') {

  // Check required properties.
  if (empty($values['type'])) {
    throw new FieldException('Attempt to create a field with no type.');
  }

  // Temporary BC layer: accept both 'id' and 'field_name'.
  // @todo $field_name and the handling for it will be removed in
  //   http://drupal.org/node/1953408.
  if (empty($values['field_name']) && empty($values['id'])) {
    throw new FieldException('Attempt to create an unnamed field.');
  }
  if (empty($values['id'])) {
    $values['id'] = $values['field_name'];
    unset($values['field_name']);
  }
  if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $values['id'])) {
    throw new FieldException('Attempt to create a field with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character');
  }
  parent::__construct($values, $entity_type);
}