public function FieldInstance::__construct

Constructs a FieldInstance object.

Parameters

array $values: An array of field instance 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; these special elements are:

  • field_name: optional. The name of the field this is an instance of.
  • field_uuid: optional. Either field_uuid or field_name is required to build field instance. field_name will gain higher priority. If field_name is not provided, field_uuid will be checked then.
  • entity_type: required.
  • bundle: required.

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

Overrides ConfigEntityBase::__construct

See also

entity_create()

Related topics

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

File

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

Class

FieldInstance
Defines the Field instance entity.

Namespace

Drupal\field\Plugin\Core\Entity

Code

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

  // Accept incoming 'field_name' instead of 'field_uuid', for easier DX on
  // creation of new instances.
  if (isset($values['field_name']) && !isset($values['field_uuid'])) {
    $field = field_info_field($values['field_name']);
    if (!$field) {
      throw new FieldException(format_string('Attempt to create an instance of unknown, disabled, or deleted field @field_id', array(
        '@field_id' => $values['field_name'],
      )));
    }
    $values['field_uuid'] = $field->uuid;
  }
  elseif (isset($values['field_uuid'])) {
    $field = field_info_field_by_id($values['field_uuid']);

    // field_info_field_by_id() will not find the field if it is inactive.
    if (!$field) {
      $field = current(field_read_fields(array(
        'uuid' => $values['field_uuid'],
      ), array(
        'include_inactive' => TRUE,
        'include_deleted' => TRUE,
      )));
    }
    if (!$field) {
      throw new FieldException(format_string('Attempt to create an instance of unknown field @uuid', array(
        '@uuid' => $values['field_uuid'],
      )));
    }
  }
  else {
    throw new FieldException('Attempt to create an instance of an unspecified field.');
  }

  // At this point, we should have a 'field_uuid' and a Field. Ditch the
  // 'field_name' property if it was provided, and assign the $field property.
  unset($values['field_name']);
  $this->field = $field;

  // Discard the 'field_type' entry that is added in config records to ease
  // schema generation. See getExportProperties().
  unset($values['field_type']);

  // Check required properties.
  if (empty($values['entity_type'])) {
    throw new FieldException(format_string('Attempt to create an instance of field @field_id without an entity type.', array(
      '@field_id' => $this->field->id,
    )));
  }
  if (empty($values['bundle'])) {
    throw new FieldException(format_string('Attempt to create an instance of field @field_id without a bundle.', array(
      '@field_id' => $this->field->id,
    )));
  }

  // 'Label' defaults to the field ID (mostly useful for field instances
  // created in tests).
  $values += array(
    'label' => $this->field->id,
  );
  parent::__construct($values, $entity_type);
}