3 namespace Drupal\Core\Field;
5 use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
6 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\Core\Field\Entity\BaseFieldOverride;
8 use Drupal\Core\Field\TypedData\FieldItemDataDefinition;
9 use Drupal\Core\TypedData\ListDataDefinition;
10 use Drupal\Core\TypedData\OptionsProviderInterface;
13 * A class for defining entity fields.
15 class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface {
17 use UnchangingCacheableDependencyTrait;
27 * An array of field property definitions.
29 * @var \Drupal\Core\TypedData\DataDefinitionInterface[]
31 * @see \Drupal\Core\TypedData\ComplexDataDefinitionInterface::getPropertyDefinitions()
33 protected $propertyDefinitions;
45 protected $indexes = [];
48 * Creates a new field definition.
51 * The type of the field.
54 * A new field definition object.
56 public static function create($type) {
57 $field_definition = new static([]);
58 $field_definition->type = $type;
59 $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition);
60 // Create a definition for the items, and initialize it with the default
61 // settings for the field type.
62 // @todo Cleanup in https://www.drupal.org/node/2116341.
63 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
64 $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type);
65 $field_definition->itemDefinition->setSettings($default_settings);
66 return $field_definition;
70 * Creates a new field definition based upon a field storage definition.
72 * In cases where one needs a field storage definitions to act like full
73 * field definitions, this creates a new field definition based upon the
74 * (limited) information available. That way it is possible to use the field
75 * definition in places where a full field definition is required; e.g., with
76 * widgets or formatters.
78 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
79 * The field storage definition to base the new field definition upon.
83 public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) {
84 return static::create($definition->getType())
85 ->setCardinality($definition->getCardinality())
86 ->setConstraints($definition->getConstraints())
87 ->setCustomStorage($definition->hasCustomStorage())
88 ->setDescription($definition->getDescription())
89 ->setLabel($definition->getLabel())
90 ->setName($definition->getName())
91 ->setProvider($definition->getProvider())
92 ->setQueryable($definition->isQueryable())
93 ->setRevisionable($definition->isRevisionable())
94 ->setSettings($definition->getSettings())
95 ->setTargetEntityTypeId($definition->getTargetEntityTypeId())
96 ->setTranslatable($definition->isTranslatable());
102 public static function createFromItemType($item_type) {
103 // The data type of a field item is in the form of "field_item:$field_type".
104 $parts = explode(':', $item_type, 2);
105 return static::create($parts[1]);
111 public function getName() {
112 return $this->definition['field_name'];
116 * Sets the field name.
118 * @param string $name
119 * The field name to set.
122 * The object itself for chaining.
124 public function setName($name) {
125 $this->definition['field_name'] = $name;
132 public function getType() {
139 public function getSettings() {
140 return $this->getItemDefinition()->getSettings();
146 * Note that the method does not unset existing settings not specified in the
147 * incoming $settings array.
151 * // Given these are the default settings.
152 * $field_definition->getSettings() === [
153 * 'fruit' => 'apple',
154 * 'season' => 'summer',
156 * // Change only the 'fruit' setting.
157 * $field_definition->setSettings(['fruit' => 'banana']);
158 * // The 'season' setting persists unchanged.
159 * $field_definition->getSettings() === [
160 * 'fruit' => 'banana',
161 * 'season' => 'summer',
165 * For clarity, it is preferred to use setSetting() if not all available
166 * settings are supplied.
168 public function setSettings(array $settings) {
169 // Assign settings individually, in order to keep the current values
170 // of settings not specified in $settings.
171 foreach ($settings as $setting_name => $setting) {
172 $this->getItemDefinition()->setSetting($setting_name, $setting);
180 public function getSetting($setting_name) {
181 return $this->getItemDefinition()->getSetting($setting_name);
187 public function setSetting($setting_name, $value) {
188 $this->getItemDefinition()->setSetting($setting_name, $value);
195 public function getProvider() {
196 return isset($this->definition['provider']) ? $this->definition['provider'] : NULL;
200 * Sets the name of the provider of this field.
202 * @param string $provider
203 * The provider name to set.
207 public function setProvider($provider) {
208 $this->definition['provider'] = $provider;
215 public function isTranslatable() {
216 return !empty($this->definition['translatable']);
220 * Sets whether the field is translatable.
222 * @param bool $translatable
223 * Whether the field is translatable.
226 * The object itself for chaining.
228 public function setTranslatable($translatable) {
229 $this->definition['translatable'] = $translatable;
236 public function isRevisionable() {
237 return !empty($this->definition['revisionable']);
241 * Sets whether the field is revisionable.
243 * @param bool $revisionable
244 * Whether the field is revisionable.
247 * The object itself for chaining.
249 public function setRevisionable($revisionable) {
250 $this->definition['revisionable'] = $revisionable;
257 public function getCardinality() {
258 // @todo: Allow to control this.
259 return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1;
263 * Sets the maximum number of items allowed for the field.
265 * Possible values are positive integers or
266 * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.
268 * @param int $cardinality
269 * The field cardinality.
273 public function setCardinality($cardinality) {
274 $this->definition['cardinality'] = $cardinality;
281 public function isMultiple() {
282 $cardinality = $this->getCardinality();
283 return ($cardinality == static::CARDINALITY_UNLIMITED) || ($cardinality > 1);
289 public function isQueryable() {
290 return isset($this->definition['queryable']) ? $this->definition['queryable'] : !$this->isComputed();
294 * Sets whether the field is queryable.
296 * @param bool $queryable
297 * Whether the field is queryable.
300 * The object itself for chaining.
302 public function setQueryable($queryable) {
303 $this->definition['queryable'] = $queryable;
308 * Sets constraints for a given field item property.
310 * Note: this overwrites any existing property constraints. If you need to
311 * add to the existing constraints, use
312 * \Drupal\Core\Field\BaseFieldDefinition::addPropertyConstraints()
314 * @param string $name
315 * The name of the property to set constraints for.
316 * @param array $constraints
317 * The constraints to set.
320 * The object itself for chaining.
322 public function setPropertyConstraints($name, array $constraints) {
323 $item_constraints = $this->getItemDefinition()->getConstraints();
324 $item_constraints['ComplexData'][$name] = $constraints;
325 $this->getItemDefinition()->setConstraints($item_constraints);
330 * Adds constraints for a given field item property.
332 * Adds a constraint to a property of a base field item. e.g.
334 * // Limit the field item's value property to the range 0 through 10.
335 * // e.g. $node->size->value.
336 * $field->addPropertyConstraints('value', [
344 * If you want to add a validation constraint that applies to the
345 * \Drupal\Core\Field\FieldItemList, use BaseFieldDefinition::addConstraint()
348 * Note: passing a new set of options for an existing property constraint will
349 * overwrite with the new options.
351 * @param string $name
352 * The name of the property to set constraints for.
353 * @param array $constraints
354 * The constraints to set.
357 * The object itself for chaining.
359 * @see \Drupal\Core\Field\BaseFieldDefinition::addConstraint()
361 public function addPropertyConstraints($name, array $constraints) {
362 $item_constraints = $this->getItemDefinition()->getConstraint('ComplexData') ?: [];
363 if (isset($item_constraints[$name])) {
364 // Add the new property constraints, overwriting as required.
365 $item_constraints[$name] = $constraints + $item_constraints[$name];
368 $item_constraints[$name] = $constraints;
370 $this->getItemDefinition()->addConstraint('ComplexData', $item_constraints);
375 * Sets the display options for the field in forms or rendered entities.
377 * This enables generic rendering of the field with widgets / formatters,
378 * including automated support for "In place editing", and with optional
379 * configurability in the "Manage display" / "Manage form display" UI screens.
381 * Unless this method is called, the field remains invisible (or requires
382 * ad-hoc rendering logic).
384 * @param string $display_context
385 * The display context. Either 'view' or 'form'.
386 * @param array $options
387 * An array of display options. Refer to
388 * \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for
389 * a list of supported keys. The options should include at least a 'weight',
390 * or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter'
391 * for the field type will be used if no 'type' is specified.
394 * The object itself for chaining.
396 public function setDisplayOptions($display_context, array $options) {
397 $this->definition['display'][$display_context]['options'] = $options;
402 * Sets whether the display for the field can be configured.
404 * @param string $display_context
405 * The display context. Either 'view' or 'form'.
406 * @param bool $configurable
407 * Whether the display options can be configured (e.g., via the "Manage
408 * display" / "Manage form display" UI screens). If TRUE, the options
409 * specified via getDisplayOptions() act as defaults.
412 * The object itself for chaining.
414 public function setDisplayConfigurable($display_context, $configurable) {
415 // If no explicit display options have been specified, default to 'hidden'.
416 if (empty($this->definition['display'][$display_context])) {
417 $this->definition['display'][$display_context]['options'] = ['region' => 'hidden'];
419 $this->definition['display'][$display_context]['configurable'] = $configurable;
426 public function getDisplayOptions($display_context) {
427 return isset($this->definition['display'][$display_context]['options']) ? $this->definition['display'][$display_context]['options'] : NULL;
433 public function isDisplayConfigurable($display_context) {
434 return isset($this->definition['display'][$display_context]['configurable']) ? $this->definition['display'][$display_context]['configurable'] : FALSE;
440 public function getDefaultValueLiteral() {
441 return isset($this->definition['default_value']) ? $this->definition['default_value'] : [];
447 public function getDefaultValueCallback() {
448 return isset($this->definition['default_value_callback']) ? $this->definition['default_value_callback'] : NULL;
454 public function getDefaultValue(FieldableEntityInterface $entity) {
455 // Allow custom default values function.
456 if ($callback = $this->getDefaultValueCallback()) {
457 $value = call_user_func($callback, $entity, $this);
460 $value = $this->getDefaultValueLiteral();
462 // Normalize into the "array keyed by delta" format.
463 if (isset($value) && !is_array($value)) {
464 $properties = $this->getPropertyNames();
465 $property = reset($properties);
467 [$property => $value],
470 // Allow the field type to process default values.
471 $field_item_list_class = $this->getClass();
472 return $field_item_list_class::processDefaultValue($value, $entity, $this);
478 public function setDefaultValue($value) {
479 if ($value === NULL) {
482 // Unless the value is an empty array, we may need to transform it.
483 if (!is_array($value) || !empty($value)) {
484 if (!is_array($value)) {
485 $value = [[$this->getMainPropertyName() => $value]];
487 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
488 $value = [0 => $value];
491 $this->definition['default_value'] = $value;
498 public function setDefaultValueCallback($callback) {
499 if (isset($callback) && !is_string($callback)) {
500 throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"');
502 $this->definition['default_value_callback'] = $callback;
509 public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
510 // If the field item class implements the interface, create an orphaned
511 // runtime item object, so that it can be used as the options provider
512 // without modifying the entity being worked on.
513 if (is_subclass_of($this->getFieldItemClass(), OptionsProviderInterface::class)) {
514 $items = $entity->get($this->getName());
515 return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
517 // @todo: Allow setting custom options provider, see
518 // https://www.drupal.org/node/2002138.
524 public function getPropertyDefinition($name) {
525 if (!isset($this->propertyDefinitions)) {
526 $this->getPropertyDefinitions();
528 if (isset($this->propertyDefinitions[$name])) {
529 return $this->propertyDefinitions[$name];
536 public function getPropertyDefinitions() {
537 if (!isset($this->propertyDefinitions)) {
538 $class = $this->getFieldItemClass();
539 $this->propertyDefinitions = $class::propertyDefinitions($this);
541 return $this->propertyDefinitions;
547 public function getPropertyNames() {
548 return array_keys($this->getPropertyDefinitions());
554 public function getMainPropertyName() {
555 $class = $this->getFieldItemClass();
556 return $class::mainPropertyName();
560 * Helper to retrieve the field item class.
562 * @todo: Remove once getClass() adds in defaults. See
563 * https://www.drupal.org/node/2116341.
565 protected function getFieldItemClass() {
566 if ($class = $this->getItemDefinition()->getClass()) {
570 $type_definition = \Drupal::typedDataManager()
571 ->getDefinition($this->getItemDefinition()->getDataType());
572 return $type_definition['class'];
579 public function __sleep() {
580 // Do not serialize the statically cached property definitions.
581 $vars = get_object_vars($this);
582 unset($vars['propertyDefinitions']);
583 return array_keys($vars);
589 public function getTargetEntityTypeId() {
590 return isset($this->definition['entity_type']) ? $this->definition['entity_type'] : NULL;
594 * Sets the ID of the type of the entity this field is attached to.
596 * @param string $entity_type_id
597 * The name of the target entity type to set.
601 public function setTargetEntityTypeId($entity_type_id) {
602 $this->definition['entity_type'] = $entity_type_id;
609 public function getTargetBundle() {
610 return isset($this->definition['bundle']) ? $this->definition['bundle'] : NULL;
614 * Sets the bundle this field is defined for.
616 * @param string|null $bundle
617 * The bundle, or NULL if the field is not bundle-specific.
621 public function setTargetBundle($bundle) {
622 $this->definition['bundle'] = $bundle;
629 public function getSchema() {
630 if (!isset($this->schema)) {
631 // Get the schema from the field item class.
632 $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getType());
633 $class = $definition['class'];
634 $schema = $class::schema($this);
635 // Fill in default values.
640 'foreign keys' => [],
643 // Merge custom indexes with those specified by the field type. Custom
645 $schema['indexes'] = $this->indexes + $schema['indexes'];
647 $this->schema = $schema;
650 return $this->schema;
656 public function getColumns() {
657 $schema = $this->getSchema();
658 // A typical use case for the method is to iterate on the columns, while
659 // some other use cases rely on identifying the first column with the key()
660 // function. Since the schema is persisted in the Field object, we take care
661 // of resetting the array pointer so that the former does not interfere with
663 reset($schema['columns']);
664 return $schema['columns'];
670 public function hasCustomStorage() {
671 return !empty($this->definition['custom_storage']) || $this->isComputed();
677 public function isBaseField() {
682 * Sets the storage behavior for this field.
684 * @param bool $custom_storage
685 * Pass FALSE if the storage takes care of storing the field,
690 * @throws \LogicException
691 * Thrown if custom storage is to be set to FALSE for a computed field.
693 public function setCustomStorage($custom_storage) {
694 if (!$custom_storage && $this->isComputed()) {
695 throw new \LogicException("Entity storage cannot store a computed field.");
697 $this->definition['custom_storage'] = $custom_storage;
704 public function getFieldStorageDefinition() {
711 public function getUniqueStorageIdentifier() {
712 return $this->getTargetEntityTypeId() . '-' . $this->getName();
718 public function getConfig($bundle) {
719 $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName());
723 return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle);
729 public function isStorageRequired() {
730 if (isset($this->definition['storage_required'])) {
731 return (bool) $this->definition['storage_required'];
734 // Default to the 'required' property of the base field.
735 return $this->isRequired();
739 * Sets whether the field storage is required.
741 * @param bool $required
742 * Whether the field storage is required.
745 * The object itself for chaining.
747 public function setStorageRequired($required) {
748 $this->definition['storage_required'] = $required;