|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_media\Plugin\Block; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\NestedArray; |
|
6
|
|
|
use Drupal\Core\Block\BlockBase; |
|
7
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
8
|
|
|
use Drupal\Core\Form\SubformStateInterface; |
|
9
|
|
|
use Drupal\media\Entity\Media; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Provides the "Media Embed" block. |
|
13
|
|
|
* |
|
14
|
|
|
* Copied wholesale from Content Browser. |
|
15
|
|
|
* @todo Make a contrib module to provide blocks for every Entity Browser... |
|
16
|
|
|
* |
|
17
|
|
|
* @Block( |
|
18
|
|
|
* id = "media_embed", |
|
19
|
|
|
* admin_label = @Translation("Media Embed"), |
|
20
|
|
|
* category = @Translation("Embed") |
|
21
|
|
|
* ) |
|
22
|
|
|
*/ |
|
23
|
|
|
class MediaEmbedBlock extends BlockBase { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The number of times this block allows rendering the same entity. |
|
27
|
|
|
* |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
const RECURSIVE_RENDER_LIMIT = 2; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* An array of counters for the recursive rendering protection. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $recursiveRenderDepth = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function defaultConfiguration() { |
|
43
|
|
|
return [ |
|
44
|
|
|
'view_mode' => '', |
|
45
|
|
|
'mids' => [], |
|
46
|
|
|
'uuids' => [], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function blockForm($form, FormStateInterface $form_state) { |
|
54
|
|
|
// This method receives a sub form state instead of the full form state. |
|
55
|
|
|
// There is an ongoing discussion around this which could result in the |
|
56
|
|
|
// passed form state going back to a full form state. In order to prevent |
|
57
|
|
|
// future breakage because of a core update we'll just check which type of |
|
58
|
|
|
// FormStateInterface we've been passed and act accordingly. |
|
59
|
|
|
// @See https://www.drupal.org/node/2798261 |
|
60
|
|
|
if ($form_state instanceof SubformStateInterface) { |
|
61
|
|
|
$form_state = $form_state->getCompleteFormState(); |
|
62
|
|
|
} |
|
63
|
|
|
$entities = $form_state->getValue([ |
|
64
|
|
|
'settings', |
|
65
|
|
|
'selection', |
|
66
|
|
|
'mids', |
|
67
|
|
|
'entities' |
|
68
|
|
|
], []); |
|
69
|
|
|
$mids = []; |
|
70
|
|
|
foreach ($entities as $entity) { |
|
71
|
|
|
$mids[] = $entity->id(); |
|
72
|
|
|
} |
|
73
|
|
|
if (empty($mids)) { |
|
74
|
|
|
$mids = $this->getDefaultMIDs(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$form['selection'] = $this->browserForm($mids); |
|
78
|
|
|
|
|
79
|
|
|
/** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repository */ |
|
80
|
|
|
$entity_display_repository = \Drupal::service('entity_display.repository'); |
|
81
|
|
|
$view_mode_options = $entity_display_repository->getViewModeOptions('media'); |
|
82
|
|
|
|
|
83
|
|
|
$form['view_mode'] = [ |
|
84
|
|
|
'#type' => 'select', |
|
85
|
|
|
'#options' => $view_mode_options, |
|
86
|
|
|
'#title' => t('View mode'), |
|
87
|
|
|
'#default_value' => $this->configuration['view_mode'], |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
return $form; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Constructs parts of the form needed to use Entity Browser. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $mids |
|
97
|
|
|
* An array of Media IDs. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
* A render array representing Entity Browser components. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function browserForm($mids) { |
|
103
|
|
|
$selection = [ |
|
104
|
|
|
'#type' => 'container', |
|
105
|
|
|
'#attributes' => ['id' => 'media-embed-block-browser'], |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
|
|
$selection['mids'] = [ |
|
109
|
|
|
'#type' => 'entity_browser', |
|
110
|
|
|
'#entity_browser' => 'media_browser_in_modal', |
|
111
|
|
|
'#entity_browser_validators' => [ |
|
112
|
|
|
'entity_type' => ['type' => 'media'] |
|
113
|
|
|
], |
|
114
|
|
|
'#process' => [ |
|
115
|
|
|
[ |
|
116
|
|
|
'\Drupal\entity_browser\Element\EntityBrowserElement', |
|
117
|
|
|
'processEntityBrowser' |
|
118
|
|
|
], |
|
119
|
|
|
[get_called_class(), 'processEntityBrowser'], |
|
120
|
|
|
], |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
$order_class = 'media-embed-block-delta-order'; |
|
124
|
|
|
|
|
125
|
|
|
$selection['table'] = [ |
|
126
|
|
|
'#type' => 'table', |
|
127
|
|
|
'#header' => [ |
|
128
|
|
|
t('Title'), |
|
129
|
|
|
t('Type'), |
|
130
|
|
|
t('Order', [], ['context' => 'Sort order']), |
|
131
|
|
|
], |
|
132
|
|
|
'#empty' => t('No media yet'), |
|
133
|
|
|
'#tabledrag' => [ |
|
134
|
|
|
[ |
|
135
|
|
|
'action' => 'order', |
|
136
|
|
|
'relationship' => 'sibling', |
|
137
|
|
|
'group' => $order_class, |
|
138
|
|
|
], |
|
139
|
|
|
], |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$delta = 0; |
|
143
|
|
|
$bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media'); |
|
144
|
|
|
|
|
145
|
|
|
foreach ($mids as $mid) { |
|
146
|
|
|
/** @var \Drupal\media\Entity\Media $media */ |
|
147
|
|
|
$media = Media::load($mid); |
|
148
|
|
|
|
|
149
|
|
|
$selection['table'][$mid] = [ |
|
150
|
|
|
'#attributes' => [ |
|
151
|
|
|
'class' => ['draggable'], |
|
152
|
|
|
'data-entity-id' => $media->getEntityTypeId() . ':' . $mid, |
|
153
|
|
|
], |
|
154
|
|
|
'title' => ['#markup' => $media->label()], |
|
155
|
|
|
'type' => ['#markup' => $bundle_info[$media->bundle()]['label']], |
|
156
|
|
|
'_weight' => [ |
|
157
|
|
|
'#type' => 'weight', |
|
158
|
|
|
'#title' => t('Weight for row @number', ['@number' => $delta + 1]), |
|
159
|
|
|
'#title_display' => 'invisible', |
|
160
|
|
|
'#delta' => count($mids), |
|
161
|
|
|
'#default_value' => $delta, |
|
162
|
|
|
'#attributes' => ['class' => [$order_class]], |
|
163
|
|
|
], |
|
164
|
|
|
]; |
|
165
|
|
|
|
|
166
|
|
|
$delta++; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $selection; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* AJAX callback: Re-renders the Entity Browser button/table. |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function updateCallback(array &$form, FormStateInterface $form_state) { |
|
176
|
|
|
$trigger = $form_state->getTriggeringElement(); |
|
177
|
|
|
$parents = array_slice($trigger['#array_parents'], 0, -2); |
|
178
|
|
|
$selection = NestedArray::getValue($form, $parents); |
|
179
|
|
|
return $selection; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Render API callback: Processes the entity browser element. |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function processEntityBrowser(&$element, FormStateInterface $form_state, &$complete_form) { |
|
|
|
|
|
|
186
|
|
|
$element['entity_ids']['#ajax'] = [ |
|
187
|
|
|
'callback' => [get_called_class(), 'updateCallback'], |
|
188
|
|
|
'wrapper' => 'media-embed-block-browser', |
|
189
|
|
|
'event' => 'entity_browser_value_updated', |
|
190
|
|
|
]; |
|
191
|
|
|
return $element; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* {@inheritdoc} |
|
196
|
|
|
*/ |
|
197
|
|
|
public function blockSubmit($form, FormStateInterface $form_state) { |
|
198
|
|
|
$this->configuration['mids'] = []; |
|
199
|
|
|
$this->configuration['uuids'] = []; |
|
200
|
|
|
foreach ($form_state->getValue(['selection', 'table'], []) as $mid => $settings) { |
|
201
|
|
|
$this->configuration['mids'][] = $mid; |
|
202
|
|
|
} |
|
203
|
|
|
$this->configuration['view_mode'] = $form_state->getValue('view_mode'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* {@inheritdoc} |
|
208
|
|
|
*/ |
|
209
|
|
|
public function build() { |
|
210
|
|
|
$build = []; |
|
211
|
|
|
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('media'); |
|
212
|
|
|
|
|
213
|
|
|
foreach ($this->getDefaultMIDs() as $mid) { |
|
214
|
|
|
/** @var \Drupal\media\Entity\Media $media */ |
|
215
|
|
|
$media = Media::load($mid); |
|
216
|
|
|
if ($media && $media->access('view')) { |
|
217
|
|
|
if (isset(static::$recursiveRenderDepth[$mid])) { |
|
218
|
|
|
static::$recursiveRenderDepth[$mid]++; |
|
219
|
|
|
} |
|
220
|
|
|
else { |
|
221
|
|
|
static::$recursiveRenderDepth[$mid] = 1; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// Protect ourselves from recursive rendering. |
|
225
|
|
|
if (static::$recursiveRenderDepth[$mid] > static::RECURSIVE_RENDER_LIMIT) { |
|
226
|
|
|
return $build; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$build[] = $view_builder->view($media, $this->configuration['view_mode']); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $build; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Gets the default MIDs for this Block. |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
* An array of Media IDs that are currently set in the Block configuration. |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function getDefaultMIDs() { |
|
243
|
|
|
// We optionally support UUIDs being put directly to our configuration, to |
|
244
|
|
|
// support profiles providing Media Embed Blocks with default config. |
|
245
|
|
|
if (!empty($this->configuration['uuids'])) { |
|
246
|
|
|
$mids = \Drupal::entityQuery('media') |
|
247
|
|
|
->condition('uuid', $this->configuration['uuids'], 'IN') |
|
248
|
|
|
->execute(); |
|
249
|
|
|
} |
|
250
|
|
|
else { |
|
251
|
|
|
$mids = []; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// Merge in the normal configuration. |
|
255
|
|
|
$mids += $this->configuration['mids']; |
|
256
|
|
|
|
|
257
|
|
|
return $mids; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.