Verified Commit 22d7c626 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3572173 Deprecate field.module remaining functions

By: claudiu.cristea
By: nicxvan
By: amateescu
By: berdir
parent e644f952
Loading
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -1518,4 +1518,32 @@ protected function resetRevisionCache(array $revision_ids): void {
    }
  }

  /**
   * Assembles a partial entity structure with initial IDs.
   *
   * @param array{entity_id: int|string, revision_id?: int|string, bundle?: string} $entity_identifiers
   *   Array with the following keys:
   *   - entity_id (required),
   *   - revision_id (optional),
   *   - bundle (optional).
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface
   *   An entity object, initialized with the provided IDs.
   *
   * @internal
   */
  public function createEntityFromIds(array $entity_identifiers): ContentEntityInterface {
    $id_properties = [];
    if ($id_key = $this->entityType->getKey('id')) {
      $id_properties[$id_key] = $entity_identifiers['entity_id'];
    }
    if (isset($entity_identifiers['revision_id']) && $revision_key = $this->entityType->getKey('revision')) {
      $id_properties[$revision_key] = $entity_identifiers['revision_id'];
    }
    if (isset($entity_identifiers['bundle']) && $bundle_key = $this->entityType->getKey('bundle')) {
      $id_properties[$bundle_key] = $entity_identifiers['bundle'];
    }
    return $this->create($id_properties);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -1831,7 +1831,7 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit
          $item_row['entity_type'] = $this->entityTypeId;
          // @todo Replace this by an entity object created via an entity
          //   factory. https://www.drupal.org/node/1867228.
          $entities[$item_row['revision_id']] = _field_create_entity_from_ids((object) $item_row);
          $entities[$item_row['revision_id']] = $this->createEntityFromIds($item_row);
        }
        $item = [];
        foreach ($column_map as $db_column => $field_column) {
+11 −2
Original line number Diff line number Diff line
@@ -24,8 +24,14 @@
 *
 * @return \Drupal\Core\Entity\FieldableEntityInterface
 *   An entity, initialized with the provided IDs.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. There is
 *   no replacement.
 *
 * @see https://www.drupal.org/node/3566774
 */
function _field_create_entity_from_ids($ids) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  $id_properties = [];
  $entity_type = \Drupal::entityTypeManager()->getDefinition($ids->entity_type);
  if ($id_key = $entity_type->getKey('id')) {
@@ -55,10 +61,13 @@ function _field_create_entity_from_ids($ids) {
 * @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
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. There is
 *   no replacement.
 *
 * @see https://www.drupal.org/node/3566774
 */
function field_form_field_config_edit_form_entity_builder($entity_type_id, $entity, &$form, FormStateInterface $form_state): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  assert($entity instanceof FieldConfigInterface);
  $previous_field_storage = $form_state->getFormObject()->getEntity()->getFieldStorageDefinition();
  assert($previous_field_storage instanceof FieldStorageConfigInterface);
+71 −4
Original line number Diff line number Diff line
@@ -2,10 +2,12 @@

namespace Drupal\field_ui\Form;

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
@@ -14,6 +16,7 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Render\Element;
@@ -23,6 +26,7 @@
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -56,13 +60,25 @@ class FieldConfigEditForm extends EntityForm {
   */
  protected string $bundle;

  /**
   * The field-type plugin manager service.
   */
  protected PluginManagerInterface $fieldTypePluginManager;

  public function __construct(
    protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
    protected TypedDataManagerInterface $typedDataManager,
    protected EntityDisplayRepositoryInterface $entityDisplayRepository,
    protected PrivateTempStore $tempStore,
    protected ElementInfoManagerInterface $elementInfo,
  ) {}
    ?PluginManagerInterface $field_type_plugin_manager = NULL,
  ) {
    if (!$field_type_plugin_manager) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $field_type_plugin_manager argument is deprecated in drupal:11.4.0 and it will be required in drupal:12.0.0. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
      $field_type_plugin_manager = \Drupal::service('plugin.manager.field.field_type');
    }
    $this->fieldTypePluginManager = $field_type_plugin_manager;
  }

  /**
   * {@inheritdoc}
@@ -74,6 +90,7 @@ public static function create(ContainerInterface $container) {
      $container->get('entity_display.repository'),
      $container->get('tempstore.private')->get('field_ui'),
      $container->get('plugin.manager.element_info'),
      $container->get('plugin.manager.field.field_type'),
    );
  }

@@ -92,7 +109,7 @@ public function getFormId() {
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['#entity_builders'][] = 'field_form_field_config_edit_form_entity_builder';
    $form['#entity_builders']['config_edit_form'] = '::entityFormEntityBuild';

    $field_storage = $this->entity->getFieldStorageDefinition();
    $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->entity->getTargetEntityTypeId());
@@ -137,7 +154,7 @@ public function form(array $form, FormStateInterface $form_state) {
    ];

    // Create an arbitrary entity object (used by the 'default value' widget).
    $ids = (object) [
    $entity_identifiers = [
      'entity_type' => $this->entity->getTargetEntityTypeId(),
      'bundle' => $this->entity->getTargetBundle(),
      'entity_id' => NULL,
@@ -167,7 +184,12 @@ public function form(array $form, FormStateInterface $form_state) {
    $subform_state = SubformState::createForSubform($form['field_storage']['subform'], $form, $form_state, $field_storage_form);
    $form['field_storage']['subform'] = $field_storage_form->buildForm($form['field_storage']['subform'], $subform_state, $this->entity);

    $form['#entity'] = _field_create_entity_from_ids($ids);
    $target_entity_storage = $this->entityTypeManager->getStorage($this->entity->getTargetEntityTypeId());
    if (!$target_entity_storage instanceof ContentEntityStorageInterface) {
      return $form;
    }

    $form['#entity'] = $target_entity_storage->createEntityFromIds($entity_identifiers);
    $items = $this->getTypedData($this->entity, $form['#entity']);
    $item = $items->first() ?: $items->appendItem();

@@ -232,6 +254,51 @@ public function form(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * Entity form builder for the field config edit form.
   *
   * @param string $entity_type_id
   *   The entity type identifier.
   * @param \Drupal\field\FieldConfigInterface $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 static::form
   * @see static::copyFormValuesToEntity
   */
  public function entityFormEntityBuild(string $entity_type_id, FieldConfigInterface $entity, array &$form, FormStateInterface &$form_state): void {
    $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.
    $item_class = EntityReferenceItem::class;
    $class = $this->fieldTypePluginManager->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 \Drupal\options\Hook\OptionsHooks::fieldStorageConfigUpdate()
      $entity->setSetting('handler_settings', []);
      // @see \Drupal\field\Hook\FieldHooks::fieldConfigPresave()
      $this->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);
    }
  }

  /**
   * {@inheritdoc}
   */
+8 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\field_ui\Form;

use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
@@ -105,12 +106,17 @@ public function form(array $form, FormStateInterface $form_state) {
    ];
    // Create an arbitrary entity object, so that we can have an instantiated
    // FieldItem.
    $ids = (object) [
    $target_entity_storage = $this->entityTypeManager->getStorage($this->entity->getTargetEntityTypeId());
    if (!$target_entity_storage instanceof ContentEntityStorageInterface) {
      return $form;
    }

    $entity_identifiers = [
      'entity_type' => $form_state->get('entity_type_id'),
      'bundle' => $form_state->get('bundle'),
      'entity_id' => NULL,
    ];
    $entity = _field_create_entity_from_ids($ids);
    $entity = $target_entity_storage->createEntityFromIds($entity_identifiers);
    if (!$this->entity->isNew()) {
      $items = $entity->get($this->entity->getName());
    }
Loading