function MediaLibraryWidget::settingsForm

Same name in other branches
  1. 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::settingsForm()
  2. 8.9.x core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::settingsForm()
  3. 11.x core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::settingsForm()

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' => $this->t('Weight for @title', [
                    '@title' => $label,
                ]),
                '#title_display' => 'invisible',
                '#default_value' => $weight,
                '#attributes' => [
                    'class' => [
                        'weight',
                    ],
                ],
            ],
            '#weight' => $weight,
            '#attributes' => [
                'class' => [
                    'draggable',
                ],
            ],
        ];
        $weight++;
    }
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.