-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacy_me_style_options.module
119 lines (111 loc) · 4.67 KB
/
legacy_me_style_options.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\paragraphs\ParagraphInterface;
/**
* @file
* Legacy ME Style Options module.
*/
/**
* Implements hook_preprocess_layout_paragraphs_component_menu().
*/
function legacy_me_style_options_preprocess_layout_paragraphs_builder_component_menu(&$variables) {
foreach (['layout', 'content'] as $category) {
if (isset($variables['types'][$category])) {
$new_types = [];
foreach ($variables['types'][$category] as $bundle => $info) {
$new_types[$bundle] = $info;
$options = \Drupal::service('style_options.discovery')->getContextOptions('paragraphs', $bundle);
foreach (array_keys($options) as $option_id) {
$definition = \Drupal::service('style_options.discovery')->getOptionDefinition($option_id);
if ($definition['plugin'] == 'template_suggestion') {
foreach ($definition['options'] as $key => $option) {
if ($key !== $definition['default']) {
$new_types[$bundle . '__' . $key] = $info;
$new_types[$bundle . '__' . $key]['id'] = $bundle . '__' . $key;
$new_types[$bundle . '__' . $key]['label'] = $option['label'];
$new_types[$bundle . '__' . $key]['url'] .= (strpos($info['url'], '?') !== FALSE ? '&' : '?') . $option_id . '=' . $key;
$new_types[$bundle . '__' . $key]['link_attributes'] = clone($info['link_attributes']);
$new_types[$bundle . '__' . $key]['link_attributes']->setAttribute('title', t('Create new :type', [':type' => $option['label']]));
}
}
}
}
}
$variables['types'][$category] = $new_types;
}
}
}
/**
* Implements hook_preprocess_layout_paragraphs_builder_controls().
*
* Change the component name in the controls widget to the template suggestion.
*/
function legacy_me_style_options_preprocess_layout_paragraphs_builder_controls(&$variables) {
/** @var \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout */
$layout = $variables['layout_paragraphs_layout'];
$component = $layout->getComponentByUuid($variables['uuid']);
$paragraph = $component->getEntity();
$context_options = \Drupal::service('style_options.discovery')
->getContextOptions('paragraphs', $paragraph->bundle());
foreach (array_keys($context_options) as $option) {
$context_defition = \Drupal::service('style_options.discovery')
->getOptionDefinition($option);
if ($context_defition['plugin'] == 'template_suggestion') {
$behavior_settings = $paragraph->getAllBehaviorSettings();
if (!isset($behavior_settings['style_options'])) {
return;
}
$style_options = $behavior_settings['style_options'];
if (isset($style_options[$option])) {
$key = $style_options[$option]['template_suggestion'];
$variables['controls']['label']['#value'] = $context_defition['options'][$key]['label'];
}
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter
*/
function legacy_me_style_options_theme_suggestions_paragraph_alter(array &$suggestions, array $variables) {
/** @var Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['elements']['#paragraph'];
if (empty($paragraph)) {
return;
}
$behavior_settings = $paragraph->getAllBehaviorSettings();
if (isset($behavior_settings['style_options'])) {
$template_suggestions = array_filter($behavior_settings['style_options'], function ($option) {
return isset($option['template_suggestion']);
});
$template_suggestions = array_map(function ($option) {
return $option['template_suggestion'];
}, $template_suggestions);
foreach ($template_suggestions as $template_suggestion) {
$suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $template_suggestion;
}
return $suggestions;
}
}
/**
* Implements hook_entity_view_mode_alter().
*/
function legacy_me_style_options_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
if (!$entity instanceof ParagraphInterface) {
return;
}
$behavior_settings = $entity->getAllBehaviorSettings();
$style_options = $behavior_settings['style_options'] ?? [];
foreach ($style_options as $option_id => $style_option) {
if (isset($style_option['display_mode'])) {
$definition = \Drupal::service('style_options.discovery')->getOptionDefinition($option_id);
$config = $definition['options'][$style_option['display_mode']];
$view_mode = $config['view_mode'];
}
}
}
/**
* Implements hook_preprocess_paragraph().
*/
function legacy_me_style_options_preprocess_paragraph(&$variables) {
$variables['#attached']['library'][] = 'legacy_me_style_options/style_options';
}