92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
*/
|
|
|
|
use Drupal\field\FieldConfigInterface;
|
|
use Drupal\field\FieldStorageConfigInterface;
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
|
|
/*
|
|
* Load all public Field API functions. Drupal currently has no
|
|
* mechanism for auto-loading core APIs, so we have to load them on
|
|
* every page request.
|
|
*/
|
|
require_once __DIR__ . '/field.purge.inc';
|
|
|
|
/**
|
|
* Assembles a partial entity structure with initial IDs.
|
|
*
|
|
* @param object $ids
|
|
* An object with the properties entity_type (required), entity_id (required),
|
|
* revision_id (optional) and bundle (optional).
|
|
*
|
|
* @return \Drupal\Core\Entity\EntityInterface
|
|
* An entity, initialized with the provided IDs.
|
|
*/
|
|
function _field_create_entity_from_ids($ids) {
|
|
$id_properties = [];
|
|
$entity_type = \Drupal::entityTypeManager()->getDefinition($ids->entity_type);
|
|
if ($id_key = $entity_type->getKey('id')) {
|
|
$id_properties[$id_key] = $ids->entity_id;
|
|
}
|
|
if (isset($ids->revision_id) && $revision_key = $entity_type->getKey('revision')) {
|
|
$id_properties[$revision_key] = $ids->revision_id;
|
|
}
|
|
if (isset($ids->bundle) && $bundle_key = $entity_type->getKey('bundle')) {
|
|
$id_properties[$bundle_key] = $ids->bundle;
|
|
}
|
|
return \Drupal::entityTypeManager()
|
|
->getStorage($ids->entity_type)
|
|
->create($id_properties);
|
|
}
|
|
|
|
/**
|
|
* Entity form builder for field config edit form.
|
|
*
|
|
* @param string $entity_type_id
|
|
* The entity type identifier.
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
|
* The entity updated with the submitted values.
|
|
* @param array $form
|
|
* The complete form array.
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
|
* The current state of the form.
|
|
*
|
|
* @see \Drupal\field_ui\Form\FieldConfigEditForm::form
|
|
* @see \Drupal\field_ui\Form\FieldConfigEditForm::copyFormValuesToEntity
|
|
*/
|
|
function field_form_field_config_edit_form_entity_builder($entity_type_id, $entity, &$form, FormStateInterface $form_state): void {
|
|
assert($entity instanceof FieldConfigInterface);
|
|
$previous_field_storage = $form_state->getFormObject()->getEntity()->getFieldStorageDefinition();
|
|
assert($previous_field_storage instanceof FieldStorageConfigInterface);
|
|
|
|
// Act on all sub-types of the entity_reference field type.
|
|
/** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
|
|
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
|
|
$item_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
|
|
$class = $field_type_manager->getPluginClass($entity->getFieldStorageDefinition()->getType());
|
|
if ($class !== $item_class && !is_subclass_of($class, $item_class)) {
|
|
return;
|
|
}
|
|
|
|
// Update handler settings when target_type is changed.
|
|
if ($entity->getFieldStorageDefinition()->getSetting('target_type') !== $previous_field_storage->getSetting('target_type')) {
|
|
// @see field_field_storage_config_update().
|
|
$entity->setSetting('handler_settings', []);
|
|
// @see field_field_config_presave().
|
|
\Drupal::moduleHandler()->invoke('field', 'field_config_create', [$entity]);
|
|
|
|
// Store updated settings in form state so that the form state can be copied
|
|
// directly to the entity.
|
|
$form_state->setValue('settings', $entity->getSettings());
|
|
|
|
// Unset user input for the settings because they are not valid after the
|
|
// target type has changed.
|
|
$user_input = $form_state->getUserInput();
|
|
unset($user_input['settings']);
|
|
$form_state->setUserInput($user_input);
|
|
}
|
|
}
|