GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — 8.x-4.x-clone ( d3260c )
by Kevin
14:07 queued 10s
created

df_tools_blocks_block_content_replicate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $display is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

45
function df_tools_blocks_block_content_view_alter(array &$build, EntityInterface $entity, /** @scrutinizer ignore-unused */ EntityViewDisplayInterface $display) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
47
  if ($entity->bundle() === 'hero') {
48
49
    $hero_alignment = $entity->get('field_hero_alignment')->getString();
0 ignored issues
show
Bug introduced by
The method get() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Drupal\Core\Entity\EntityChangedInterface or Drupal\Core\Entity\RevisionableInterface or Drupal\Core\Entity\EntityBase or Drupal\Core\Entity\EntityPublishedInterface or Drupal\Core\Entity\TranslatableInterface or Drupal\Core\Entity\SynchronizableInterface or Drupal\Tests\Core\Entity...rageTestEntityInterface or Drupal\Core\Entity\EntityDescriptionInterface or Drupal\Core\Entity\Entit...uginCollectionInterface or Drupal\Core\Entity\RevisionLogInterface or Drupal\Core\Entity\Trans...leRevisionableInterface or Drupal\Tests\Core\Entity\RevisionableEntity or Drupal\Core\Entity\Entity or Drupal\Tests\Core\Entity\SimpleTestEntity or Drupal\Tests\content_mod...n\Unit\SimpleTestEntity or Drupal\Tests\Core\Entity\RevisionableEntity or Drupal\Core\Entity\Trans...leRevisionableInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
    $hero_alignment = $entity->/** @scrutinizer ignore-call */ get('field_hero_alignment')->getString();
Loading history...
50
    if (preg_match('/^[a-z_]+$/i', $hero_alignment)) {
51
      switch ($hero_alignment) {
52
        case 'center':
53
          $hero_alignment = 'justify-content-center';
54
          break;
55
        case 'right':
56
          $hero_alignment = 'justify-content-end';
57
          break;
58
        default:
59
          $hero_alignment = 'justify-content-start';
60
          break;
61
      }
62
    }
63
64
    $text_alignment = $entity->get('field_text_align')->getString();
65
    if (preg_match('/^[a-z_]+$/i', $hero_alignment)) {
66
      switch ($text_alignment) {
67
        case 'center':
68
          $text_alignment = 'center';
69
          break;
70
        case 'right':
71
          $text_alignment = 'right';
72
          break;
73
        default:
74
          $text_alignment = 'left';
75
          break;
76
      }
77
    }
78
79
    $hero_callout_width = $entity->get('field_callout_width')->getString();
80
    switch($hero_callout_width) {
81
      case '03':
82
        $hero_callout_width = 'col col-md-5 col-lg-3';
83
        break;
84
      case '04':
85
        $hero_callout_width = 'col col-md-6 col-lg-4';
86
        break;
87
      case '05':
88
        $hero_callout_width = 'col col-md-7 col-lg-5';
89
        break;
90
      case '06':
91
        $hero_callout_width = 'col col-md-8 col-lg-6';
92
        break;
93
      case '07':
94
        $hero_callout_width = 'col col-md-9 col-lg-7';
95
        break;
96
      case '08':
97
        $hero_callout_width = 'col col-md-10 col-lg-8';
98
        break;
99
      case '09':
100
        $hero_callout_width = 'col col-md-11 col-lg-9';
101
        break;
102
      case '10':
103
        $hero_callout_width = 'col col-md-12 col-lg-10';
104
        break;
105
      case '11':
106
        $hero_callout_width = 'col col-md-12 col-lg-11';
107
        break;
108
      case '12':
109
        $hero_callout_width = 'col-12';
110
        break;
111
    }
112
113
    $wrapper = [
114
      '#prefix' => '<div class="container"><div class="col-12"><div class="row ' . $hero_alignment . '">',
115
      '#suffix' => '</div></div></div>',
116
      '#type' => 'container',
117
      '#weight' => -1,
118
      '#attributes' => [
119
        'class' => ['hero-block-fields', $hero_callout_width ],
120
      ],
121
      '#children' => [],
122
    ];
123
    foreach (Element::getVisibleChildren($build) as $field_name) {
124
      if ($field_name === 'field_icon' && isset($build[$field_name]['#object'])) {
125
        $icon = $build[$field_name]['#object']->field_icon->getString();
126
        if (!empty($icon)) {
127
          $wrapper['#children'][$field_name] = [
128
            '#markup' => '<i class="fa ' . $icon . ' hero-icon"></i>',
129
            '#allowed_tags' => ['i'],
130
          ];
131
        }
132
        unset($build[$field_name]);
133
      }
134
      elseif ($field_name !== 'field_hero_background') {
135
        $wrapper['#children'][$field_name] = $build[$field_name];
136
        unset($build[$field_name]);
137
      }
138
    }
139
140
    $hero_first_line_size = $entity->get('field_first_line_size')->getString();
141
    $wrapper['#children']['field_first_line']['#attributes']['class'][] = 'size-' . $hero_first_line_size;
142
143
    $hero_second_line_size = $entity->get('field_second_line_size')->getString();
144
    $wrapper['#children']['field_second_line']['#attributes']['class'][] = 'size-' . $hero_second_line_size;
145
146
    // Attach styles.
147
    $alignment = $entity->get('field_hero_alignment')->getString();
148
    if (preg_match('/^[a-z_]+$/i', $alignment)) {
149
      $wrapper['#attributes']['class'][] = Html::cleanCssIdentifier('text-align-' . $text_alignment);
150
    }
151
    $color_regex = '/^#[0-9a-f]{6}$/i';
152
    $callout_background_color = $entity->get('field_callout_background_color')->getString();
153
    $callout_background_opacity = $entity->get('field_callout_background_opacity')->getString();
154
    $hex = trim($callout_background_color, '#');
155
    $r = hexdec(substr($hex, 0, 2));
156
    $g = hexdec(substr($hex, 2, 2));
157
    $b = hexdec(substr($hex, 4, 2));
158
    $callout_background_color = "background-color: rgba($r,$g,$b,$callout_background_opacity)";
159
    $wrapper['#attributes']['style'] = $callout_background_color;
160
    $text_color = $entity->get('field_text_color')->getString();
161
    if (preg_match($color_regex, $text_color)) {
162
      $wrapper['#children']['field_first_line']['#attributes']['style'] = 'color: ' . $text_color;
163
      $wrapper['#children']['field_second_line']['#attributes']['style'] = 'color: ' . $text_color;
164
    }
165
    if ($entity->get('field_gradient')->getString()) {
166
      $gradient_color = $entity->get('field_gradient_color')->getString();
167
      if (preg_match($color_regex, $gradient_color)) {
168
        switch ($alignment) {
169
          case 'left':
170
            $direction = 'to right';
171
            break;
172
173
          case 'right':
174
            $direction = 'to left';
175
            break;
176
177
          default:
178
            $direction = 'to bottom';
179
            break;
180
        }
181
        $build['field_hero_background']['#attributes']['style'] = "background-image: linear-gradient($direction, $gradient_color, transparent)";
182
      }
183
    }
184
    $callout_color = $entity->get('field_callout_color')->getString();
185
    if (preg_match($color_regex, $callout_color)) {
186
      $style = 'background-color: ' . $callout_color . ';';
187
      $hex = trim($callout_color, '#');
188
      $r = hexdec(substr($hex, 0, 2));
189
      $g = hexdec(substr($hex, 2, 2));
190
      $b = hexdec(substr($hex, 4, 2));
191
      if ($r + $g + $b > 382) {
192
        $wrapper['#children']['field_hero_link'][0]['#attributes']['class'][] = 'hero-link-light-bg';
193
        $style .= 'border-color:black;color:black;';
194
      }
195
      else {
196
        $wrapper['#children']['field_hero_link'][0]['#attributes']['class'][] = 'hero-link-dark-bg';
197
        $style .= 'border-color:white;color:white;';
198
      }
199
      $wrapper['#children']['field_hero_link'][0]['#attributes']['style'] = $style;
200
    }
201
    $wrapper['#attached']['library'][] = 'df_tools_blocks/hero';
202
    $build['wrapper'] = $wrapper;
203
    $build['#prefix'] = '<div class="hero-block">';
204
    $build['#suffix'] = '</div>';
205
  }
206
  if ($entity->bundle() === 'video_hero') {
207
    $wrapper = [
208
      '#type' => 'container',
209
      '#weight' => -1,
210
      '#attributes' => [
211
        'class' => ['video-hero-block-fields'],
212
      ],
213
      '#children' => [],
214
    ];
215
    foreach (Element::getVisibleChildren($build) as $field_name) {
216
      if ($field_name === 'field_hero_link') {
217
        $build[$field_name]['#attributes']['class'][] = 'button';
218
      }
219
      if ($field_name !== 'field_media') {
220
        $wrapper['#children'][$field_name] = $build[$field_name];
221
        unset($build[$field_name]);
222
      }
223
    }
224
    $build['wrapper'] = $wrapper;
225
    $build['#attributes']['class'][] = 'video-hero-block';
226
    $build['#attributes']['class'][] = 'full-width-row';
227
    $build['#attached']['library'][] = 'df_tools_blocks/video_hero';
228
  }
229
}
230
231
/**
232
 * Implements hook_theme_suggestions_HOOK_alter().
233
 */
234
function df_tools_blocks_theme_suggestions_block_alter(array &$suggestions, array $variables) {
235
  // Add block--block-content--bundle.html.twig suggestions.
236
  if (isset($variables['elements']['content']) && isset($variables['elements']['content']['#block_content'])) {
237
    /** @var \Drupal\block_content\Entity\BlockContent $entity */
238
    $entity = $variables['elements']['content']['#block_content'];
239
    $suggestions[] = 'block__block_content__' . $entity->bundle();
240
  }
241
}
242
243
/**
244
 * Implements hook_form_alter().
245
 */
246
function df_tools_blocks_form_alter(&$form, FormStateInterface $form_state, $form_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

246
function df_tools_blocks_form_alter(&$form, /** @scrutinizer ignore-unused */ FormStateInterface $form_state, $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
247
  if (preg_match('/^block_content.*panels_ipe_form$/', $form_id)) {
248
    // Wrap the revision information and any other vertical tabs in a fieldset.
249
    // This breaks vertical tab styling, but we don't want this open most of
250
    // the time anyway.
251
    $form['advanced'] = [
252
      '#type' => 'details',
253
      '#title' => t('Advanced'),
254
      '#collapsed' => TRUE,
255
      '#attributes' => ['class' => ['fieldset']],
256
      '#weight' => 100,
257
      0 => $form['advanced'],
258
    ];
259
260
    // Hide the Block description description and remove any mention of "Block".
261
    $form['info']['widget'][0]['value']['#title'] = t('Description');
262
    $form['info']['widget'][0]['value']['#description'] = [];
263
  }
264
265
  // Add a callback to #process to the layout_builder_update_block and
266
  // layout_builder_add_block. drupal.org/node/3028391
267
  $form_ids = ['layout_builder_update_block', 'layout_builder_add_block'];
268
  if (in_array($form_id, $form_ids)) {
269
    $form['settings']['block_form']['#process'][] = '_layout_builder_block_form_alter';
270
  }
271
}
272
273
/**
274
 * Process callback for layout_builder block form.
275
 *
276
 */
277
function _layout_builder_block_form_alter(array $element, FormStateInterface $form_state) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

277
function _layout_builder_block_form_alter(array $element, /** @scrutinizer ignore-unused */ FormStateInterface $form_state) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
278
279
  if (isset($element['field_hero_background'])) {
280
    // Move the hero image out of the details element and set a better label.
281
    $element['field_hero_background']['widget']['#type'] = 'container';
282
    $element['field_hero_background']['widget']['entity_browser']['#process'][] = '_df_tools_blocks_hero_browser_button';
283
     // Remove the link fieldset and put the URI and title side-by-side.
284
    $element['field_hero_link']['widget'][0]['#type'] = 'container';
285
    $element['field_hero_link']['widget'][0]['#attributes']['class'][] = 'row';
286
    unset($element['field_hero_link']['widget'][0]['uri']['#description']);
287
    $element['field_hero_link']['widget'][0]['uri']['#title'] = t('Callout link');
288
    $element['field_hero_link']['widget'][0]['title']['#title'] = t('Callout text');
289
    $element['field_hero_link']['widget'][0]['uri']['#prefix'] = '<div class="col-6">';
290
    $element['field_hero_link']['widget'][0]['uri']['#suffix'] = '</div>';
291
    $element['field_hero_link']['widget'][0]['title']['#prefix'] = '<div class="col-6">';
292
    $element['field_hero_link']['widget'][0]['title']['#suffix'] = '</div>';
293
    $style_fields = [
294
      'field_text_color',
295
      'field_text_align',
296
      'field_first_line_size',
297
      'field_second_line_size',
298
      'field_gradient',
299
      'field_gradient_color',
300
      'field_callout_color',
301
      'field_callout_width',
302
      'field_callout_background_color',
303
      'field_callout_background_opacity',
304
      'field_hero_alignment',
305
    ];
306
    $element['style_fields'] = [
307
      '#type' => 'details',
308
      '#title' => t('Advanced settings'),
309
      '#weight' => 2,
310
    ];
311
312
    foreach ($style_fields as $field) {
313
      if ((strpos($field, 'color') !== FALSE) && isset($element[$field])) {
314
          $element[$field]['widget'][0]['value']['#type'] = 'color';
315
      }
316
      $element[$field]['#attributes']['class'][] = 'col-4';
317
      $element['style_fields'][] = $element[$field];
318
      $element['style_fields']['#weight'] = $element[$field]['#weight'];
319
      unset($element[$field]);
320
    }
321
  }
322
  return $element;
323
}
324
325
/**
326
 * Implements hook_form_FORM_ID_alter().
327
 */
328
function df_tools_blocks_form_panels_ipe_block_plugin_form_alter(&$form, FormStateInterface $form_state, $form_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

328
function df_tools_blocks_form_panels_ipe_block_plugin_form_alter(&$form, FormStateInterface $form_state, /** @scrutinizer ignore-unused */ $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
329
  // Modify the block placement form.
330
  if (isset($form['flipper']['front']['settings']['admin_label'])) {
331
    $form['flipper']['front']['settings']['admin_label']['#weight'] = -2;
332
  }
333
334
  if (isset($form['flipper']['front']['settings']['label_display'])) {
335
    if (empty($form['uuid']['#value'])) {
336
      $form['flipper']['front']['settings']['label_display']['#default_value'] = FALSE;
337
    }
338
    $form['flipper']['front']['settings']['label_display']['#weight'] = -1;
339
  }
340
341
  if (isset($form['flipper']['front']['settings']['label'])) {
342
    $form['flipper']['front']['settings']['label']['#weight'] = 0;
343
    $form['flipper']['front']['settings']['label']['#states'] = [
344
      'visible' => [
345
        ':input[name="settings[label_display]"]' => ['checked' => TRUE],
346
      ],
347
    ];
348
  }
349
350
  // Add special logic for the Entity Browser block plugins.
351
  if (isset($form['plugin_id'])) {
352
    if ($form['plugin_id']['#value'] === 'content_embed') {
353
      // Only auto-open the Content Embed block if it is empty.
354
      if (empty(Element::getVisibleChildren($form['flipper']['front']['settings']['selection']['table'])) && !$form_state->isProcessingInput()) {
355
        $form['#attached']['library'][] = 'df_tools_blocks/auto_open';
356
        $form['#attributes']['data-df-tools-blocks-auto-open'] = 'settings_selection_nids_entity_browser';
357
      }
358
359
      // Modify the Content Embed form to guide users to valid View Modes.
360
      if (empty($form['flipper']['front']['settings']['view_mode']['#default_value'])) {
361
        $form['flipper']['front']['settings']['view_mode']['#default_value'] = 'featured';
362
      }
363
      $excluded_modes = [
364
        'basic_info',
365
        'content_browser',
366
        'rss',
367
        'search_index',
368
        'search_result',
369
        'token',
370
        'full',
371
        'df',
372
      ];
373
      foreach ($excluded_modes as $view_mode) {
374
        if (isset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode])) {
375
          unset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode]);
376
        }
377
      }
378
    }
379
    elseif ($form['plugin_id']['#value'] === 'media_embed') {
380
      if (empty(Element::getVisibleChildren($form['flipper']['front']['settings']['selection']['table'])) && !$form_state->isProcessingInput()) {
381
        $form['#attached']['library'][] = 'df_tools_blocks/auto_open';
382
        $form['#attributes']['data-df-tools-blocks-auto-open'] = 'settings_selection_mids_entity_browser';
383
      }
384
      $excluded_modes = [
385
        'media_browser',
386
        'product_slideshow_thumbnail',
387
        'hero_thin',
388
        'hero_product',
389
        'embedded',
390
        'token',
391
      ];
392
      foreach ($excluded_modes as $view_mode) {
393
        if (isset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode])) {
394
          unset($form['flipper']['front']['settings']['view_mode']['#options'][$view_mode]);
395
        }
396
      }
397
    }
398
  }
399
}
400
401
/**
402
 * Implements hook_form_FORM_ID_alter().
403
 */
404
function df_tools_blocks_form_block_content_hero_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
405
  df_tools_blocks_form_block_content_hero_panels_ipe_form_alter($form, $form_state, $form_id);
406
}
407
408
/**
409
 * Implements hook_form_FORM_ID_alter().
410
 */
411
function df_tools_blocks_form_block_content_hero_panels_ipe_form_alter(&$form, FormStateInterface $form_state, $form_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

411
function df_tools_blocks_form_block_content_hero_panels_ipe_form_alter(&$form, /** @scrutinizer ignore-unused */ FormStateInterface $form_state, $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

411
function df_tools_blocks_form_block_content_hero_panels_ipe_form_alter(&$form, FormStateInterface $form_state, /** @scrutinizer ignore-unused */ $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
412
  // Move the hero image out of the details element and set a better label.
413
  $form['field_hero_background']['widget']['#type'] = 'container';
414
  $form['field_hero_background']['widget']['entity_browser']['#process'][] = '_df_tools_blocks_hero_browser_button';
415
  // Remove the link fieldset and put the URI and title side-by-side.
416
  $form['field_hero_link']['widget'][0]['#type'] = 'container';
417
  $form['field_hero_link']['widget'][0]['#attributes']['class'][] = 'row';
418
  unset($form['field_hero_link']['widget'][0]['uri']['#description']);
419
  $form['field_hero_link']['widget'][0]['uri']['#title'] = t('Callout link');
420
  $form['field_hero_link']['widget'][0]['title']['#title'] = t('Callout text');
421
  $form['field_hero_link']['widget'][0]['uri']['#prefix'] = '<div class="large-6 columns">';
422
  $form['field_hero_link']['widget'][0]['uri']['#suffix'] = '</div>';
423
  $form['field_hero_link']['widget'][0]['title']['#prefix'] = '<div class="large-6 columns">';
424
  $form['field_hero_link']['widget'][0]['title']['#suffix'] = '</div>';
425
  // Group complex form elements into the advanced section.
426
  $form['field_icon']['#attributes']['class'][] = 'visually-hidden';
427
  $form['field_nested_block']['#attributes']['class'][] = 'visually-hidden';
428
  $style_fields = [
429
    'field_text_color',
430
    'field_gradient_color',
431
    'field_callout_color',
432
    'field_callout_background_color',
433
  ];
434
  $form['style_fields'] = [
435
    '#type' => 'container',
436
    '#attributes' => ['class' => ['row']],
437
  ];
438
  foreach ($style_fields as $field) {
439
    if (strpos($field, 'color') !== FALSE) {
440
      $form[$field]['widget'][0]['value']['#type'] = 'color';
441
    }
442
    $form[$field]['#attributes']['class'][] = 'large-4 columns';
443
    $form['style_fields'][] = $form[$field];
444
    $form['style_fields']['#weight'] = $form[$field]['#weight'];
445
    unset($form[$field]);
446
  }
447
}
448
449
/**
450
 * Does nit-picky changes on the Hero image entity browser.
451
 *
452
 * @param array $element
453
 *   The element render array.
454
 *
455
 * @return array
456
 *   The possibly modified form render array.
457
 */
458
function _df_tools_blocks_hero_browser_button(array $element) {
459
  $element['entity_browser']['open_modal']['#value'] = t('Select background');
460
  if (isset($element['entity_browser']['open_modal']['#ajax'])) {
461
    /** @var \Drupal\entity_browser\DisplayInterface $display */
462
    $display = $element['entity_browser']['open_modal']['#ajax']['callback'][0];
463
    $configuration = $display->getConfiguration();
464
    $configuration['link_text'] = t('Background media');
465
    $display->setConfiguration($configuration);
466
  }
467
  return $element;
468
}
469
470
/**
471
 * Callback to re-sort blocks into categories.
472
 *
473
 * @param array &$block_info
474
 *   An array of block plugin definitions.
475
 */
476
function df_tools_blocks_alter_block_categories(array &$block_info) {
477
  // Create an associative array which maps default => custom block categories.
478
  $category_map = [
479
    'Embed' => t('Existing Content'),
480
    'Entity Block' => t('Existing Content'),
481
    'Commerce' => t('Forms'),
482
    'Lists (Views)' => t('Lists'),
483
    'Listes (Views)' => t('Lists'),
484
    'Listas (Views)' => t('Lists'),
485
    'Views' => t('Lists'),
486
    'AddToAny' => t('Social'),
487
    'Product hero' => t('Hero'),
488
    'Workbench moderation' => t('Hidden'),
489
    'User' => t('Hidden'),
490
    'Utilisateur' => t('Hidden'),
491
    'Usuario' => t('Hidden'),
492
    'Chaos tools' => t('Hidden'),
493
    'Help' => t('Hidden'),
494
    'core' => t('Hidden'),
495
    'System' => t('Hidden'),
496
    'Moderation Dashboard' => t('Hidden'),
497
    'Entity Browser' => t('Hidden'),
498
    'Facets' => t('Hidden'),
499
    'Facets summary (Experimental)' => t('Hidden'),
500
    'Responsive Preview' => t('Hidden'),
501
    'DF Tools Blocks' => t('Hidden'),
502
  ];
503
504
  $label_map = [
505
    'Content Embed' => t('Content'),
506
    'Media Embed' => t('Media'),
507
  ];
508
509
  foreach ($block_info as $key => $info) {
510
    // Retrieve the name of the block category.
511
    $category = $info['category'];
512
    $label = $info['label'];
513
514
    // Retrieve the untranslated name of the category if it has been translated.
515
    if ($category instanceof TranslatableMarkup) {
516
      $category = $category->getUntranslatedString();
517
    }
518
    if ($label instanceof TranslatableMarkup) {
519
      $label = $label->getUntranslatedString();
520
    }
521
522
    // Move all fields of the currently panelized entity into a
523
    // 'Current @Entity_Type' category. If the block is not a field on the
524
    // current entity, allow its category to be remapped.
525
    if ($category === '@entity') {
526
      $block_info[$key]['category'] = $info['category']->render() . ' Fields';
527
    }
528
    // Place Block Content entities into categories based on their type.
529
    elseif ($category === 'Custom') {
530
      list($type, $uuid) = explode(':', $block_info[$key]['plugin_id']);
531
      unset($type);
532
      $ids = \Drupal::entityQuery('block_content')
533
        ->condition('uuid', $uuid, '=')
534
        ->execute();
535
      if ($block = BlockContent::load(reset($ids))) {
0 ignored issues
show
Bug introduced by
It seems like $ids can also be of type integer; however, parameter $array of reset() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

535
      if ($block = BlockContent::load(reset(/** @scrutinizer ignore-type */ $ids))) {
Loading history...
536
        if ($bundle = BlockContentType::load($block->bundle())) {
537
          $block_info[$key]['category'] = $bundle->label();
538
        }
539
      }
540
    }
541
542
    if (isset($block_info[$key])) {
543
      if (isset($category_map[$category])) {
544
        // Already translated.
545
        $block_info[$key]['category'] = $category_map[$category];
546
      }
547
      elseif (is_string($block_info[$key]['category'])) {
548
        // Already translated.
549
        $block_info[$key]['category'] = $block_info[$key]['category'];
550
      }
551
      if (isset($label_map[$label])) {
552
        // Already translated.
553
        $block_info[$key]['label'] = $label_map[$label];
554
      }
555
      elseif (is_string($block_info[$key]['label'])) {
556
        // Already translated.
557
        $block_info[$key]['label'] = $block_info[$key]['label'];
558
      }
559
    }
560
561
    if ($label === 'Entity gallery' && $category === 'Entity Block') {
562
      $block_info[$key]['category'] = t('Hidden');
563
    }
564
565
    if ($label === 'Entity Gallery Browser') {
566
      $block_info[$key]['category'] = t('Existing Content');
567
      $block_info[$key]['label'] = t('Gallery');
568
    }
569
  }
570
}
571
572
/**
573
 * Implements hook_panels_ipe_blocks_alter().
574
 *
575
 * Improves Panelizer/Panels IPE UX by removing fields that cannot be placed.
576
 */
577
function df_tools_blocks_panels_ipe_blocks_alter(&$block_info) {
578
  // Re-sort blocks into specific categories.
579
  df_tools_blocks_alter_block_categories($block_info);
580
  // Filter out fields that aren't relevant for this entity.
581
  $request = \Drupal::request();
582
  $storage_id = str_replace('*', '', $request->attributes->get('panels_storage_id', ''));
583
  list($entity_type, $id) = explode(':', $storage_id);
584
  $entity_type_manager = \Drupal::entityTypeManager();
585
  if (!empty($entity_type) && !empty($id) && $entity_type_manager->hasDefinition($entity_type)) {
586
    if ($entity = $entity_type_manager->getStorage($entity_type)->load($id)) {
587
      foreach ($block_info as $key => $info) {
588
        if ($info['id'] === 'entity_field') {
589
          list($block_id, $block_entity_type, $field_name) = explode(':', $info['plugin_id']);
590
          unset($block_id);
591
          if ($block_entity_type !== $entity_type || ($entity instanceof FieldableEntityInterface && !$entity->hasField($field_name))) {
592
            unset($block_info[$key]);
593
          }
594
        }
595
      }
596
    }
597
  }
598
  $block_info = array_values($block_info);
599
}
600
601
/**
602
 * Implements hook_theme().
603
 */
604
function df_tools_blocks_theme($existing, $type, $theme, $path) {
0 ignored issues
show
Unused Code introduced by
The parameter $existing is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

604
function df_tools_blocks_theme(/** @scrutinizer ignore-unused */ $existing, $type, $theme, $path) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

604
function df_tools_blocks_theme($existing, /** @scrutinizer ignore-unused */ $type, $theme, $path) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

604
function df_tools_blocks_theme($existing, $type, $theme, /** @scrutinizer ignore-unused */ $path) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $theme is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

604
function df_tools_blocks_theme($existing, $type, /** @scrutinizer ignore-unused */ $theme, $path) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
605
  return [
606
    'code_block' => [
607
      'variables' => ['code' => NULL],
608
    ],
609
  ];
610
}
611