summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/field/field.module
blob: ee5db3615262c740ba54e8487ca472d902063c4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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);
  }
}