FieldConfig.php in Drupal 9
File
core/modules/field/src/Entity/FieldConfig.php
View source
<?php
namespace Drupal\field\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\FieldableEntityStorageInterface;
use Drupal\Core\Field\FieldConfigBase;
use Drupal\Core\Field\FieldException;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field\FieldConfigInterface;
class FieldConfig extends FieldConfigBase implements FieldConfigInterface {
protected $deleted = FALSE;
protected $fieldStorage;
public function __construct(array $values, $entity_type = 'field_config') {
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();
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("Attempt to create a field '{$values['field_name']}' without an entity_type.");
}
}
if (empty($values['bundle'])) {
throw new FieldException("Attempt to create a field '{$values['field_name']}' without a bundle.");
}
parent::__construct($values, $entity_type);
}
public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
$this
->getFieldStorageDefinition();
if (empty($this->label)) {
$this->label = $this
->getName();
}
}
public function preSave(EntityStorageInterface $storage) {
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$storage_definition = $this
->getFieldStorageDefinition();
$default_settings = $field_type_manager
->getDefaultFieldSettings($storage_definition
->getType());
$this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
if ($this
->isNew()) {
\Drupal::service('field_definition.listener')
->onFieldDefinitionCreate($this);
}
else {
if ($this->entity_type != $this->original->entity_type) {
throw new FieldException("Cannot change an existing field's entity_type.");
}
if ($this->bundle != $this->original->bundle) {
throw new FieldException("Cannot change an existing field's bundle.");
}
if ($storage_definition
->uuid() != $this->original
->getFieldStorageDefinition()
->uuid()) {
throw new FieldException("Cannot change an existing field's storage.");
}
\Drupal::service('field_definition.listener')
->onFieldDefinitionUpdate($this, $this->original);
}
parent::preSave($storage);
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('config', $this
->getFieldStorageDefinition()
->getConfigDependencyName());
return $this;
}
public static function preDelete(EntityStorageInterface $storage, array $fields) {
$deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
$entity_type_manager = \Drupal::entityTypeManager();
parent::preDelete($storage, $fields);
foreach ($fields as $field) {
$target_entity_storage = $entity_type_manager
->getStorage($field
->getTargetEntityTypeId());
if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage
->countFieldData($field
->getFieldStorageDefinition(), TRUE)) {
$field = clone $field;
$field->deleted = TRUE;
$field->fieldStorage = NULL;
$deleted_fields_repository
->addFieldDefinition($field);
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $fields) {
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
foreach ($fields as $field) {
if (!$field->deleted) {
\Drupal::service('field_definition.listener')
->onFieldDefinitionDelete($field);
}
}
$entity = reset($fields);
if ($entity
->isSyncing()) {
return;
}
$storages_to_delete = [];
foreach ($fields as $field) {
$storage_definition = $field
->getFieldStorageDefinition();
if (!$field->deleted && !$field
->isUninstalling() && $storage_definition
->isDeletable()) {
$storages_to_delete[$storage_definition
->uuid()] = $storage_definition;
}
}
if ($storages_to_delete) {
\Drupal::entityTypeManager()
->getStorage('field_storage_config')
->delete($storages_to_delete);
}
}
protected function linkTemplates() {
$link_templates = parent::linkTemplates();
if (\Drupal::moduleHandler()
->moduleExists('field_ui')) {
$link_templates["{$this->entity_type}-field-edit-form"] = 'entity.field_config.' . $this->entity_type . '_field_edit_form';
$link_templates["{$this->entity_type}-storage-edit-form"] = 'entity.field_config.' . $this->entity_type . '_storage_edit_form';
$link_templates["{$this->entity_type}-field-delete-form"] = 'entity.field_config.' . $this->entity_type . '_field_delete_form';
if (isset($link_templates['config-translation-overview'])) {
$link_templates["config-translation-overview.{$this->entity_type}"] = "entity.field_config.config_translation_overview.{$this->entity_type}";
}
}
return $link_templates;
}
protected function urlRouteParameters($rel) {
$parameters = parent::urlRouteParameters($rel);
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this->entity_type);
$bundle_parameter_key = $entity_type
->getBundleEntityType() ?: 'bundle';
$parameters[$bundle_parameter_key] = $this->bundle;
return $parameters;
}
public function isDeleted() {
return $this->deleted;
}
public function getFieldStorageDefinition() {
if (!$this->fieldStorage) {
$field_storage_definition = NULL;
$field_storage_definitions = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions($this->entity_type);
if (isset($field_storage_definitions[$this->field_name])) {
$field_storage_definition = $field_storage_definitions[$this->field_name];
}
elseif ($this->deleted) {
$deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')
->getFieldStorageDefinitions();
foreach ($deleted_storage_definitions as $deleted_storage_definition) {
if ($deleted_storage_definition
->getName() === $this->field_name) {
$field_storage_definition = $deleted_storage_definition;
}
}
}
if (!$field_storage_definition) {
throw new FieldException("Attempt to create a field {$this->field_name} that does not exist on entity type {$this->entity_type}.");
}
if (!$field_storage_definition instanceof FieldStorageConfigInterface) {
throw new FieldException("Attempt to create a configurable field of non-configurable field storage {$this->field_name}.");
}
$this->fieldStorage = $field_storage_definition;
}
return $this->fieldStorage;
}
public function isDisplayConfigurable($context) {
return TRUE;
}
public function getDisplayOptions($display_context) {
return [
'region' => 'hidden',
];
}
public function isReadOnly() {
return FALSE;
}
public function isComputed() {
return FALSE;
}
public function getUniqueIdentifier() {
return $this
->uuid();
}
public static function loadByName($entity_type_id, $bundle, $field_name) {
return \Drupal::entityTypeManager()
->getStorage('field_config')
->load($entity_type_id . '.' . $bundle . '.' . $field_name);
}
}