You are here

public function EntityReferenceItem::fieldSettingsForm in Drupal 10

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::fieldSettingsForm()
  2. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm()

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

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.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

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

File

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

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $field = $form_state
    ->getFormObject()
    ->getEntity();

  // Get all selection plugins for this entity type.
  $selection_plugins = \Drupal::service('plugin.manager.entity_reference_selection')
    ->getSelectionGroups($this
    ->getSetting('target_type'));
  $handlers_options = [];
  foreach (array_keys($selection_plugins) as $selection_group_id) {

    // We only display base plugins (e.g. 'default', 'views', ...) and not
    // entity type specific plugins (e.g. 'default:node', 'default:user',
    // ...).
    if (array_key_exists($selection_group_id, $selection_plugins[$selection_group_id])) {
      $handlers_options[$selection_group_id] = Html::escape($selection_plugins[$selection_group_id][$selection_group_id]['label']);
    }
    elseif (array_key_exists($selection_group_id . ':' . $this
      ->getSetting('target_type'), $selection_plugins[$selection_group_id])) {
      $selection_group_plugin = $selection_group_id . ':' . $this
        ->getSetting('target_type');
      $handlers_options[$selection_group_plugin] = Html::escape($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']);
    }
  }
  $form = [
    '#type' => 'container',
    '#process' => [
      [
        static::class,
        'fieldSettingsAjaxProcess',
      ],
    ],
    '#element_validate' => [
      [
        static::class,
        'fieldSettingsFormValidate',
      ],
    ],
  ];
  $form['handler'] = [
    '#type' => 'details',
    '#title' => t('Reference type'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#process' => [
      [
        static::class,
        'formProcessMergeParent',
      ],
    ],
  ];
  $form['handler']['handler'] = [
    '#type' => 'select',
    '#title' => t('Reference method'),
    '#options' => $handlers_options,
    '#default_value' => $field
      ->getSetting('handler'),
    '#required' => TRUE,
    '#ajax' => TRUE,
    '#limit_validation_errors' => [],
  ];
  $form['handler']['handler_submit'] = [
    '#type' => 'submit',
    '#value' => t('Change handler'),
    '#limit_validation_errors' => [],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#submit' => [
      [
        static::class,
        'settingsAjaxSubmit',
      ],
    ],
  ];
  $form['handler']['handler_settings'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'entity_reference-settings',
      ],
    ],
  ];
  $handler = \Drupal::service('plugin.manager.entity_reference_selection')
    ->getSelectionHandler($field);
  $form['handler']['handler_settings'] += $handler
    ->buildConfigurationForm([], $form_state);
  return $form;
}