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
|
|
|
* ) |
21
|
|
|
*/ |
22
|
|
|
class View extends SelectionDisplayBase { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function defaultConfiguration() { |
28
|
|
|
return array( |
29
|
|
|
'view' => NULL, |
30
|
|
|
'view_display' => NULL, |
31
|
|
|
) + parent::defaultConfiguration(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state) { |
38
|
|
|
$form = []; |
39
|
|
|
|
40
|
|
|
// TODO - do we need better error handling for view and view_display (in case |
41
|
|
|
// either of those is nonexistent or display not of correct type)? |
42
|
|
|
$storage = &$form_state->getStorage(); |
43
|
|
|
if (empty($storage['selection_display_view']) || $form_state->isRebuilding()) { |
44
|
|
|
$storage['selection_display_view'] = $this->entityTypeManager |
45
|
|
|
->getStorage('view') |
46
|
|
|
->load($this->configuration['view']) |
47
|
|
|
->getExecutable(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// TODO - if there are entities that are selected multiple times this displays |
51
|
|
|
// them only once. Reason for that is how SQL and Views work and we probably |
52
|
|
|
// can't do much about it. |
53
|
|
|
$selected_entities = $form_state->get(['entity_browser', 'selected_entities']); |
54
|
|
|
if (!empty($selected_entities)) { |
55
|
|
|
$ids = array_map(function (EntityInterface $item) { |
56
|
|
|
return $item->id(); |
57
|
|
|
}, $selected_entities); |
58
|
|
|
$storage['selection_display_view']->setArguments([implode(',', $ids)]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$form['view'] = $storage['selection_display_view']->executeDisplay($this->configuration['view_display']); |
62
|
|
|
|
63
|
|
|
$form['use_selected'] = array( |
64
|
|
|
'#type' => 'submit', |
65
|
|
|
'#value' => $this->t('Use selection'), |
66
|
|
|
'#name' => 'use_selected', |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return $form; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function submit(array &$form, FormStateInterface $form_state) { |
76
|
|
|
if ($form_state->getTriggeringElement()['#name'] == 'use_selected') { |
77
|
|
|
$this->selectionDone($form_state); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
85
|
|
|
$options = []; |
86
|
|
|
|
87
|
|
|
// Get all views displays. |
88
|
|
|
$views = Views::getAllViews(); |
89
|
|
|
foreach ($views as $view_id => $view) { |
90
|
|
|
foreach ($view->get('display') as $display_id => $display) { |
91
|
|
|
$options[$view_id . '.' . $display_id] = $this->t('@view : @display', array('@view' => $view->label(), '@display' => $display['display_title'])); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$form['view'] = [ |
96
|
|
|
'#type' => 'select', |
97
|
|
|
'#title' => $this->t('View : View display'), |
98
|
|
|
'#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'], |
99
|
|
|
'#options' => $options, |
100
|
|
|
'#required' => TRUE, |
101
|
|
|
'#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).'), |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
return $form; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
111
|
|
|
$values = $form_state->getValues(); |
112
|
|
|
|
113
|
|
View Code Duplication |
if (!empty($values['view'])) { |
|
|
|
|
114
|
|
|
list($view_id, $display_id) = explode('.', $values['view']); |
115
|
|
|
$this->configuration['view'] = $view_id; |
116
|
|
|
$this->configuration['view_display'] = $display_id; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function calculateDependencies() { |
|
|
|
|
124
|
|
|
$dependencies = []; |
125
|
|
|
if ($this->configuration['view']) { |
126
|
|
|
$view = ViewEntity::load($this->configuration['view']); |
127
|
|
|
$dependencies[$view->getConfigDependencyKey()] = [$view->getConfigDependencyName()]; |
128
|
|
|
} |
129
|
|
|
return $dependencies; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.