/** * Constructs a BaseFieldOverride object. * * In most cases, base field override entities are created via * BaseFieldOverride::createFromBaseFieldDefinition($definition, 'bundle') * * @param array $values * An array of base field bundle override properties, keyed by property * name. The field to override is specified by referring to an existing * field 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 bundle field override is attached to. Other array * elements will be used to set the corresponding properties on the class; * see the class property documentation for details. * @param string $entity_type * (optional) The type of the entity to create. Defaults to * 'base_field_override'. * * @see entity_create() * * @throws \Drupal\Core\Field\FieldException * Exception thrown if $values does not contain a field_name, entity_type or * bundle value. */ public function __construct(array $values, $entity_type = 'base_field_override') { if (empty($values['field_name'])) { throw new FieldException('Attempt to create a base field bundle override of a field without a field_name'); } if (empty($values['entity_type'])) { throw new FieldException(String::format('Attempt to create a base field bundle override of field @field_name without an entity_type', array('@field_name' => $values['field_name']))); } if (empty($values['bundle'])) { throw new FieldException(String::format('Attempt to create a base field bundle override of field @field_name without a bundle', array('@field_name' => $values['field_name']))); } parent::__construct($values, $entity_type); }
/** * Constructs a FieldConfig object. * * In most cases, Field entities are created via * entity_create('field_config', $values), where $values is the same * parameter as in this constructor. * * @param array $values * An array of field properties, keyed by property name. The * storage associated to the field can be specified either with: * - field_storage: the FieldStorageConfigInterface object, * 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. * * @see entity_create() */ 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(SafeMarkup::format('Attempt to create a field @field_name without an entity_type.', array('@field_name' => $values['field_name']))); } } // 'bundle' is required in either case. if (empty($values['bundle'])) { throw new FieldException(SafeMarkup::format('Attempt to create a field @field_name without a bundle.', array('@field_name' => $values['field_name']))); } parent::__construct($values, $entity_type); }
/** * Constructs a BaseFieldOverride object. * * In most cases, base field override entities are created via * BaseFieldOverride::createFromBaseFieldDefinition($definition, 'bundle') * * @param array $values * An array of base field bundle override properties, keyed by property * name. The field to override is specified by referring to an existing * field 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 bundle field override is attached to. Other array * elements will be used to set the corresponding properties on the class; * see the class property documentation for details. * @param string $entity_type * (optional) The type of the entity to create. Defaults to * 'base_field_override'. * * @see entity_create() * * @throws \Drupal\Core\Field\FieldException * Exception thrown if $values does not contain a field_name, entity_type or * bundle value. */ public function __construct(array $values, $entity_type = 'base_field_override') { if (empty($values['field_name'])) { throw new FieldException('Attempt to create a base field bundle override of a field without a field_name'); } if (empty($values['entity_type'])) { throw new FieldException("Attempt to create a base field bundle override of field {$values['field_name']} without an entity_type"); } if (empty($values['bundle'])) { throw new FieldException("Attempt to create a base field bundle override of field {$values['field_name']} without a bundle"); } parent::__construct($values, $entity_type); }