You are here

public function EntityReferenceItem::storageSettingsForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::storageSettingsForm()

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

bool $has_data: TRUE if the field already has data, FALSE if not.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::storageSettingsForm

1 method overrides EntityReferenceItem::storageSettingsForm()
FileItem::storageSettingsForm in core/modules/file/src/Plugin/Field/FieldType/FileItem.php
Returns a form for the storage-level settings.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 362

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  $element['target_type'] = [
    '#type' => 'select',
    '#title' => t('Type of item to reference'),
    '#default_value' => $this
      ->getSetting('target_type'),
    '#required' => TRUE,
    '#disabled' => $has_data,
    '#size' => 1,
  ];

  // Only allow the field to target entity types that have an ID key. This
  // is enforced in ::propertyDefinitions().
  $entity_type_manager = \Drupal::entityTypeManager();
  $filter = function (string $entity_type_id) use ($entity_type_manager) : bool {
    return $entity_type_manager
      ->getDefinition($entity_type_id)
      ->hasKey('id');
  };
  $options = \Drupal::service('entity_type.repository')
    ->getEntityTypeLabels(TRUE);
  foreach ($options as $group_name => $group) {
    $element['target_type']['#options'][$group_name] = array_filter($group, $filter, ARRAY_FILTER_USE_KEY);
  }
  return $element;
}