3 namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\SelectionDisplayBase;
8 use Drupal\views\Views;
9 use Drupal\views\Entity\View as ViewEntity;
12 * Displays current selection in a View.
14 * @EntityBrowserSelectionDisplay(
16 * label = @Translation("View selection display"),
17 * description = @Translation("Use a pre-configured view as selection area."),
18 * acceptPreselection = TRUE,
23 class View extends SelectionDisplayBase {
28 public function defaultConfiguration() {
31 'view_display' => NULL,
32 ] + parent::defaultConfiguration();
38 public function getForm(array &$original_form, FormStateInterface $form_state) {
41 // TODO - do we need better error handling for view and view_display (in case
42 // either of those is nonexistent or display not of correct type)?
43 $storage = &$form_state->getStorage();
44 if (empty($storage['selection_display_view']) || $form_state->isRebuilding()) {
45 $storage['selection_display_view'] = $this->entityTypeManager
47 ->load($this->configuration['view'])
51 // TODO - if there are entities that are selected multiple times this displays
52 // them only once. Reason for that is how SQL and Views work and we probably
53 // can't do much about it.
54 $selected_entities = $form_state->get(['entity_browser', 'selected_entities']);
55 if (!empty($selected_entities)) {
56 $ids = array_map(function (EntityInterface $item) {
58 }, $selected_entities);
59 $storage['selection_display_view']->setArguments([implode(',', $ids)]);
62 $form['view'] = $storage['selection_display_view']->executeDisplay($this->configuration['view_display']);
64 $form['use_selected'] = [
66 '#value' => $this->t('Use selection'),
67 '#name' => 'use_selected',
76 public function submit(array &$form, FormStateInterface $form_state) {
77 if ($form_state->getTriggeringElement()['#name'] == 'use_selected') {
78 $this->selectionDone($form_state);
85 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
88 // Get all views displays.
89 $views = Views::getAllViews();
90 foreach ($views as $view_id => $view) {
91 foreach ($view->get('display') as $display_id => $display) {
92 $options[$view_id . '.' . $display_id] = $this->t('@view : @display', ['@view' => $view->label(), '@display' => $display['display_title']]);
98 '#title' => $this->t('View : View display'),
99 '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
100 '#options' => $options,
102 '#description' => $this->t('View display to use for displaying currently selected items. Do note that to get something usefull out of this display, its first contextual filter should be a filter on the primary identifier field of your entity type (e.g., Node ID, Media ID).'),
111 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
112 $values = $form_state->getValues();
114 if (!empty($values['view'])) {
115 list($view_id, $display_id) = explode('.', $values['view']);
116 $this->configuration['view'] = $view_id;
117 $this->configuration['view_display'] = $display_id;
124 public function calculateDependencies() {
126 if ($this->configuration['view']) {
127 $view = ViewEntity::load($this->configuration['view']);
128 $dependencies[$view->getConfigDependencyKey()] = [$view->getConfigDependencyName()];
130 return $dependencies;