View source
<?php
namespace Drupal\entity_browser\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\style\Table;
use Drupal\views\ResultRow;
use Drupal\views\Render\ViewsRenderPipelineMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
class SelectForm extends FieldPluginBase {
protected $currentRequest;
protected $selectionStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, KeyValueStoreExpirableInterface $selection_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentRequest = $request_stack
->getCurrentRequest();
$this->selectionStorage = $selection_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack'), $container
->get('entity_browser.selection_storage'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['use_field_cardinality'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['use_field_cardinality'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use field cardinality'),
'#default_value' => $this->options['use_field_cardinality'],
'#description' => $this
->t('If the view is used in a context where cardinality is 1, use radios instead of checkboxes.'),
];
}
public function getRowId(ResultRow $row) {
$entity = $this
->getEntity($row);
return $entity
->getEntityTypeId() . ':' . $entity
->id();
}
public function render(ResultRow $values) {
return ViewsRenderPipelineMarkup::create('<!--form-item-' . $this->options['id'] . '--' . $this
->getRowId($values) . '-->');
}
public function preRender(&$values) {
parent::preRender($values);
if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof Table) {
$this->options['element_label_class'] .= 'select-all';
$this->options['label'] = '';
}
}
public function viewsForm(&$render) {
if (!empty($this->view->result)) {
$render[$this->options['id']]['#tree'] = TRUE;
$render[$this->options['id']]['#printed'] = TRUE;
$cardinality = $this
->getCardinality();
$use_field_cardinality = $this->options['use_field_cardinality'];
$use_radios = $use_field_cardinality && $cardinality === 1;
foreach ($this->view->result as $row) {
$value = $this
->getRowId($row);
$render[$this->options['id']][$value] = [
'#type' => 'checkbox',
'#title' => $this
->t('Select this item'),
'#title_display' => 'invisible',
'#return_value' => $value,
'#attributes' => [
'name' => "entity_browser_select[{$value}]",
],
'#default_value' => NULL,
];
if ($use_radios) {
$render[$this->options['id']][$value]['#type'] = 'radio';
$render[$this->options['id']][$value]['#attributes'] = [
'name' => "entity_browser_select",
];
$render[$this->options['id']][$value]['#parents'] = [
'entity_browser_select',
];
$render[$this->options['id']][$value]['#value'] = FALSE;
}
}
$render['entity_browser_select_form_metadata'] = [
'cardinality' => [
'#type' => 'hidden',
'#value' => $cardinality,
],
'use_field_cardinality' => [
'#type' => 'hidden',
'#value' => (int) $use_field_cardinality,
],
'#tree' => TRUE,
];
}
$render['view']['#cache']['tags'][] = 'config:entity_browser.settings';
}
protected function getWidgetContext() {
if ($this->currentRequest->query
->has('uuid')) {
$uuid = $this->currentRequest->query
->get('uuid');
if ($storage = $this->selectionStorage
->get($uuid)) {
if (isset($storage['widget_context'])) {
return $storage['widget_context'];
}
}
}
return [];
}
protected function getCardinality() {
$widget_context = $this
->getWidgetContext();
if (!empty($widget_context['cardinality'])) {
return $widget_context['cardinality'];
}
return NULL;
}
public function query() {
}
public function clickSortable() {
return FALSE;
}
}