function MediaLibrarySelectForm::viewsForm

Same name in other branches
  1. 9 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',
        ],
    ];
    $form['actions']['submit']['#value'] = $this->t('Insert selected');
    $form['actions']['submit']['#button_type'] = 'primary';
    $form['actions']['submit']['#field_id'] = $selection_field_id;
    // By default, the AJAX system tries to move the focus back to the element
    // that triggered the AJAX request. Since the media library is closed after
    // clicking the select button, the focus can't be moved back. We need to set
    // the 'data-disable-refocus' attribute to prevent the AJAX system from
    // moving focus to a random element. The select button triggers an update in
    // the opener, and the opener should be responsible for moving the focus. An
    // example of this can be seen in MediaLibraryWidget::updateWidget().
    // @see \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::updateWidget()
    $form['actions']['submit']['#attributes']['data-disable-refocus'] = 'true';
}

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