Chris@0: getName(); Chris@0: $values['entity_type'] = $field_storage->getTargetEntityTypeId(); Chris@0: // The internal property is fieldStorage, not field_storage. Chris@0: unset($values['field_storage']); Chris@0: $values['fieldStorage'] = $field_storage; Chris@0: } Chris@0: else { Chris@0: if (empty($values['field_name'])) { Chris@0: throw new FieldException('Attempt to create a field without a field_name.'); Chris@0: } Chris@0: if (empty($values['entity_type'])) { Chris@0: throw new FieldException("Attempt to create a field '{$values['field_name']}' without an entity_type."); Chris@0: } Chris@0: } Chris@0: // 'bundle' is required in either case. Chris@0: if (empty($values['bundle'])) { Chris@0: throw new FieldException("Attempt to create a field '{$values['field_name']}' without a bundle."); 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 postCreate(EntityStorageInterface $storage) { Chris@0: parent::postCreate($storage); Chris@0: Chris@0: // Validate that we have a valid storage for this field. This throws an Chris@0: // exception if the storage is invalid. Chris@0: $this->getFieldStorageDefinition(); Chris@0: Chris@0: // 'Label' defaults to the field name (mostly useful for fields created in Chris@0: // tests). Chris@0: if (empty($this->label)) { Chris@0: $this->label = $this->getName(); Chris@0: } 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: $entity_manager = \Drupal::entityManager(); Chris@0: $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); Chris@0: Chris@0: $storage_definition = $this->getFieldStorageDefinition(); 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: $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType()); Chris@0: $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; Chris@0: Chris@0: if ($this->isNew()) { Chris@0: // Notify the entity storage. Chris@0: $entity_manager->onFieldDefinitionCreate($this); Chris@0: } Chris@0: else { Chris@0: // Some updates are always disallowed. Chris@0: if ($this->entity_type != $this->original->entity_type) { Chris@0: throw new FieldException("Cannot change an existing field's entity_type."); Chris@0: } Chris@0: if ($this->bundle != $this->original->bundle) { Chris@0: throw new FieldException("Cannot change an existing field's bundle."); Chris@0: } Chris@0: if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) { Chris@0: throw new FieldException("Cannot change an existing field's storage."); Chris@0: } Chris@0: // Notify the entity storage. Chris@0: $entity_manager->onFieldDefinitionUpdate($this, $this->original); Chris@0: } Chris@0: Chris@0: parent::preSave($storage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function calculateDependencies() { Chris@0: parent::calculateDependencies(); Chris@0: // Mark the field_storage_config as a dependency. Chris@0: $this->addDependency('config', $this->getFieldStorageDefinition()->getConfigDependencyName()); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function preDelete(EntityStorageInterface $storage, array $fields) { Chris@0: $state = \Drupal::state(); Chris@0: $entity_type_manager = \Drupal::entityTypeManager(); Chris@0: Chris@0: parent::preDelete($storage, $fields); Chris@0: // Keep the field definitions in the state storage so we can use them Chris@0: // later during field_purge_batch(). Chris@0: $deleted_fields = $state->get('field.field.deleted') ?: []; Chris@0: Chris@0: /** @var \Drupal\field\FieldConfigInterface $field */ Chris@0: foreach ($fields as $field) { Chris@0: // Only mark a field for purging if there is data. Otherwise, just remove Chris@0: // it. Chris@0: $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId()); Chris@0: if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) { Chris@0: $config = $field->toArray(); Chris@0: $config['deleted'] = TRUE; Chris@0: $config['field_storage_uuid'] = $field->getFieldStorageDefinition()->uuid(); Chris@0: $deleted_fields[$field->uuid()] = $config; Chris@0: } Chris@0: } Chris@0: $state->set('field.field.deleted', $deleted_fields); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function postDelete(EntityStorageInterface $storage, array $fields) { Chris@0: // Clear the cache upfront, to refresh the results of getBundles(). Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions(); Chris@0: Chris@0: // Notify the entity storage. Chris@0: foreach ($fields as $field) { Chris@0: if (!$field->deleted) { Chris@0: \Drupal::entityManager()->onFieldDefinitionDelete($field); Chris@0: } Chris@0: } Chris@0: Chris@0: // If this is part of a configuration synchronization then the following Chris@0: // configuration updates are not necessary. Chris@0: $entity = reset($fields); Chris@0: if ($entity->isSyncing()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Delete the associated field storages if they are not used anymore and are Chris@0: // not persistent. Chris@0: $storages_to_delete = []; Chris@0: foreach ($fields as $field) { Chris@0: $storage_definition = $field->getFieldStorageDefinition(); Chris@0: if (!$field->deleted && !$field->isUninstalling() && $storage_definition->isDeletable()) { Chris@0: // Key by field UUID to avoid deleting the same storage twice. Chris@0: $storages_to_delete[$storage_definition->uuid()] = $storage_definition; Chris@0: } Chris@0: } Chris@0: if ($storages_to_delete) { Chris@0: \Drupal::entityManager()->getStorage('field_storage_config')->delete($storages_to_delete); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function linkTemplates() { Chris@0: $link_templates = parent::linkTemplates(); Chris@0: if (\Drupal::moduleHandler()->moduleExists('field_ui')) { Chris@0: $link_templates["{$this->entity_type}-field-edit-form"] = 'entity.field_config.' . $this->entity_type . '_field_edit_form'; Chris@0: $link_templates["{$this->entity_type}-storage-edit-form"] = 'entity.field_config.' . $this->entity_type . '_storage_edit_form'; Chris@0: $link_templates["{$this->entity_type}-field-delete-form"] = 'entity.field_config.' . $this->entity_type . '_field_delete_form'; Chris@0: Chris@0: if (isset($link_templates['config-translation-overview'])) { Chris@0: $link_templates["config-translation-overview.{$this->entity_type}"] = "entity.field_config.config_translation_overview.{$this->entity_type}"; Chris@0: } Chris@0: } Chris@0: return $link_templates; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function urlRouteParameters($rel) { Chris@0: $parameters = parent::urlRouteParameters($rel); Chris@0: $entity_type = \Drupal::entityManager()->getDefinition($this->entity_type); Chris@0: $bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle'; Chris@0: $parameters[$bundle_parameter_key] = $this->bundle; Chris@0: return $parameters; 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 getFieldStorageDefinition() { Chris@0: if (!$this->fieldStorage) { Chris@0: $fields = $this->entityManager()->getFieldStorageDefinitions($this->entity_type); Chris@0: if (!isset($fields[$this->field_name])) { Chris@0: throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}."); Chris@0: } Chris@0: if (!$fields[$this->field_name] instanceof FieldStorageConfigInterface) { Chris@0: throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}."); Chris@0: } Chris@0: $this->fieldStorage = $fields[$this->field_name]; Chris@0: } Chris@0: Chris@0: return $this->fieldStorage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDisplayConfigurable($context) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDisplayOptions($display_context) { Chris@0: // Hide configurable fields by default. Chris@0: return ['region' => 'hidden']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isReadOnly() { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isComputed() { Chris@0: return FALSE; 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 $bundle Chris@0: * Bundle name. 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 Chris@0: * name, otherwise NULL. Chris@0: */ Chris@0: public static function loadByName($entity_type_id, $bundle, $field_name) { Chris@0: return \Drupal::entityManager()->getStorage('field_config')->load($entity_type_id . '.' . $bundle . '.' . $field_name); Chris@0: } Chris@0: Chris@0: }