function MediaLibrarySelectForm::viewsForm

Same name in other branches
  1. 8.9.x core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php \Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::viewsForm()
  2. 10 core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php \Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::viewsForm()
  3. 11.x core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php \Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::viewsForm()

Form constructor for the media library select form.

Parameters

array $form: An associative array containing the structure of the form.

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

File

core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php, line 47

Class

MediaLibrarySelectForm
Defines a field that outputs a checkbox and form for selecting media.

Namespace

Drupal\media_library\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {
    $form['#attributes']['class'] = [
        'js-media-library-views-form',
    ];
    // Add an attribute that identifies the media type displayed in the form.
    if (isset($this->view->args[0])) {
        $form['#attributes']['data-drupal-media-type'] = $this->view->args[0];
    }
    // Render checkboxes for all rows.
    $form[$this->options['id']]['#tree'] = TRUE;
    foreach ($this->view->result as $row_index => $row) {
        $entity = $this->getEntity($row);
        $form[$this->options['id']][$row_index] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Select @label', [
                '@label' => $entity->label(),
            ]),
            '#title_display' => 'invisible',
            '#return_value' => $entity->id(),
        ];
    }
    // The selection is persistent across different pages in the media library
    // and populated via JavaScript.
    $selection_field_id = $this->options['id'] . '_selection';
    $form[$selection_field_id] = [
        '#type' => 'hidden',
        '#attributes' => [
            // This is used to identify the hidden field in the form via JavaScript.
'id' => 'media-library-modal-selection',
        ],
    ];
    // @todo Remove in https://www.drupal.org/project/drupal/issues/2504115
    // Currently the default URL for all AJAX form elements is the current URL,
    // not the form action. This causes bugs when this form is rendered from an
    // AJAX path like /views/ajax, which cannot process AJAX form submits.
    $query = $this->view
        ->getRequest()->query
        ->all();
    $query[FormBuilderInterface::AJAX_FORM_REQUEST] = TRUE;
    $query['views_display_id'] = $this->view
        ->getDisplay()->display['id'];
    $form['actions']['submit']['#ajax'] = [
        'url' => Url::fromRoute('media_library.ui'),
        'options' => [
            'query' => $query,
        ],
        'callback' => [
            static::class,
            'updateWidget',
        ],
        // The AJAX system automatically moves focus to the first tabbable
        // element of the modal, so we need to disable refocus on the button.
'disable-refocus' => TRUE,
    ];
    $form['actions']['submit']['#value'] = $this->t('Insert selected');
    $form['actions']['submit']['#button_type'] = 'primary';
    $form['actions']['submit']['#field_id'] = $selection_field_id;
}

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