-
Notifications
You must be signed in to change notification settings - Fork 0
/
paragraphs_overrides.module
143 lines (116 loc) · 3.89 KB
/
paragraphs_overrides.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Implements hook_theme()
*
* Register a theme for each paragraph type
*
*/
function paragraphs_overrides_theme($existing, $type, $theme, $path) {
$theme_templates = [];
$paragraph_types = paragraphs_overrides_get_paragraph_types();
// Register custom Paragraph bundle templates
foreach ($paragraph_types as $paragraph_type) {
$theme_templates['paragraph__paragraphs_overrides__' . $paragraph_type] = [
'base hook' => 'paragraph',
];
}
// Register custom Paragraph bundle template fallback
$theme_templates['paragraph__paragraphs_overrides'] = [
'base hook' => 'paragraph',
];
// Register custom Paragraph field wrapper
$theme_templates['field__paragraph__paragraphs_overrides__wrapper'] = [
'base hook' => 'field',
];
// Register custom Paragraph field wrapper
$theme_templates['field__paragraph__paragraphs_overrides__components_container'] = [
'base hook' => 'field',
];
// Register custom Paragraph field wrapper
$theme_templates['field__paragraph__paragraphs_overrides__lp_components_container'] = [
'base hook' => 'field',
];
// Navigation theme
$theme_templates['site_navigation_experience'] = [
'variables' => [],
'template' => 'site-navigation-experience',
];
return $theme_templates;
}
/**
* Implements HOOK_theme_suggestions_HOOK_alter
*
* Field wrapper template override suggestion
*/
function paragraphs_overrides_theme_suggestions_field_alter(array &$suggestions, array $variables, $hook) {
$field = $variables['element']['#field_name'];
// Body field wrapper
// if ($field == 'field_basic_page_body') {
// $suggestions[] = 'field__paragraph__paragraphs_overrides__wrapper';
// }
// Case study field override
if ($field == 'field_components') {
$suggestions[] = 'field__paragraph__paragraphs_overrides__components_container';
}
// Landing page field override
if ($field == 'field_lp_components') {
$suggestions[] = 'field__paragraph__paragraphs_overrides__lp_components_container';
}
}
/**
* Implements HOOK_theme_suggestions_HOOK_alter
*
* Paragraph type template suggestions
*
*/
function paragraphs_overrides_theme_suggestions_paragraph_alter(&$suggestions, $variables) {
$entity = $variables['elements']['#paragraph'];
$type = $entity->getType();
$suggestions[] = 'paragraph__paragraphs_overrides';
$suggestions[] = 'paragraph__paragraphs_overrides__' . $type;
}
/**
* Implements hook_theme_registry_alter()
*
* Register these (above) templates with Drupal
*
*/
function paragraphs_overrides_theme_registry_alter(&$theme_registry) {
$module_path = drupal_get_path('module', 'paragraphs_overrides');
$template_objects = drupal_find_theme_templates($theme_registry, '.html.twig', $module_path);
$paragraph_types = paragraphs_overrides_get_paragraph_types();
// 1. Loop through the paragraph types
// 2. Check if each paragraph exists in the `$template_objects` array
// 3. If it doesn't exist, remove it from the registry so we don't get any errors
// 4. If it does exist, set actual path to template
foreach ($paragraph_types as $type) {
$template = 'paragraph__paragraphs_overrides__' . $type;
if (!isset($template_objects[$template])) {
unset($theme_registry['paragraph__paragraphs_overrides__' . $type]);
}
else {
$theme_registry['paragraph__paragraphs_overrides__' . $type]['path'] = $template_objects[$template]['path'];
}
}
}
/**
* Helper function to get a list of paragraph types by machine name
*
* @return array
*/
function paragraphs_overrides_get_paragraph_types() {
$paragraph_bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo('paragraph');
return array_keys($paragraph_bundles);
}
/**
* Implements hook_preprocess_HOOK().
*/
function paragraphs_overrides_preprocess_paragraph(&$variables) {
$entity = $variables['elements']['#paragraph'];
$type = $entity->getType();
$function = $type . "__override";
if (function_exists($function)) {
$variables = $function($variables);
}
}