6d43421c3fd8c7ab93df27be296d467e532cc1d8
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / EntityBrowser / SelectionDisplay / View.php
1 <?php
2
3 namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay;
4
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;
10
11 /**
12  * Displays current selection in a View.
13  *
14  * @EntityBrowserSelectionDisplay(
15  *   id = "view",
16  *   label = @Translation("View selection display"),
17  *   description = @Translation("Use a pre-configured view as selection area."),
18  *   acceptPreselection = TRUE,
19  *   provider = "views",
20  *   js_commands = FALSE
21  * )
22  */
23 class View extends SelectionDisplayBase {
24
25   /**
26    * {@inheritdoc}
27    */
28   public function defaultConfiguration() {
29     return [
30       'view' => NULL,
31       'view_display' => NULL,
32     ] + parent::defaultConfiguration();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function getForm(array &$original_form, FormStateInterface $form_state) {
39     $form = [];
40
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
46         ->getStorage('view')
47         ->load($this->configuration['view'])
48         ->getExecutable();
49     }
50
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) {
57         return $item->id();
58       }, $selected_entities);
59       $storage['selection_display_view']->setArguments([implode(',', $ids)]);
60     }
61
62     $form['view'] = $storage['selection_display_view']->executeDisplay($this->configuration['view_display']);
63
64     $form['use_selected'] = [
65       '#type' => 'submit',
66       '#value' => $this->t('Use selection'),
67       '#name' => 'use_selected',
68     ];
69
70     return $form;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function submit(array &$form, FormStateInterface $form_state) {
77     if ($form_state->getTriggeringElement()['#name'] == 'use_selected') {
78       $this->selectionDone($form_state);
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
86     $options = [];
87
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']]);
93       }
94     }
95
96     $form['view'] = [
97       '#type' => 'select',
98       '#title' => $this->t('View : View display'),
99       '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
100       '#options' => $options,
101       '#required' => TRUE,
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).'),
103     ];
104
105     return $form;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
112     $values = $form_state->getValues();
113
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;
118     }
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function calculateDependencies() {
125     $dependencies = [];
126     if ($this->configuration['view']) {
127       $view = ViewEntity::load($this->configuration['view']);
128       $dependencies[$view->getConfigDependencyKey()] = [$view->getConfigDependencyName()];
129     }
130     return $dependencies;
131   }
132
133 }