Chris@0: type = $type; Chris@0: $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition); Chris@0: // Create a definition for the items, and initialize it with the default Chris@0: // settings for the field type. Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type); Chris@0: $field_definition->itemDefinition->setSettings($default_settings); Chris@0: return $field_definition; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new field definition based upon a field storage definition. Chris@0: * Chris@0: * In cases where one needs a field storage definitions to act like full Chris@0: * field definitions, this creates a new field definition based upon the Chris@0: * (limited) information available. That way it is possible to use the field Chris@0: * definition in places where a full field definition is required; e.g., with Chris@0: * widgets or formatters. Chris@0: * Chris@0: * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition Chris@0: * The field storage definition to base the new field definition upon. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) { Chris@0: return static::create($definition->getType()) Chris@0: ->setCardinality($definition->getCardinality()) Chris@0: ->setConstraints($definition->getConstraints()) Chris@0: ->setCustomStorage($definition->hasCustomStorage()) Chris@0: ->setDescription($definition->getDescription()) Chris@0: ->setLabel($definition->getLabel()) Chris@0: ->setName($definition->getName()) Chris@0: ->setProvider($definition->getProvider()) Chris@0: ->setRevisionable($definition->isRevisionable()) Chris@0: ->setSettings($definition->getSettings()) Chris@0: ->setTargetEntityTypeId($definition->getTargetEntityTypeId()) Chris@0: ->setTranslatable($definition->isTranslatable()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function createFromItemType($item_type) { Chris@0: // The data type of a field item is in the form of "field_item:$field_type". Chris@0: $parts = explode(':', $item_type, 2); Chris@0: return static::create($parts[1]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() { Chris@0: return $this->definition['field_name']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the field name. Chris@0: * Chris@0: * @param string $name Chris@0: * The field name to set. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setName($name) { Chris@0: $this->definition['field_name'] = $name; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getType() { Chris@0: return $this->type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSettings() { Chris@0: return $this->getItemDefinition()->getSettings(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * Note that the method does not unset existing settings not specified in the Chris@0: * incoming $settings array. Chris@0: * Chris@0: * For example: Chris@0: * @code Chris@0: * // Given these are the default settings. Chris@0: * $field_definition->getSettings() === [ Chris@0: * 'fruit' => 'apple', Chris@0: * 'season' => 'summer', Chris@0: * ]; Chris@0: * // Change only the 'fruit' setting. Chris@0: * $field_definition->setSettings(['fruit' => 'banana']); Chris@0: * // The 'season' setting persists unchanged. Chris@0: * $field_definition->getSettings() === [ Chris@0: * 'fruit' => 'banana', Chris@0: * 'season' => 'summer', Chris@0: * ]; Chris@0: * @endcode Chris@0: * Chris@0: * For clarity, it is preferred to use setSetting() if not all available Chris@0: * settings are supplied. Chris@0: */ Chris@0: public function setSettings(array $settings) { Chris@0: // Assign settings individually, in order to keep the current values Chris@0: // of settings not specified in $settings. Chris@0: foreach ($settings as $setting_name => $setting) { Chris@0: $this->getItemDefinition()->setSetting($setting_name, $setting); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSetting($setting_name) { Chris@0: return $this->getItemDefinition()->getSetting($setting_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setSetting($setting_name, $value) { Chris@0: $this->getItemDefinition()->setSetting($setting_name, $value); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getProvider() { Chris@0: return isset($this->definition['provider']) ? $this->definition['provider'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the name of the provider of this field. Chris@0: * Chris@0: * @param string $provider Chris@0: * The provider name to set. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setProvider($provider) { Chris@0: $this->definition['provider'] = $provider; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isTranslatable() { Chris@0: return !empty($this->definition['translatable']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the field is translatable. Chris@0: * Chris@0: * @param bool $translatable Chris@0: * Whether the field is translatable. Chris@0: * Chris@0: * @return $this Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setTranslatable($translatable) { Chris@0: $this->definition['translatable'] = $translatable; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isRevisionable() { Chris@16: // Multi-valued base fields are always considered revisionable, just like Chris@16: // configurable fields. Chris@16: return !empty($this->definition['revisionable']) || $this->isMultiple(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the field is revisionable. Chris@0: * Chris@0: * @param bool $revisionable Chris@0: * Whether the field is revisionable. Chris@0: * Chris@0: * @return $this Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setRevisionable($revisionable) { Chris@0: $this->definition['revisionable'] = $revisionable; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCardinality() { Chris@0: // @todo: Allow to control this. Chris@0: return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the maximum number of items allowed for the field. Chris@0: * Chris@0: * Possible values are positive integers or Chris@0: * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED. Chris@0: * Chris@16: * Note that if the entity type that this base field is attached to is Chris@16: * revisionable and the field has a cardinality higher than 1, the field is Chris@16: * considered revisionable by default. Chris@16: * Chris@0: * @param int $cardinality Chris@0: * The field cardinality. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setCardinality($cardinality) { Chris@0: $this->definition['cardinality'] = $cardinality; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isMultiple() { Chris@0: $cardinality = $this->getCardinality(); Chris@0: return ($cardinality == static::CARDINALITY_UNLIMITED) || ($cardinality > 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isQueryable() { Chris@0: @trigger_error('BaseFieldDefinition::isQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::hasCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED); Chris@0: return !$this->hasCustomStorage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the field is queryable. Chris@0: * Chris@0: * @param bool $queryable Chris@0: * Whether the field is queryable. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: * Chris@0: * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use Chris@0: * \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage() instead. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2856563 Chris@0: */ Chris@0: public function setQueryable($queryable) { Chris@0: @trigger_error('BaseFieldDefinition::setQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED); Chris@0: $this->definition['queryable'] = $queryable; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets constraints for a given field item property. Chris@0: * Chris@0: * Note: this overwrites any existing property constraints. If you need to Chris@0: * add to the existing constraints, use Chris@0: * \Drupal\Core\Field\BaseFieldDefinition::addPropertyConstraints() Chris@0: * Chris@0: * @param string $name Chris@0: * The name of the property to set constraints for. Chris@0: * @param array $constraints Chris@0: * The constraints to set. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setPropertyConstraints($name, array $constraints) { Chris@0: $item_constraints = $this->getItemDefinition()->getConstraints(); Chris@0: $item_constraints['ComplexData'][$name] = $constraints; Chris@0: $this->getItemDefinition()->setConstraints($item_constraints); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds constraints for a given field item property. Chris@0: * Chris@0: * Adds a constraint to a property of a base field item. e.g. Chris@0: * @code Chris@0: * // Limit the field item's value property to the range 0 through 10. Chris@0: * // e.g. $node->size->value. Chris@0: * $field->addPropertyConstraints('value', [ Chris@0: * 'Range' => [ Chris@0: * 'min' => 0, Chris@0: * 'max' => 10, Chris@0: * ] Chris@0: * ]); Chris@0: * @endcode Chris@0: * Chris@0: * If you want to add a validation constraint that applies to the Chris@0: * \Drupal\Core\Field\FieldItemList, use BaseFieldDefinition::addConstraint() Chris@0: * instead. Chris@0: * Chris@0: * Note: passing a new set of options for an existing property constraint will Chris@0: * overwrite with the new options. Chris@0: * Chris@0: * @param string $name Chris@0: * The name of the property to set constraints for. Chris@0: * @param array $constraints Chris@0: * The constraints to set. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: * Chris@0: * @see \Drupal\Core\Field\BaseFieldDefinition::addConstraint() Chris@0: */ Chris@0: public function addPropertyConstraints($name, array $constraints) { Chris@0: $item_constraints = $this->getItemDefinition()->getConstraint('ComplexData') ?: []; Chris@0: if (isset($item_constraints[$name])) { Chris@0: // Add the new property constraints, overwriting as required. Chris@0: $item_constraints[$name] = $constraints + $item_constraints[$name]; Chris@0: } Chris@0: else { Chris@0: $item_constraints[$name] = $constraints; Chris@0: } Chris@0: $this->getItemDefinition()->addConstraint('ComplexData', $item_constraints); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the display options for the field in forms or rendered entities. Chris@0: * Chris@0: * This enables generic rendering of the field with widgets / formatters, Chris@0: * including automated support for "In place editing", and with optional Chris@0: * configurability in the "Manage display" / "Manage form display" UI screens. Chris@0: * Chris@0: * Unless this method is called, the field remains invisible (or requires Chris@0: * ad-hoc rendering logic). Chris@0: * Chris@0: * @param string $display_context Chris@0: * The display context. Either 'view' or 'form'. Chris@0: * @param array $options Chris@0: * An array of display options. Refer to Chris@0: * \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for Chris@0: * a list of supported keys. The options should include at least a 'weight', Chris@0: * or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter' Chris@0: * for the field type will be used if no 'type' is specified. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setDisplayOptions($display_context, array $options) { Chris@0: $this->definition['display'][$display_context]['options'] = $options; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the display for the field can be configured. Chris@0: * Chris@0: * @param string $display_context Chris@0: * The display context. Either 'view' or 'form'. Chris@0: * @param bool $configurable Chris@0: * Whether the display options can be configured (e.g., via the "Manage Chris@0: * display" / "Manage form display" UI screens). If TRUE, the options Chris@0: * specified via getDisplayOptions() act as defaults. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setDisplayConfigurable($display_context, $configurable) { Chris@0: // If no explicit display options have been specified, default to 'hidden'. Chris@0: if (empty($this->definition['display'][$display_context])) { Chris@0: $this->definition['display'][$display_context]['options'] = ['region' => 'hidden']; Chris@0: } Chris@0: $this->definition['display'][$display_context]['configurable'] = $configurable; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDisplayOptions($display_context) { Chris@0: return isset($this->definition['display'][$display_context]['options']) ? $this->definition['display'][$display_context]['options'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDisplayConfigurable($display_context) { Chris@0: return isset($this->definition['display'][$display_context]['configurable']) ? $this->definition['display'][$display_context]['configurable'] : FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDefaultValueLiteral() { Chris@0: return isset($this->definition['default_value']) ? $this->definition['default_value'] : []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDefaultValueCallback() { Chris@0: return isset($this->definition['default_value_callback']) ? $this->definition['default_value_callback'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDefaultValue(FieldableEntityInterface $entity) { Chris@0: // Allow custom default values function. Chris@0: if ($callback = $this->getDefaultValueCallback()) { Chris@0: $value = call_user_func($callback, $entity, $this); Chris@0: } Chris@0: else { Chris@0: $value = $this->getDefaultValueLiteral(); Chris@0: } Chris@17: $value = $this->normalizeValue($value, $this->getMainPropertyName()); Chris@0: // Allow the field type to process default values. Chris@0: $field_item_list_class = $this->getClass(); Chris@0: return $field_item_list_class::processDefaultValue($value, $entity, $this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setDefaultValue($value) { Chris@0: if ($value === NULL) { Chris@0: $value = []; Chris@0: } Chris@0: // Unless the value is an empty array, we may need to transform it. Chris@0: if (!is_array($value) || !empty($value)) { Chris@0: if (!is_array($value)) { Chris@0: $value = [[$this->getMainPropertyName() => $value]]; Chris@0: } Chris@0: elseif (is_array($value) && !is_numeric(array_keys($value)[0])) { Chris@0: $value = [0 => $value]; Chris@0: } Chris@0: } Chris@0: $this->definition['default_value'] = $value; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setDefaultValueCallback($callback) { Chris@0: if (isset($callback) && !is_string($callback)) { Chris@0: throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"'); Chris@0: } Chris@0: $this->definition['default_value_callback'] = $callback; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the initial value for the field. Chris@0: * Chris@0: * @return array Chris@0: * The initial value for the field, as a numerically indexed array of items, Chris@0: * each item being a property/value array (array() for no default value). Chris@0: */ Chris@0: public function getInitialValue() { Chris@17: return $this->normalizeValue($this->definition['initial_value'], $this->getMainPropertyName()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets an initial value for the field. Chris@0: * Chris@0: * @param mixed $value Chris@0: * The initial value for the field. This can be either: Chris@0: * - a literal, in which case it will be assigned to the first property of Chris@0: * the first item; Chris@0: * - a numerically indexed array of items, each item being a property/value Chris@0: * array; Chris@0: * - a non-numerically indexed array, in which case the array is assumed to Chris@0: * be a property/value array and used as the first item; Chris@0: * - an empty array for no initial value. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setInitialValue($value) { Chris@0: // @todo Implement initial value support for multi-value fields in Chris@0: // https://www.drupal.org/node/2883851. Chris@0: if ($this->isMultiple()) { Chris@0: throw new FieldException('Multi-value fields can not have an initial value.'); Chris@0: } Chris@0: Chris@17: $this->definition['initial_value'] = $this->normalizeValue($value, $this->getMainPropertyName()); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the name of the field that will be used for getting initial values. Chris@0: * Chris@0: * @return string|null Chris@0: * The field name. Chris@0: */ Chris@0: public function getInitialValueFromField() { Chris@0: return isset($this->definition['initial_value_from_field']) ? $this->definition['initial_value_from_field'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a field that will be used for getting initial values. Chris@0: * Chris@0: * @param string $field_name Chris@0: * The name of the field that will be used for getting initial values. Chris@16: * @param mixed $default_value Chris@16: * (optional) The default value for the field, in case the inherited value Chris@16: * is NULL. This can be either: Chris@16: * - a literal, in which case it will be assigned to the first property of Chris@16: * the first item; Chris@16: * - a numerically indexed array of items, each item being a property/value Chris@16: * array; Chris@16: * - a non-numerically indexed array, in which case the array is assumed to Chris@16: * be a property/value array and used as the first item; Chris@16: * - an empty array for no initial value. Chris@16: * If the field being added is required or an entity key, it is recommended Chris@16: * to provide a default value. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@16: public function setInitialValueFromField($field_name, $default_value = NULL) { Chris@0: $this->definition['initial_value_from_field'] = $field_name; Chris@16: $this->setInitialValue($default_value); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOptionsProvider($property_name, FieldableEntityInterface $entity) { Chris@0: // If the field item class implements the interface, create an orphaned Chris@0: // runtime item object, so that it can be used as the options provider Chris@0: // without modifying the entity being worked on. Chris@14: if (is_subclass_of($this->getItemDefinition()->getClass(), OptionsProviderInterface::class)) { Chris@0: $items = $entity->get($this->getName()); Chris@0: return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0); Chris@0: } Chris@0: // @todo: Allow setting custom options provider, see Chris@0: // https://www.drupal.org/node/2002138. Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyDefinition($name) { Chris@0: if (!isset($this->propertyDefinitions)) { Chris@0: $this->getPropertyDefinitions(); Chris@0: } Chris@0: if (isset($this->propertyDefinitions[$name])) { Chris@0: return $this->propertyDefinitions[$name]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyDefinitions() { Chris@0: if (!isset($this->propertyDefinitions)) { Chris@14: $class = $this->getItemDefinition()->getClass(); Chris@0: $this->propertyDefinitions = $class::propertyDefinitions($this); Chris@0: } Chris@0: return $this->propertyDefinitions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyNames() { Chris@0: return array_keys($this->getPropertyDefinitions()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMainPropertyName() { Chris@14: $class = $this->getItemDefinition()->getClass(); Chris@0: return $class::mainPropertyName(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper to retrieve the field item class. Chris@0: * Chris@14: * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use Chris@14: * \Drupal\Core\TypedData\ListDataDefinition::getClass() instead. Chris@0: */ Chris@0: protected function getFieldItemClass() { Chris@14: @trigger_error('BaseFieldDefinition::getFieldItemClass() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\TypedData\ListDataDefinition::getClass(). See https://www.drupal.org/node/2933964.', E_USER_DEPRECATED); Chris@0: if ($class = $this->getItemDefinition()->getClass()) { Chris@0: return $class; Chris@0: } Chris@0: else { Chris@0: $type_definition = \Drupal::typedDataManager() Chris@0: ->getDefinition($this->getItemDefinition()->getDataType()); Chris@0: return $type_definition['class']; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __sleep() { Chris@0: // Do not serialize the statically cached property definitions. Chris@0: $vars = get_object_vars($this); Chris@0: unset($vars['propertyDefinitions'], $vars['typedDataManager']); Chris@0: return array_keys($vars); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTargetEntityTypeId() { Chris@0: return isset($this->definition['entity_type']) ? $this->definition['entity_type'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the ID of the type of the entity this field is attached to. Chris@0: * Chris@0: * @param string $entity_type_id Chris@0: * The name of the target entity type to set. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setTargetEntityTypeId($entity_type_id) { Chris@0: $this->definition['entity_type'] = $entity_type_id; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTargetBundle() { Chris@0: return isset($this->definition['bundle']) ? $this->definition['bundle'] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the bundle this field is defined for. Chris@0: * Chris@0: * @param string|null $bundle Chris@0: * The bundle, or NULL if the field is not bundle-specific. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setTargetBundle($bundle) { Chris@0: $this->definition['bundle'] = $bundle; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSchema() { Chris@0: if (!isset($this->schema)) { Chris@0: // Get the schema from the field item class. Chris@0: $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getType()); Chris@0: $class = $definition['class']; Chris@0: $schema = $class::schema($this); Chris@0: // Fill in default values. Chris@0: $schema += [ Chris@0: 'columns' => [], Chris@0: 'unique keys' => [], Chris@0: 'indexes' => [], Chris@0: 'foreign keys' => [], Chris@0: ]; Chris@0: Chris@0: // Merge custom indexes with those specified by the field type. Custom Chris@0: // indexes prevail. Chris@0: $schema['indexes'] = $this->indexes + $schema['indexes']; Chris@0: Chris@0: $this->schema = $schema; Chris@0: } Chris@0: Chris@0: return $this->schema; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getColumns() { Chris@0: $schema = $this->getSchema(); Chris@0: return $schema['columns']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasCustomStorage() { Chris@0: return !empty($this->definition['custom_storage']) || $this->isComputed(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isBaseField() { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the storage behavior for this field. Chris@0: * Chris@0: * @param bool $custom_storage Chris@0: * Pass FALSE if the storage takes care of storing the field, Chris@0: * TRUE otherwise. Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws \LogicException Chris@0: * Thrown if custom storage is to be set to FALSE for a computed field. Chris@0: */ Chris@0: public function setCustomStorage($custom_storage) { Chris@0: if (!$custom_storage && $this->isComputed()) { Chris@0: throw new \LogicException("Entity storage cannot store a computed field."); Chris@0: } Chris@0: $this->definition['custom_storage'] = $custom_storage; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFieldStorageDefinition() { Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getUniqueStorageIdentifier() { Chris@0: return $this->getTargetEntityTypeId() . '-' . $this->getName(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@14: public function getUniqueIdentifier() { Chris@14: // If we have a specified target bundle, we're dealing with a bundle base Chris@14: // field definition, so we need to include it in the unique identifier. Chris@14: if ($this->getTargetBundle()) { Chris@14: return $this->getTargetEntityTypeId() . '-' . $this->getTargetBundle() . '-' . $this->getName(); Chris@14: } Chris@14: Chris@14: return $this->getUniqueStorageIdentifier(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function isDeleted() { Chris@14: return !empty($this->definition['deleted']); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Sets whether the field storage is deleted. Chris@14: * Chris@14: * @param bool $deleted Chris@14: * Whether the field storage is deleted. Chris@14: * Chris@14: * @return $this Chris@14: */ Chris@14: public function setDeleted($deleted) { Chris@14: $this->definition['deleted'] = $deleted; Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@0: public function getConfig($bundle) { Chris@0: $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName()); Chris@0: if ($override) { Chris@0: return $override; Chris@0: } Chris@0: return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isStorageRequired() { Chris@0: if (isset($this->definition['storage_required'])) { Chris@0: return (bool) $this->definition['storage_required']; Chris@0: } Chris@0: Chris@0: // Default to the 'required' property of the base field. Chris@0: return $this->isRequired(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether the field storage is required. Chris@0: * Chris@0: * @param bool $required Chris@0: * Whether the field storage is required. Chris@0: * Chris@0: * @return static Chris@0: * The object itself for chaining. Chris@0: */ Chris@0: public function setStorageRequired($required) { Chris@0: $this->definition['storage_required'] = $required; Chris@0: return $this; Chris@0: } Chris@0: Chris@14: /** Chris@14: * Magic method: Implements a deep clone. Chris@14: */ Chris@14: public function __clone() { Chris@14: parent::__clone(); Chris@14: Chris@14: // The itemDefinition (\Drupal\Core\Field\TypedData\FieldItemDataDefinition) Chris@14: // has a property fieldDefinition, which is a recursive reference to the Chris@14: // parent BaseFieldDefinition, therefore the reference to the old object has Chris@14: // to be overwritten with a reference to the cloned one. Chris@14: $this->itemDefinition->setFieldDefinition($this); Chris@14: // Reset the static cache of the field property definitions in order to Chris@14: // ensure that the clone will reference different field property definitions Chris@14: // objects. Chris@14: $this->propertyDefinitions = NULL; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function isInternal() { Chris@14: // All fields are not internal unless explicitly set. Chris@14: return !empty($this->definition['internal']); Chris@14: } Chris@14: Chris@0: }