1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains df_tools_blocks.module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\block_content\Entity\BlockContent; |
9
|
|
|
use Drupal\block_content\Entity\BlockContentType; |
10
|
|
|
use Drupal\Component\Utility\Html; |
11
|
|
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; |
12
|
|
|
use Drupal\Core\Entity\EntityInterface; |
13
|
|
|
use Drupal\Core\Entity\FieldableEntityInterface; |
14
|
|
|
use Drupal\Core\Form\FormStateInterface; |
15
|
|
|
use Drupal\Core\Render\Element; |
16
|
|
|
use Drupal\Core\StringTranslation\TranslatableMarkup; |
17
|
|
|
use Drupal\field\Entity\FieldConfig; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implements hook_modules_installed(). |
21
|
|
|
*/ |
22
|
|
|
function df_tools_blocks_modules_installed(array $modules) { |
23
|
|
|
// Don't do anything during config sync. |
24
|
|
|
if (\Drupal::isConfigSyncing()) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Add support for Acquia DAM Asset media entities to the Media field if the |
29
|
|
|
// df_tools_media_acquiadam module is installed. |
30
|
|
|
if (in_array('df_tools_media_acquiadam', $modules)) { |
31
|
|
|
// Retrieve the block's 'media' field. |
32
|
|
|
$instance = FieldConfig::loadByName('block_content', 'media', 'field_media'); |
33
|
|
|
|
34
|
|
|
// Add 'acquia_dam_asset' to the list of allowed target bundles. |
35
|
|
|
$settings = $instance->getSetting('handler_settings'); |
36
|
|
|
$settings['target_bundles'][] = 'acquia_dam_asset'; |
37
|
|
|
$instance->setSetting('handler_settings', $settings); |
38
|
|
|
$instance->save(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Implements hook_ENTITY_TYPE_view_alter(). |
44
|
|
|
*/ |
45
|
|
|
function df_tools_blocks_block_content_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { |
46
|
|
|
if ($entity->bundle() === 'basic') { |
47
|
|
|
$wrapper = [ |
48
|
|
|
'#prefix' => '<div class="full-width-inner">', |
49
|
|
|
'#suffix' => '</div>', |
50
|
|
|
'#type' => 'container', |
51
|
|
|
'#weight' => -1, |
52
|
|
|
'#attributes' => [ |
53
|
|
|
'class' => ['basic-block-fields'], |
54
|
|
|
], |
55
|
|
|
]; |
56
|
|
|
$build['wrapper'] = $wrapper; |
57
|
|
|
$build['#prefix'] = '<div class="basic-block">'; |
58
|
|
|
$build['#suffix'] = '</div>'; |
59
|
|
|
} |
60
|
|
|
if ($entity->bundle() === 'hero') { |
61
|
|
|
$wrapper = [ |
62
|
|
|
'#prefix' => '<div class="full-width-inner">', |
63
|
|
|
'#suffix' => '</div>', |
64
|
|
|
'#type' => 'container', |
65
|
|
|
'#weight' => -1, |
66
|
|
|
'#attributes' => [ |
67
|
|
|
'class' => ['hero-block-fields'], |
68
|
|
|
], |
69
|
|
|
'#children' => [], |
70
|
|
|
]; |
71
|
|
|
foreach (Element::getVisibleChildren($build) as $field_name) { |
72
|
|
|
if ($field_name === 'field_icon' && isset($build[$field_name]['#object'])) { |
73
|
|
|
$icon = $build[$field_name]['#object']->field_icon->getString(); |
74
|
|
|
if (!empty($icon)) { |
75
|
|
|
$wrapper['#children'][$field_name] = [ |
76
|
|
|
'#markup' => '<i class="fa ' . $icon . ' hero-icon"></i>', |
77
|
|
|
'#allowed_tags' => ['i'], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
unset($build[$field_name]); |
81
|
|
|
} |
82
|
|
|
elseif ($field_name !== 'field_hero_image') { |
83
|
|
|
$wrapper['#children'][$field_name] = $build[$field_name]; |
84
|
|
|
unset($build[$field_name]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
// Attach styles. |
88
|
|
|
$alignment = $entity->get('field_hero_alignment')->getString(); |
|
|
|
|
89
|
|
|
if (preg_match('/^[a-z_]+$/i', $alignment)) { |
90
|
|
|
$wrapper['#attributes']['class'][] = Html::cleanCssIdentifier('hero-block-align-' . $alignment); |
91
|
|
|
} |
92
|
|
|
$color_regex = '/^#[0-9a-f]{6}$/i'; |
93
|
|
|
$text_color = $entity->get('field_text_color')->getString(); |
94
|
|
|
if (preg_match($color_regex, $text_color)) { |
95
|
|
|
$wrapper['#children']['field_first_line']['#attributes']['style'] = 'color: ' . $text_color; |
96
|
|
|
$wrapper['#children']['field_second_line']['#attributes']['style'] = 'color: ' . $text_color; |
97
|
|
|
} |
98
|
|
|
$gradient_color = $entity->get('field_gradient_color')->getString(); |
99
|
|
|
if (preg_match($color_regex, $gradient_color)) { |
100
|
|
|
switch ($alignment) { |
101
|
|
|
case 'left': |
102
|
|
|
$direction = 'to right'; |
103
|
|
|
break; |
104
|
|
|
|
105
|
|
|
case 'right': |
106
|
|
|
$direction = 'to left'; |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
default: |
110
|
|
|
$direction = 'to bottom'; |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
$build['field_hero_image']['#attributes']['style'] = "background-image: linear-gradient($direction, $gradient_color, transparent)"; |
114
|
|
|
} |
115
|
|
|
$callout_color = $entity->get('field_callout_color')->getString(); |
116
|
|
|
if (preg_match($color_regex, $callout_color)) { |
117
|
|
|
$style = 'background-color: ' . $callout_color . ';'; |
118
|
|
|
$hex = trim($callout_color, '#'); |
119
|
|
|
$r = hexdec(substr($hex, 0, 2)); |
120
|
|
|
$g = hexdec(substr($hex, 2, 2)); |
121
|
|
|
$b = hexdec(substr($hex, 4, 2)); |
122
|
|
|
if ($r + $g + $b > 382) { |
123
|
|
|
$wrapper['#children']['field_hero_link'][0]['#attributes']['class'][] = 'hero-link-light-bg'; |
124
|
|
|
$style .= 'border-color:black;color:black;'; |
125
|
|
|
} |
126
|
|
|
else { |
127
|
|
|
$wrapper['#children']['field_hero_link'][0]['#attributes']['class'][] = 'hero-link-dark-bg'; |
128
|
|
|
$style .= 'border-color:white;color:white;'; |
129
|
|
|
} |
130
|
|
|
$wrapper['#children']['field_hero_link'][0]['#attributes']['style'] = $style; |
131
|
|
|
} |
132
|
|
|
$wrapper['#attached']['library'][] = 'df_tools_blocks/hero'; |
133
|
|
|
$build['wrapper'] = $wrapper; |
134
|
|
|
$build['#prefix'] = '<div class="hero-block">'; |
135
|
|
|
$build['#suffix'] = '</div>'; |
136
|
|
|
} |
137
|
|
|
if ($entity->bundle() === 'video_hero') { |
138
|
|
|
$wrapper = [ |
139
|
|
|
'#type' => 'container', |
140
|
|
|
'#weight' => -1, |
141
|
|
|
'#attributes' => [ |
142
|
|
|
'class' => ['video-hero-block-fields'], |
143
|
|
|
], |
144
|
|
|
'#children' => [], |
145
|
|
|
]; |
146
|
|
|
foreach (Element::getVisibleChildren($build) as $field_name) { |
147
|
|
|
if ($field_name === 'field_hero_link') { |
148
|
|
|
$build[$field_name]['#attributes']['class'][] = 'button'; |
149
|
|
|
} |
150
|
|
|
if ($field_name !== 'field_media') { |
151
|
|
|
$wrapper['#children'][$field_name] = $build[$field_name]; |
152
|
|
|
unset($build[$field_name]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
$build['wrapper'] = $wrapper; |
156
|
|
|
$build['#attributes']['class'][] = 'video-hero-block'; |
157
|
|
|
$build['#attributes']['class'][] = 'full-width-row'; |
158
|
|
|
$build['#attached']['library'][] = 'df_tools_blocks/video_hero'; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Implements hook_theme_suggestions_HOOK_alter(). |
164
|
|
|
*/ |
165
|
|
|
function df_tools_blocks_theme_suggestions_block_alter(array &$suggestions, array $variables) { |
166
|
|
|
// Add block--block-content--bundle.html.twig suggestions. |
167
|
|
|
if (isset($variables['elements']['content']) && isset($variables['elements']['content']['#block_content'])) { |
168
|
|
|
/** @var \Drupal\block_content\Entity\BlockContent $entity */ |
169
|
|
|
$entity = $variables['elements']['content']['#block_content']; |
170
|
|
|
$suggestions[] = 'block__block_content__' . $entity->bundle(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Implements hook_form_alter(). |
176
|
|
|
*/ |
177
|
|
|
function df_tools_blocks_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
178
|
|
|
if (preg_match('/^block_content.*panels_ipe_form$/', $form_id)) { |
179
|
|
|
// Wrap the revision information and any other vertical tabs in a fieldset. |
180
|
|
|
// This breaks vertical tab styling, but we don't want this open most of |
181
|
|
|
// the time anyway. |
182
|
|
|
$form['advanced'] = [ |
183
|
|
|
'#type' => 'details', |
184
|
|
|
'#title' => t('Advanced'), |
185
|
|
|
'#collapsed' => TRUE, |
186
|
|
|
'#attributes' => ['class' => ['fieldset']], |
187
|
|
|
'#weight' => 100, |
188
|
|
|
0 => $form['advanced'], |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
// Hide the Block description description and remove any mention of "Block". |
192
|
|
|
$form['info']['widget'][0]['value']['#title'] = t('Description'); |
193
|
|
|
$form['info']['widget'][0]['value']['#description'] = []; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (preg_match('/^block_content_\X*_edit_form$/', $form_id)) { |
197
|
|
|
// Add a 'copy' button to all custom blocks edit forms. |
198
|
|
|
$form['actions']['copy'] = [ |
199
|
|
|
'#type' => 'submit', |
200
|
|
|
'#value' => t('Copy'), |
201
|
|
|
'#submit' => ['df_tools_blocks_block_content_replicate'], |
202
|
|
|
'#weight' => 9, |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Implements hook_form_FORM_ID_alter(). |
209
|
|
|
*/ |
210
|
|
|
function df_tools_blocks_form_panels_ipe_block_plugin_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
211
|
|
|
// Modify the block placement form. |
212
|
|
|
if (isset($form['flipper']['front']['settings']['admin_label'])) { |
213
|
|
|
$form['flipper']['front']['settings']['admin_label']['#weight'] = -2; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (isset($form['flipper']['front']['settings']['label_display'])) { |
217
|
|
|
if (empty($form['uuid']['#value'])) { |
218
|
|
|
$form['flipper']['front']['settings']['label_display']['#default_value'] = FALSE; |
219
|
|
|
} |
220
|
|
|
$form['flipper']['front']['settings']['label_display']['#weight'] = -1; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (isset($form['flipper']['front']['settings']['label'])) { |
224
|
|
|
$form['flipper']['front']['settings']['label']['#weight'] = 0; |
225
|
|
|
$form['flipper']['front']['settings']['label']['#states'] = [ |
226
|
|
|
'visible' => [ |
227
|
|
|
':input[name="settings[label_display]"]' => ['checked' => TRUE], |
228
|
|
|
], |
229
|
|
|
]; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Add special logic for the Entity Browser block plugins. |
233
|
|
|
if (isset($form['plugin_id'])) { |
234
|
|
|
if ($form['plugin_id']['#value'] === 'content_embed') { |
235
|
|
|
// Only auto-open the Content Embed block if it is empty. |
236
|
|
|
if (empty(Element::getVisibleChildren($form['flipper']['front']['settings']['selection']['table'])) && !$form_state->isProcessingInput()) { |
237
|
|
|
$form['#attached']['library'][] = 'df_tools_blocks/auto_open'; |
238
|
|
|
$form['#attributes']['data-df-tools-blocks-auto-open'] = 'settings_selection_nids_entity_browser'; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// Modify the Content Embed form to guide users to valid View Modes. |
242
|
|
|
if (empty($form['flipper']['front']['settings']['view_mode']['#default_value'])) { |
243
|
|
|
$form['flipper']['front']['settings']['view_mode']['#default_value'] = 'featured'; |
244
|
|
|
} |
245
|
|
|
$excluded_modes = [ |
246
|
|
|
'basic_info', |
247
|
|
|
'content_browser', |
248
|
|
|
'rss', |
249
|
|
|
'search_index', |
250
|
|
|
'search_result', |
251
|
|
|
'token', |
252
|
|
|
'full', |
253
|
|
|
'df', |
254
|
|
|
]; |
255
|
|
|
foreach ($excluded_modes as $view_mode) { |
256
|
|
|
if (isset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode])) { |
257
|
|
|
unset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
elseif ($form['plugin_id']['#value'] === 'media_embed') { |
262
|
|
|
if (empty(Element::getVisibleChildren($form['flipper']['front']['settings']['selection']['table'])) && !$form_state->isProcessingInput()) { |
263
|
|
|
$form['#attached']['library'][] = 'df_tools_blocks/auto_open'; |
264
|
|
|
$form['#attributes']['data-df-tools-blocks-auto-open'] = 'settings_selection_mids_entity_browser'; |
265
|
|
|
} |
266
|
|
|
$excluded_modes = [ |
267
|
|
|
'media_browser', |
268
|
|
|
'product_slideshow_thumbnail', |
269
|
|
|
'hero_thin', |
270
|
|
|
'hero_product', |
271
|
|
|
'embedded', |
272
|
|
|
'token', |
273
|
|
|
]; |
274
|
|
|
foreach ($excluded_modes as $view_mode) { |
275
|
|
|
if (isset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode])) { |
276
|
|
|
unset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode]); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Implements hook_form_FORM_ID_alter(). |
285
|
|
|
*/ |
286
|
|
|
function df_tools_blocks_form_block_content_hero_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
287
|
|
|
df_tools_blocks_form_block_content_hero_panels_ipe_form_alter($form, $form_state, $form_id); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Implements hook_form_FORM_ID_alter(). |
292
|
|
|
*/ |
293
|
|
|
function df_tools_blocks_form_block_content_hero_panels_ipe_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
294
|
|
|
// Move the hero image out of the details element and set a better label. |
295
|
|
|
$form['field_hero_image']['widget']['#type'] = 'container'; |
296
|
|
|
$form['field_hero_image']['widget']['entity_browser']['#process'][] = '_df_tools_blocks_hero_browser_button'; |
297
|
|
|
// Remove the link fieldset and put the URI and title side-by-side. |
298
|
|
|
$form['field_hero_link']['widget'][0]['#type'] = 'container'; |
299
|
|
|
$form['field_hero_link']['widget'][0]['#attributes']['class'][] = 'row'; |
300
|
|
|
unset($form['field_hero_link']['widget'][0]['uri']['#description']); |
301
|
|
|
$form['field_hero_link']['widget'][0]['uri']['#title'] = t('Callout link'); |
302
|
|
|
$form['field_hero_link']['widget'][0]['title']['#title'] = t('Callout text'); |
303
|
|
|
$form['field_hero_link']['widget'][0]['uri']['#prefix'] = '<div class="large-6 columns">'; |
304
|
|
|
$form['field_hero_link']['widget'][0]['uri']['#suffix'] = '</div>'; |
305
|
|
|
$form['field_hero_link']['widget'][0]['title']['#prefix'] = '<div class="large-6 columns">'; |
306
|
|
|
$form['field_hero_link']['widget'][0]['title']['#suffix'] = '</div>'; |
307
|
|
|
// Group complex form elements into the advanced section. |
308
|
|
|
$form['field_icon']['#attributes']['class'][] = 'visually-hidden'; |
309
|
|
|
$form['field_nested_block']['#attributes']['class'][] = 'visually-hidden'; |
310
|
|
|
$style_fields = [ |
311
|
|
|
'field_text_color', |
312
|
|
|
'field_gradient_color', |
313
|
|
|
'field_callout_color', |
314
|
|
|
]; |
315
|
|
|
$form['style_fields'] = [ |
316
|
|
|
'#type' => 'container', |
317
|
|
|
'#attributes' => ['class' => ['row']], |
318
|
|
|
]; |
319
|
|
|
foreach ($style_fields as $field) { |
320
|
|
|
if (strpos($field, 'color') !== FALSE) { |
321
|
|
|
$form[$field]['widget'][0]['value']['#type'] = 'color'; |
322
|
|
|
} |
323
|
|
|
$form[$field]['#attributes']['class'][] = 'large-4 columns'; |
324
|
|
|
$form['style_fields'][] = $form[$field]; |
325
|
|
|
$form['style_fields']['#weight'] = $form[$field]['#weight']; |
326
|
|
|
unset($form[$field]); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Does nit-picky changes on the Hero image entity browser. |
332
|
|
|
* |
333
|
|
|
* @param array $element |
334
|
|
|
* The element render array. |
335
|
|
|
* |
336
|
|
|
* @return array |
337
|
|
|
* The possibly modified form render array. |
338
|
|
|
*/ |
339
|
|
|
function _df_tools_blocks_hero_browser_button(array $element) { |
340
|
|
|
$element['entity_browser']['open_modal']['#value'] = t('Select background'); |
341
|
|
|
if (isset($element['entity_browser']['open_modal']['#ajax'])) { |
342
|
|
|
/** @var \Drupal\entity_browser\DisplayInterface $display */ |
343
|
|
|
$display = $element['entity_browser']['open_modal']['#ajax']['callback'][0]; |
344
|
|
|
$configuration = $display->getConfiguration(); |
345
|
|
|
$configuration['link_text'] = t('Background image'); |
346
|
|
|
$display->setConfiguration($configuration); |
347
|
|
|
} |
348
|
|
|
return $element; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Implements hook_migration_plugins_alter(). |
353
|
|
|
*/ |
354
|
|
|
function df_tools_blocks_migration_plugins_alter(&$definitions) { |
355
|
|
|
// Set up the correct pa$th for the CSV files in this module. |
356
|
|
|
$types = ['hero', 'media']; |
357
|
|
|
foreach ($types as $type) { |
358
|
|
|
$id = 'import_block_' . $type; |
359
|
|
|
$definitions[$id]['source']['path'] = str_replace('..', dirname(__FILE__), $definitions[$id]['source']['path']); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Callback to re-sort blocks into categories. |
365
|
|
|
* |
366
|
|
|
* @param array &$block_info |
367
|
|
|
* An array of block plugin definitions. |
368
|
|
|
*/ |
369
|
|
|
function df_tools_blocks_alter_block_categories(array &$block_info) { |
370
|
|
|
// Create an associative array which maps default => custom block categories. |
371
|
|
|
$category_map = [ |
372
|
|
|
'Embed' => t('Existing Content'), |
373
|
|
|
'Entity Block' => t('Existing Content'), |
374
|
|
|
'Commerce' => t('Forms'), |
375
|
|
|
'Lists (Views)' => t('Lists'), |
376
|
|
|
'Listes (Views)' => t('Lists'), |
377
|
|
|
'Listas (Views)' => t('Lists'), |
378
|
|
|
'Views' => t('Lists'), |
379
|
|
|
'AddToAny' => t('Social'), |
380
|
|
|
'Product hero' => t('Hero'), |
381
|
|
|
'Workbench moderation' => t('Hidden'), |
382
|
|
|
'User' => t('Hidden'), |
383
|
|
|
'Utilisateur' => t('Hidden'), |
384
|
|
|
'Usuario' => t('Hidden'), |
385
|
|
|
'Chaos tools' => t('Hidden'), |
386
|
|
|
'Help' => t('Hidden'), |
387
|
|
|
'core' => t('Hidden'), |
388
|
|
|
'System' => t('Hidden'), |
389
|
|
|
'Moderation Dashboard' => t('Hidden'), |
390
|
|
|
'Entity Browser' => t('Hidden'), |
391
|
|
|
'Facets' => t('Hidden'), |
392
|
|
|
'Facets summary (Experimental)' => t('Hidden'), |
393
|
|
|
'Responsive Preview' => t('Hidden'), |
394
|
|
|
'DF Tools Blocks' => t('Hidden'), |
395
|
|
|
]; |
396
|
|
|
|
397
|
|
|
$label_map = [ |
398
|
|
|
'Content Embed' => t('Content'), |
399
|
|
|
'Media Embed' => t('Media'), |
400
|
|
|
]; |
401
|
|
|
|
402
|
|
|
foreach ($block_info as $key => $info) { |
403
|
|
|
// Retrieve the name of the block category. |
404
|
|
|
$category = $info['category']; |
405
|
|
|
$label = $info['label']; |
406
|
|
|
|
407
|
|
|
// Retrieve the untranslated name of the category if it has been translated. |
408
|
|
|
if ($category instanceof TranslatableMarkup) { |
409
|
|
|
$category = $category->getUntranslatedString(); |
410
|
|
|
} |
411
|
|
|
if ($label instanceof TranslatableMarkup) { |
412
|
|
|
$label = $label->getUntranslatedString(); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// Move all fields of the currently panelized entity into a |
416
|
|
|
// 'Current @Entity_Type' category. If the block is not a field on the |
417
|
|
|
// current entity, allow its category to be remapped. |
418
|
|
|
if ($category === '@entity') { |
419
|
|
|
$block_info[$key]['category'] = $info['category']->render() . ' Fields'; |
420
|
|
|
} |
421
|
|
|
// Place Block Content entities into categories based on their type. |
422
|
|
|
elseif ($category === 'Custom') { |
423
|
|
|
list($type, $uuid) = explode(':', $block_info[$key]['plugin_id']); |
424
|
|
|
unset($type); |
425
|
|
|
$ids = \Drupal::entityQuery('block_content') |
426
|
|
|
->condition('uuid', $uuid, '=') |
427
|
|
|
->execute(); |
428
|
|
|
if ($block = BlockContent::load(reset($ids))) { |
|
|
|
|
429
|
|
|
if ($bundle = BlockContentType::load($block->bundle())) { |
430
|
|
|
$block_info[$key]['category'] = $bundle->label(); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
if (isset($block_info[$key])) { |
436
|
|
|
if (isset($category_map[$category])) { |
437
|
|
|
// Already translated. |
438
|
|
|
$block_info[$key]['category'] = $category_map[$category]; |
439
|
|
|
} |
440
|
|
|
elseif (is_string($block_info[$key]['category'])) { |
441
|
|
|
// Already translated. |
442
|
|
|
$block_info[$key]['category'] = $block_info[$key]['category']; |
443
|
|
|
} |
444
|
|
|
if (isset($label_map[$label])) { |
445
|
|
|
// Already translated. |
446
|
|
|
$block_info[$key]['label'] = $label_map[$label]; |
447
|
|
|
} |
448
|
|
|
elseif (is_string($block_info[$key]['label'])) { |
449
|
|
|
// Already translated. |
450
|
|
|
$block_info[$key]['label'] = $block_info[$key]['label']; |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
if ($label === 'Entity gallery' && $category === 'Entity Block') { |
455
|
|
|
$block_info[$key]['category'] = t('Hidden'); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
if ($label === 'Entity Gallery Browser') { |
459
|
|
|
$block_info[$key]['category'] = t('Existing Content'); |
460
|
|
|
$block_info[$key]['label'] = t('Gallery'); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Implements hook_panels_ipe_blocks_alter(). |
467
|
|
|
* |
468
|
|
|
* Improves Panelizer/Panels IPE UX by removing fields that cannot be placed. |
469
|
|
|
*/ |
470
|
|
|
function df_tools_blocks_panels_ipe_blocks_alter(&$block_info) { |
471
|
|
|
// Re-sort blocks into specific categories. |
472
|
|
|
df_tools_blocks_alter_block_categories($block_info); |
473
|
|
|
// Filter out fields that aren't relevant for this entity. |
474
|
|
|
$request = \Drupal::request(); |
475
|
|
|
$storage_id = str_replace('*', '', $request->attributes->get('panels_storage_id', '')); |
476
|
|
|
list($entity_type, $id) = explode(':', $storage_id); |
477
|
|
|
$entity_type_manager = \Drupal::entityTypeManager(); |
478
|
|
|
if (!empty($entity_type) && !empty($id) && $entity_type_manager->hasDefinition($entity_type)) { |
479
|
|
|
if ($entity = $entity_type_manager->getStorage($entity_type)->load($id)) { |
480
|
|
|
foreach ($block_info as $key => $info) { |
481
|
|
|
if ($info['id'] === 'entity_field') { |
482
|
|
|
list($block_id, $block_entity_type, $field_name) = explode(':', $info['plugin_id']); |
483
|
|
|
unset($block_id); |
484
|
|
|
if ($block_entity_type !== $entity_type || ($entity instanceof FieldableEntityInterface && !$entity->hasField($field_name))) { |
485
|
|
|
unset($block_info[$key]); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
$block_info = array_values($block_info); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Form submission handler for df_tools_blocks_form_alter(). |
496
|
|
|
*/ |
497
|
|
|
function df_tools_blocks_block_content_replicate($form, FormStateInterface $form_state) { |
498
|
|
|
// Retrieve block content entity from form_state. |
499
|
|
|
$entity = $form_state->getFormObject()->getEntity(); |
|
|
|
|
500
|
|
|
// Extract a few values from the entity. |
501
|
|
|
$block_info = $entity->get('info')->value; |
502
|
|
|
$block_id = $entity->id(); |
503
|
|
|
// Replicate the block content entity. |
504
|
|
|
if (\Drupal::service('df_tools_blocks.copier')->makeCopy($entity)) { |
505
|
|
|
$message = t('Block Content: copied @info [id:@id]', ['@info' => $block_info, '@id' => $block_id]); |
506
|
|
|
\Drupal::logger('df_tools_blocks')->info($message); |
507
|
|
|
drupal_set_message($message, 'status'); |
|
|
|
|
508
|
|
|
} |
509
|
|
|
// Redirect to block list comes free! |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Implements hook_theme(). |
514
|
|
|
*/ |
515
|
|
|
function df_tools_blocks_theme($existing, $type, $theme, $path) { |
516
|
|
|
return [ |
517
|
|
|
'code_block' => [ |
518
|
|
|
'variables' => ['code' => NULL], |
519
|
|
|
], |
520
|
|
|
]; |
521
|
|
|
} |
522
|
|
|
|