Chris@0: {$field_name}. The maximum length is Chris@0: * Field:NAME_MAX_LENGTH. Chris@0: * Chris@0: * Example: body, field_main_image. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $field_name; Chris@0: Chris@0: /** Chris@0: * The name of the entity type the field can be attached to. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $entity_type; Chris@0: Chris@0: /** Chris@0: * The field type. Chris@0: * Chris@0: * Example: text, integer. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $type; Chris@0: Chris@0: /** Chris@0: * The name of the module that provides the field type. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $module; Chris@0: Chris@0: /** Chris@0: * Field-type specific settings. Chris@0: * Chris@0: * An array of key/value pairs, The keys and default values are defined by the Chris@0: * field type. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $settings = []; Chris@0: Chris@0: /** Chris@0: * The field cardinality. Chris@0: * Chris@0: * The maximum number of values the field can hold. Possible values are Chris@0: * positive integers or Chris@0: * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED. Defaults to 1. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: protected $cardinality = 1; Chris@0: Chris@0: /** Chris@0: * Flag indicating whether the field is translatable. Chris@0: * Chris@0: * Defaults to TRUE. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $translatable = TRUE; Chris@0: Chris@0: /** Chris@0: * Flag indicating whether the field is available for editing. Chris@0: * Chris@0: * If TRUE, some actions not available though the UI (but are still possible Chris@0: * through direct API manipulation): Chris@0: * - field settings cannot be changed, Chris@0: * - new fields cannot be created Chris@0: * - existing fields cannot be deleted. Chris@0: * Defaults to FALSE. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $locked = FALSE; Chris@0: Chris@0: /** Chris@0: * Flag indicating whether the field storage should be deleted when orphaned. Chris@0: * Chris@0: * By default field storages for configurable fields are removed when there Chris@0: * are no remaining fields using them. If multiple modules provide bundles Chris@0: * which need to use the same field storage then setting this to TRUE will Chris@0: * preserve the field storage regardless of what happens to the bundles. The Chris@0: * classic use case for this is node body field storage since Book, Forum, the Chris@0: * Standard profile and bundle (node type) creation through the UI all use Chris@0: * same field storage. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $persist_with_no_fields = FALSE; Chris@0: Chris@0: /** Chris@0: * A boolean indicating whether or not the field item uses custom storage. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $custom_storage = FALSE; Chris@0: Chris@0: /** Chris@0: * The custom storage indexes for the field data storage. Chris@0: * Chris@0: * This set of indexes is merged with the "default" indexes specified by the Chris@0: * field type in hook_field_schema() to determine the actual set of indexes Chris@0: * that get created. Chris@0: * Chris@0: * The indexes are defined using the same definition format as Schema API Chris@0: * index specifications. Only columns that are part of the field schema, as Chris@0: * defined by the field type in hook_field_schema(), are allowed. Chris@0: * Chris@0: * Some storage backends might not support indexes, and discard that Chris@0: * information. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $indexes = []; Chris@0: Chris@0: /** Chris@0: * Flag indicating whether the field is deleted. Chris@0: * Chris@0: * The delete() method marks the field as "deleted" and removes the Chris@0: * corresponding entry from the config storage, but keeps its definition in Chris@0: * the state storage while field data is purged by a separate Chris@0: * garbage-collection process. Chris@0: * Chris@0: * Deleted fields stay out of the regular entity lifecycle (notably, their Chris@0: * values are not populated in loaded entities, and are not saved back). Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $deleted = FALSE; Chris@0: Chris@0: /** Chris@0: * The field schema. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $schema; Chris@0: Chris@0: /** Chris@0: * An array of field property definitions. Chris@0: * Chris@0: * @var \Drupal\Core\TypedData\DataDefinitionInterface[] Chris@0: * Chris@0: * @see \Drupal\Core\TypedData\ComplexDataDefinitionInterface::getPropertyDefinitions() Chris@0: */ Chris@0: protected $propertyDefinitions; Chris@0: Chris@0: /** Chris@0: * Static flag set to prevent recursion during field deletes. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected static $inDeletion = FALSE; Chris@0: Chris@0: /** Chris@0: * Constructs a FieldStorageConfig object. Chris@0: * Chris@0: * In most cases, Field entities are created via Chris@0: * FieldStorageConfig::create($values)), where $values is the same parameter Chris@0: * as in this constructor. Chris@0: * Chris@0: * @param array $values Chris@0: * An array of field properties, keyed by property name. Most array Chris@0: * elements will be used to set the corresponding properties on the class; Chris@0: * see the class property documentation for details. Some array elements Chris@0: * have special meanings and a few are required. Special elements are: Chris@0: * - name: required. As a temporary Backwards Compatibility layer right now, Chris@0: * a 'field_name' property can be accepted in place of 'id'. Chris@0: * - entity_type: required. Chris@0: * - type: required. Chris@0: * Chris@0: * @see entity_create() Chris@0: */ Chris@0: public function __construct(array $values, $entity_type = 'field_storage_config') { Chris@0: // Check required properties. Chris@0: if (empty($values['field_name'])) { Chris@0: throw new FieldException('Attempt to create a field storage without a field name.'); Chris@0: } Chris@0: if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $values['field_name'])) { Chris@0: throw new FieldException("Attempt to create a field storage {$values['field_name']} with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character"); Chris@0: } Chris@0: if (empty($values['type'])) { Chris@0: throw new FieldException("Attempt to create a field storage {$values['field_name']} with no type."); Chris@0: } Chris@0: if (empty($values['entity_type'])) { Chris@0: throw new FieldException("Attempt to create a field storage {$values['field_name']} with no entity_type."); Chris@0: } Chris@0: Chris@0: parent::__construct($values, $entity_type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function id() { Chris@0: return $this->getTargetEntityTypeId() . '.' . $this->getName(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides \Drupal\Core\Entity\Entity::preSave(). Chris@0: * Chris@0: * @throws \Drupal\Core\Field\FieldException Chris@0: * If the field definition is invalid. Chris@0: * @throws \Drupal\Core\Entity\EntityStorageException Chris@0: * In case of failures at the configuration storage level. Chris@0: */ Chris@0: public function preSave(EntityStorageInterface $storage) { Chris@0: // Clear the derived data about the field. Chris@0: unset($this->schema); Chris@0: Chris@0: // Filter out unknown settings and make sure all settings are present, so Chris@0: // that a complete field definition is passed to the various hooks and Chris@0: // written to config. Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: $default_settings = $field_type_manager->getDefaultStorageSettings($this->type); Chris@0: $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; Chris@0: Chris@0: if ($this->isNew()) { Chris@0: $this->preSaveNew($storage); Chris@0: } Chris@0: else { Chris@0: $this->preSaveUpdated($storage); Chris@0: } Chris@0: Chris@0: parent::preSave($storage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares saving a new field definition. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityStorageInterface $storage Chris@0: * The entity storage. Chris@0: * Chris@0: * @throws \Drupal\Core\Field\FieldException Chris@0: * If the field definition is invalid. Chris@0: */ Chris@0: protected function preSaveNew(EntityStorageInterface $storage) { Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: Chris@0: // Assign the ID. Chris@0: $this->id = $this->id(); Chris@0: Chris@17: // Field name cannot be longer than FieldStorageConfig::NAME_MAX_LENGTH Chris@17: // characters. We use mb_strlen() because the DB layer assumes that column Chris@17: // widths are given in characters rather than bytes. Chris@17: if (mb_strlen($this->getName()) > static::NAME_MAX_LENGTH) { Chris@0: throw new FieldException('Attempt to create a field storage with an name longer than ' . static::NAME_MAX_LENGTH . ' characters: ' . $this->getName()); Chris@0: } Chris@0: Chris@0: // Disallow reserved field names. Chris@0: $disallowed_field_names = array_keys($entity_manager->getBaseFieldDefinitions($this->getTargetEntityTypeId())); Chris@0: if (in_array($this->getName(), $disallowed_field_names)) { Chris@0: throw new FieldException("Attempt to create field storage {$this->getName()} which is reserved by entity type {$this->getTargetEntityTypeId()}."); Chris@0: } Chris@0: Chris@0: // Check that the field type is known. Chris@0: $field_type = $field_type_manager->getDefinition($this->getType(), FALSE); Chris@0: if (!$field_type) { Chris@0: throw new FieldException("Attempt to create a field storage of unknown type {$this->getType()}."); Chris@0: } Chris@0: $this->module = $field_type['provider']; Chris@0: Chris@0: // Notify the entity manager. Chris@18: \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function calculateDependencies() { Chris@0: parent::calculateDependencies(); Chris@0: // Ensure the field is dependent on the providing module. Chris@0: $this->addDependency('module', $this->getTypeProvider()); Chris@0: // Ask the field type for any additional storage dependencies. Chris@0: // @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies() Chris@0: $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getType(), FALSE); Chris@0: $this->addDependencies($definition['class']::calculateStorageDependencies($this)); Chris@0: Chris@0: // Ensure the field is dependent on the provider of the entity type. Chris@0: $entity_type = \Drupal::entityManager()->getDefinition($this->entity_type); Chris@0: $this->addDependency('module', $entity_type->getProvider()); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares saving an updated field definition. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityStorageInterface $storage Chris@0: * The entity storage. Chris@0: */ Chris@0: protected function preSaveUpdated(EntityStorageInterface $storage) { Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: Chris@0: // Some updates are always disallowed. Chris@0: if ($this->getType() != $this->original->getType()) { Chris@0: throw new FieldException("Cannot change the field type for an existing field storage."); Chris@0: } Chris@0: if ($this->getTargetEntityTypeId() != $this->original->getTargetEntityTypeId()) { Chris@0: throw new FieldException("Cannot change the entity type for an existing field storage."); Chris@0: } Chris@0: Chris@0: // See if any module forbids the update by throwing an exception. This Chris@0: // invokes hook_field_storage_config_update_forbid(). Chris@0: $module_handler->invokeAll('field_storage_config_update_forbid', [$this, $this->original]); Chris@0: Chris@0: // Notify the entity manager. A listener can reject the definition Chris@0: // update as invalid by raising an exception, which stops execution before Chris@0: // the definition is written to config. Chris@18: \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionUpdate($this, $this->original); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function postSave(EntityStorageInterface $storage, $update = TRUE) { Chris@0: if ($update) { Chris@0: // Invalidate the render cache for all affected entities. Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: $entity_type = $this->getTargetEntityTypeId(); Chris@0: if ($entity_manager->hasHandler($entity_type, 'view_builder')) { Chris@0: $entity_manager->getViewBuilder($entity_type)->resetCache(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function preDelete(EntityStorageInterface $storage, array $field_storages) { Chris@14: /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */ Chris@14: $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository'); Chris@0: Chris@0: // Set the static flag so that we don't delete field storages whilst Chris@0: // deleting fields. Chris@0: static::$inDeletion = TRUE; Chris@0: Chris@0: // Delete or fix any configuration that is dependent, for example, fields. Chris@0: parent::preDelete($storage, $field_storages); Chris@0: Chris@14: // Keep the field storage definitions in the deleted fields repository so we Chris@14: // can use them later during field_purge_batch(). Chris@0: /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ Chris@0: foreach ($field_storages as $field_storage) { Chris@0: // Only mark a field for purging if there is data. Otherwise, just remove Chris@0: // it. Chris@0: $target_entity_storage = \Drupal::entityTypeManager()->getStorage($field_storage->getTargetEntityTypeId()); Chris@0: if (!$field_storage->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field_storage, TRUE)) { Chris@14: $storage_definition = clone $field_storage; Chris@14: $storage_definition->deleted = TRUE; Chris@14: $deleted_fields_repository->addFieldStorageDefinition($storage_definition); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function postDelete(EntityStorageInterface $storage, array $fields) { Chris@0: // Notify the storage. Chris@0: foreach ($fields as $field) { Chris@0: if (!$field->deleted) { Chris@18: \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($field); Chris@0: $field->deleted = TRUE; Chris@0: } Chris@0: } Chris@0: // Unset static flag. Chris@0: static::$inDeletion = FALSE; 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: $class = $this->getFieldItemClass(); Chris@0: $schema = $class::schema($this); Chris@0: // Fill in default values for optional entries. 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 hasCustomStorage() { Chris@0: return $this->custom_storage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isBaseField() { Chris@0: return FALSE; 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 getBundles() { Chris@0: if (!$this->isDeleted()) { Chris@0: $map = \Drupal::entityManager()->getFieldMap(); Chris@0: if (isset($map[$this->getTargetEntityTypeId()][$this->getName()]['bundles'])) { Chris@0: return $map[$this->getTargetEntityTypeId()][$this->getName()]['bundles']; Chris@0: } Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() { Chris@0: return $this->field_name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDeleted() { Chris@0: return $this->deleted; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTypeProvider() { Chris@0: return $this->module; 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: // @todo FieldTypePluginManager maintains its own static cache. However, do Chris@0: // some CPU and memory profiling to see if it's worth statically caching Chris@0: // $field_type_info, or the default field storage and field settings, Chris@0: // within $this. Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: Chris@0: $settings = $field_type_manager->getDefaultStorageSettings($this->getType()); Chris@0: return $this->settings + $settings; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSetting($setting_name) { Chris@0: // @todo See getSettings() about potentially statically caching this. Chris@0: // We assume here that one call to array_key_exists() is more efficient Chris@0: // than calling getSettings() when all we need is a single setting. Chris@0: if (array_key_exists($setting_name, $this->settings)) { Chris@0: return $this->settings[$setting_name]; Chris@0: } Chris@0: $settings = $this->getSettings(); Chris@0: if (array_key_exists($setting_name, $settings)) { Chris@0: return $settings[$setting_name]; Chris@0: } Chris@0: else { Chris@0: return NULL; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setSetting($setting_name, $value) { Chris@0: $this->settings[$setting_name] = $value; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setSettings(array $settings) { Chris@0: $this->settings = $settings + $this->settings; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isTranslatable() { Chris@0: return $this->translatable; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isRevisionable() { Chris@0: // All configurable fields are revisionable. Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setTranslatable($translatable) { Chris@0: $this->translatable = $translatable; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getProvider() { Chris@0: return 'field'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLabel() { Chris@0: return $this->label(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDescription() { Chris@0: return NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCardinality() { Chris@0: /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */ Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: $definition = $field_type_manager->getDefinition($this->getType()); Chris@0: $enforced_cardinality = isset($definition['cardinality']) ? $definition['cardinality'] : NULL; Chris@0: Chris@0: // Enforced cardinality is a positive integer or -1. Chris@0: if ($enforced_cardinality !== NULL && $enforced_cardinality < 1 && $enforced_cardinality !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { Chris@0: throw new FieldException("Invalid enforced cardinality '$enforced_cardinality'. Allowed values: a positive integer or -1."); Chris@0: } Chris@0: Chris@0: return $enforced_cardinality ?: $this->cardinality; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setCardinality($cardinality) { Chris@0: $this->cardinality = $cardinality; 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@0: if (is_subclass_of($this->getFieldItemClass(), 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 isMultiple() { Chris@0: $cardinality = $this->getCardinality(); Chris@0: return ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) || ($cardinality > 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isLocked() { Chris@0: return $this->locked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setLocked($locked) { Chris@0: $this->locked = $locked; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTargetEntityTypeId() { Chris@0: return $this->entity_type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isQueryable() { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether a field has any data. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the field has data for any entity; FALSE otherwise. Chris@0: */ Chris@0: public function hasData() { Chris@0: return \Drupal::entityManager()->getStorage($this->entity_type)->countFieldData($this, TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements the magic __sleep() method. Chris@0: * Chris@0: * Using the Serialize interface and serialize() / unserialize() methods Chris@0: * breaks entity forms in PHP 5.4. Chris@0: * @todo Investigate in https://www.drupal.org/node/2074253. Chris@0: */ Chris@0: public function __sleep() { Chris@0: // Only serialize necessary properties, excluding those that can be Chris@0: // recalculated. Chris@0: $properties = get_object_vars($this); Chris@0: unset($properties['schema'], $properties['propertyDefinitions'], $properties['original']); Chris@0: return array_keys($properties); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConstraints() { Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConstraint($constraint_name) { Chris@0: return NULL; 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@0: $class = $this->getFieldItemClass(); 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@0: $class = $this->getFieldItemClass(); Chris@0: return $class::mainPropertyName(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getUniqueStorageIdentifier() { Chris@0: return $this->uuid(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper to retrieve the field item class. Chris@0: */ Chris@0: protected function getFieldItemClass() { Chris@0: $type_definition = \Drupal::typedDataManager() Chris@0: ->getDefinition('field_item:' . $this->getType()); Chris@0: return $type_definition['class']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads a field config entity based on the entity type and field name. Chris@0: * Chris@0: * @param string $entity_type_id Chris@0: * ID of the entity type. Chris@0: * @param string $field_name Chris@0: * Name of the field. Chris@0: * Chris@0: * @return static Chris@0: * The field config entity if one exists for the provided field name, Chris@0: * otherwise NULL. Chris@0: */ Chris@0: public static function loadByName($entity_type_id, $field_name) { Chris@0: return \Drupal::entityManager()->getStorage('field_storage_config')->load($entity_type_id . '.' . $field_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDeletable() { Chris@0: // The field storage is not deleted, is configured to be removed when there Chris@0: // are no fields, the field storage has no bundles, and field storages are Chris@0: // not in the process of being deleted. Chris@0: return !$this->deleted && !$this->persist_with_no_fields && count($this->getBundles()) == 0 && !static::$inDeletion; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getIndexes() { Chris@0: return $this->indexes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setIndexes(array $indexes) { Chris@0: $this->indexes = $indexes; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: }