You are here

public function EntityReferenceBrowserWidget::settingsForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

1 call to EntityReferenceBrowserWidget::settingsForm()
FileBrowserWidget::settingsForm in src/Plugin/Field/FieldWidget/FileBrowserWidget.php
Returns a form to configure settings for the widget.
1 method overrides EntityReferenceBrowserWidget::settingsForm()
FileBrowserWidget::settingsForm in src/Plugin/Field/FieldWidget/FileBrowserWidget.php
Returns a form to configure settings for the widget.

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php, line 150

Class

EntityReferenceBrowserWidget
Plugin implementation of the 'entity_reference' widget for entity browser.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  $browsers = [];

  /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
  foreach ($this->entityTypeManager
    ->getStorage('entity_browser')
    ->loadMultiple() as $browser) {
    $browsers[$browser
      ->id()] = $browser
      ->label();
  }
  $element['entity_browser'] = [
    '#title' => $this
      ->t('Entity browser'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('entity_browser'),
    '#options' => $browsers,
  ];
  $target_type = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  $entity_type = $this->entityTypeManager
    ->getStorage($target_type)
    ->getEntityType();
  $displays = [];
  foreach ($this->fieldDisplayManager
    ->getDefinitions() as $id => $definition) {
    if ($this->fieldDisplayManager
      ->createInstance($id)
      ->isApplicable($entity_type)) {
      $displays[$id] = $definition['label'];
    }
  }
  $id = Html::getId($this->fieldDefinition
    ->getName()) . '-field-widget-display-settings-ajax-wrapper-' . md5($this->fieldDefinition
    ->getUniqueIdentifier());
  $element['field_widget_display'] = [
    '#title' => $this
      ->t('Entity display plugin'),
    '#type' => 'radios',
    '#default_value' => $this
      ->getSetting('field_widget_display'),
    '#options' => $displays,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'updateFieldWidgetDisplaySettings',
      ],
      'wrapper' => $id,
    ],
    '#limit_validation_errors' => [],
  ];
  if ($this
    ->getSetting('field_widget_display')) {
    $element['field_widget_display_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Entity display plugin configuration'),
      '#open' => TRUE,
      '#prefix' => '<div id="' . $id . '">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    ];
    $element['field_widget_display_settings'] += $this->fieldDisplayManager
      ->createInstance($form_state
      ->getValue([
      'fields',
      $this->fieldDefinition
        ->getName(),
      'settings_edit_form',
      'settings',
      'field_widget_display',
    ], $this
      ->getSetting('field_widget_display')), $form_state
      ->getValue([
      'fields',
      $this->fieldDefinition
        ->getName(),
      'settings_edit_form',
      'settings',
      'field_widget_display_settings',
    ], $this
      ->getSetting('field_widget_display_settings')) + [
      'entity_type' => $this->fieldDefinition
        ->getFieldStorageDefinition()
        ->getSetting('target_type'),
    ])
      ->settingsForm($form, $form_state);
  }
  $edit_button_access = TRUE;
  if ($entity_type
    ->id() == 'file') {

    // For entities of type "file", it only makes sense to have the edit
    // button if the module "file_entity" is present.
    $edit_button_access = $this->moduleHandler
      ->moduleExists('file_entity');
  }
  $element['field_widget_edit'] = [
    '#title' => $this
      ->t('Display Edit button'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('field_widget_edit'),
    '#access' => $edit_button_access,
  ];
  $element['field_widget_remove'] = [
    '#title' => $this
      ->t('Display Remove button'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('field_widget_remove'),
  ];
  $element['field_widget_replace'] = [
    '#title' => $this
      ->t('Display Replace button'),
    '#description' => $this
      ->t('This button will only be displayed if there is a single entity in the current selection.'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('field_widget_replace'),
  ];
  $element['open'] = [
    '#title' => $this
      ->t('Show widget details as open by default'),
    '#description' => $this
      ->t('If marked, the fieldset container that wraps the browser on the entity form will be loaded initially expanded.'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('open'),
  ];
  $element['selection_mode'] = [
    '#title' => $this
      ->t('Selection mode'),
    '#description' => $this
      ->t('Determines how selection in entity browser will be handled. Will selection be appended/prepended or it will be replaced in case of editing.'),
    '#type' => 'select',
    '#options' => EntityBrowserElement::getSelectionModeOptions(),
    '#default_value' => $this
      ->getSetting('selection_mode'),
  ];
  $element['#element_validate'] = [
    [
      get_class($this),
      'validateSettingsForm',
    ],
  ];
  return $element;
}