MediaLibrarySelectForm.php in Drupal 8
File
core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
View source
<?php
namespace Drupal\media_library\Plugin\views\field;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media_library\MediaLibraryState;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Render\ViewsRenderPipelineMarkup;
use Drupal\views\ResultRow;
use Symfony\Component\HttpFoundation\Request;
class MediaLibrarySelectForm extends FieldPluginBase {
public function getValue(ResultRow $row, $field = NULL) {
return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
}
public function render(ResultRow $values) {
return ViewsRenderPipelineMarkup::create($this
->getValue($values));
}
public function viewsForm(array &$form, FormStateInterface $form_state) {
$form['#attributes']['class'] = [
'js-media-library-views-form',
];
if (isset($this->view->args[0])) {
$form['#attributes']['data-drupal-media-type'] = $this->view->args[0];
}
$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(),
];
}
$selection_field_id = $this->options['id'] . '_selection';
$form[$selection_field_id] = [
'#type' => 'hidden',
'#attributes' => [
'id' => 'media-library-modal-selection',
],
];
$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;
$form['actions']['submit']['#attributes']['data-disable-refocus'] = 'true';
}
public static function updateWidget(array &$form, FormStateInterface $form_state, Request $request) {
$field_id = $form_state
->getTriggeringElement()['#field_id'];
$selected_ids = $form_state
->getValue($field_id);
$selected_ids = $selected_ids ? array_filter(explode(',', $selected_ids)) : [];
$state = MediaLibraryState::fromRequest($request);
return \Drupal::service('media_library.opener_resolver')
->get($state)
->getSelectionResponse($state, $selected_ids)
->addCommand(new CloseDialogCommand());
}
public function viewsFormValidate(array &$form, FormStateInterface $form_state) {
$selected = array_filter($form_state
->getValue($this->options['id']));
if (empty($selected)) {
$form_state
->setErrorByName('', $this
->t('No items selected.'));
}
}
public function clickSortable() {
return FALSE;
}
}