You are here

public function MediaLibraryWidget::settingsForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::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

File

core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php, line 176

Class

MediaLibraryWidget
Plugin implementation of the 'media_library_widget' widget.

Namespace

Drupal\media_library\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = [];
  $media_type_ids = $this
    ->getAllowedMediaTypeIdsSorted();
  if (count($media_type_ids) <= 1) {
    return $elements;
  }
  $elements['media_types'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Tab order'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight',
      ],
    ],
    '#value_callback' => [
      static::class,
      'setMediaTypesValue',
    ],
  ];
  $media_types = $this->entityTypeManager
    ->getStorage('media_type')
    ->loadMultiple($media_type_ids);
  $weight = 0;
  foreach ($media_types as $media_type_id => $media_type) {
    $label = $media_type
      ->label();
    $elements['media_types'][$media_type_id] = [
      'label' => [
        '#markup' => $label,
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => t('Weight for @title', [
          '@title' => $label,
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $weight,
        '#attributes' => [
          'class' => [
            'weight',
          ],
        ],
      ],
      '#weight' => $weight,
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
    $weight++;
  }
  return $elements;
}