-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstanford_news.module
187 lines (161 loc) · 6.21 KB
/
stanford_news.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @file
* stanford_news.module
*/
use Drupal\views\ViewExecutable;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\Cache;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_theme().
*/
function stanford_news_theme() {
return [
'field__node__su_news_topics' => [
'template' => 'field/field--node--su-news-topics',
'base hook' => 'field',
],
'signup_block' => [
'variables' => [
'form_action' => NULL,
],
'template' => 'block/signup-block',
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function stanford_news_preprocess_field__su_news_dek(&$variables) {
$variables['attributes']['class'][] = 'flex-10-of-12';
}
/**
* Implements hook_preprocess_block().
*/
function stanford_news_preprocess_block(&$variables) {
// Attach Library to the signup block wherever it goes.
if (!empty($variables['elements']['#id'])) {
if ($variables['elements']['#id'] == 'newslettersignup') {
$variables['#attached']['library'][] = 'stanford_news/newsletter_signup';
}
}
}
/**
* Implements hook_preprocess_node().
*/
function stanford_news_page_attachments(array &$attachments) {
// Get the node from the route.
$node = \Drupal::routeMatch()->getParameter('node');
// Not a node.. Then just continue.
if (!$node instanceof NodeInterface || $node->bundle() != 'stanford_news') {
return;
}
$attachments['#attached']['library'][] = 'stanford_news/news_node';
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function stanford_news_node_presave(EntityInterface $entity) {
if ($entity->bundle() != "stanford_news") {
return;
}
$rabbit_hole = \Drupal::config('rabbit_hole.behavior_settings.node_type_' . $entity->bundle());
$token_service = \Drupal::service('token');
// When the source field is filled out on a news item we want to set the
// redirect options so it doesn't get indexed.
if ($rabbit_hole) {
/** @var \Drupal\Core\Utility\Token $token_service */
$target = $token_service->replace("[node:su_news_source:uri]", ['node' => $entity]);
// Check if the token that was provided is actually a url. If it is, then we
// can set the rabbit hole action to redirect to that url.
if (is_string($target) && UrlHelper::isValid($target, TRUE)) {
$entity->set('rh_action', 'page_redirect');
$entity->set('rh_redirect', $target);
$entity->set('rh_redirect_response', 301);
}
else {
$entity->set('rh_action', 'bundle_default');
$entity->set('rh_redirect', '');
}
}
// If a news item is being edited or saved, clear out some cache tags.
Cache::invalidateTags(["node_list:{$entity->bundle()}"]);
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function stanford_news_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Only run on stanford news items.
if ($entity->bundle() !== "stanford_news" || !node_is_page($entity)) {
return;
}
$rh_action = $entity->get('rh_action')->getString();
$rh_redirect = $entity->get('rh_redirect')->getString();
// Display a message to the user that this page would redirect to another
// location for other users.
if ($rh_action == "page_redirect" && !empty($rh_redirect)) {
\Drupal::messenger()
->addWarning(t('This page will redirect to @url for other visitors.', ['@url' => $rh_redirect]));
}
}
/**
* Implements hook_views_post_render().
*
* Views render arrays contain a cache tag "node_list". This cache tag is
* cleared every time ANY node is created, edited or deleted. When this happens
* every view on the site gets its cache flushed. This causes poor performance
* since a view would get flushed even if it has no relation to that node. To
* assist in cache tags, we create a custom cache tag based on the node type
* filter on the view. Its a small improvement but will have huge impact in
* keeping cached renders much longer.
*
* @see stanford_person_node_presave()
* @see stanford_person_taxonomy_term_presave ()
*/
function stanford_news_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
// Node Base Table Views.
if ($view->storage->id() == 'stanford_news') {
$output['#attached']['library'][] = 'stanford_news/news_list';
$node_list_position = array_search('node_list', $output['#cache']['tags']);
unset($output['#cache']['tags'][$node_list_position]);
foreach ($view->filter['type']->value as $node_type) {
$output['#cache']['tags'][] = "node_list:$node_type";
}
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function stanford_news_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
$field = $context['items']->getFieldDefinition();
if ($field instanceof FieldConfigInterface && $field->id() == "node.stanford_news.su_news_topics") {
$element['#stanford_news_topics_add_more'] = t('Add another term');
}
}
/**
* Implements hook_preprocess_field_multiple_value_form().
*
* We look for a value that was placed there earlier by
* stanford_news_field_widget_form_alter() and change the add_more button
* to use that.
*/
function stanford_news_preprocess_field_multiple_value_form(&$variables) {
foreach (Element::children($variables['element']) as $child) {
$child_element = &$variables['element'][$child];
if (isset($child_element['#stanford_news_topics_add_more'])) {
if (isset($child_element['#stanford_news_topics_add_more']) && isset($variables['element']['add_more']['#value']) && $variables['element']['add_more']['#value'] != $child_element['#stanford_news_topics_add_more']) {
$variables['element']['add_more']['#value'] = $child_element['#stanford_news_topics_add_more'];
}
if (isset($child_element['#stanford_news_topics_add_more']) && isset($variables['button']['#value']) && $variables['button']['#value'] != $child_element['#stanford_news_topics_add_more']) {
$variables['button']['#value'] = $child_element['#stanford_news_topics_add_more'];
}
}
}
}