forked from CornellCustomDev/cwd_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwd_base.theme
executable file
·144 lines (127 loc) · 6.34 KB
/
cwd_base.theme
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
143
144
<?php
/* Make 'is_front' available to the an additional template (call from the associated preprocess)
-------------------------------------------------------------------------- */
function cwd_base_is_front(&$variables) {
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (Exception $e) {
$variables['is_front'] = FALSE;
}
$variables['#cache']['contexts'][] = 'url.path.is_front'; // Ensure the cache varies correctly. https://www.drupal.org/node/2830442
}
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function cwd_base_preprocess_html(&$variables) {
$variables['base_path'] = base_path();
cwd_base_is_front($variables); // make 'is_front' available to the html.html template
/* Make system.site.name and system.site.slogan available to html.html template
------------------------------------------------------------------------ */
$site_config = \Drupal::config('system.site');
$variables['site_name'] = $site_config->get('name');
$variables['site_slogan'] = $site_config->get('slogan');
/* If the node object is present, make node data available to the html.html template
------------------------------------------------------------------------ */
if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {
$variables['node'] = $node;
}
/* If in a 401/403/404 error state, make a boolean available to the html.html template
------------------------------------------------------------------------ */
$is_error_page = false;
$status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if ($status) { // If we later want specific codes, we can add: "if ($status->getStatusCode() == 404)"
$is_error_page = true;
}
$variables['is_error_page'] = $is_error_page;
/* Get Section Title (top-most parent, under home)
------------------------------------------------------------------------ */
$section_title = $section_path = NULL;
$path_items = explode('/',\Drupal::request()->getRequestUri()); // generate an array of path items
$section_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/'. $path_items[1]); // array index 1 will always return the top level under root
$request = \Symfony\Component\HttpFoundation\Request::create($section_alias);
$router = \Drupal::service('router.no_access_checks'); // router.matcher won't work for nodes below, since the values object is protected
try {
$match = $router->matchRequest($request);
if (array_key_exists('node',$match)) { // nodes
$section_title = $match['node']->getTitle();
$section_path = $section_alias;
}
else if (array_key_exists('_title',$match)) { // non-nodes (/user, views content, etc)
$section_title = $match['_title'];
$section_path = $section_alias;
}
}
catch (\Exception $e) {
// No route was found, variables will be left NULL
}
$variables['section_title'] = $section_title;
$variables['section_path'] = $section_path;
}
/**
* Implements hook_preprocess_HOOK() for node templates.
*/
function cwd_base_preprocess_node(&$variables) {
cwd_base_is_front($variables); // make 'is_front' available to the node.html template
}
/**
* The following functions are adding twig suggestions that are already provided
* by core but not presented in twig debug.
-------------------------------------------------------------------------- */
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function cwd_base_theme_suggestions_block_alter(array &$suggestions, array $variables)
{
if (isset($variables['elements']['content']['#block_content'])) {
array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
$block = \Drupal\block\Entity\Block::load($variables['elements']['#id']);
array_splice($suggestions, 2, 0, 'block__bundle__' . $block->getRegion() . "__" . $variables['elements']['content']['#block_content']->bundle());
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function cwd_base_theme_suggestions_views_view_fields_alter(array &$suggestions, array $variables)
{
if (isset($variables['view'])) {
array_splice($suggestions, 1, 0, 'views_view_fields__row__' . $variables['view']->id());
array_splice($suggestions, 1, 0, 'views_view_fields__row__' . $variables['view']->id() . '__'. $variables['view']->current_display);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function cwd_base_theme_suggestions_views_view_unformatted_alter(array &$suggestions, array $variables)
{
if (isset($variables['view'])) {
array_splice($suggestions, 1, 0, 'views_view_unformatted__' . $variables['view']->id());
array_splice($suggestions, 1, 0, 'views_view_unformatted__' . $variables['view']->id() . '__'. $variables['view']->current_display);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Not just adding to twig debug -- these template names aren't available OOTB.
* TO DO: I'm not really sure my statement in the previous line is true... why would this function add template support but the previous functions somehow don't...?
*/
function cwd_base_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $variables['elements']['#taxonomy_term'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
// Add view mode theme suggestions.
$suggestions[] = 'taxonomy_term__' . $sanitized_view_mode;
$suggestions[] = 'taxonomy_term__' . $term->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'taxonomy_term__' . $term->id() . '__' . $sanitized_view_mode;
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* (Again) Not just adding to twig debug -- these template names aren't available OOTB.
* TO DO: (Again) I'm not really sure my statement in the previous line is true... why would this function add template support but the previous functions somehow don't...? -- but I _am_ sure that without this function, page--node--CTYPE templates don't get picked up.
*/
function cwd_base_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {
$suggestions[] = 'page__node__' . $node->bundle();
}
}