modules/stanford_news/stanford_news.module
<?php
/**
* @file
* stanford_news.module
*/
use Drupal\views\ViewExecutable;
use Drupal\node\NodeInterface;
use Drupal\views\Plugin\views\cache\CachePluginBase;
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;
}
if ($node->hasField('su_news_hide_social')) {
$attachments['#attached']['drupalSettings']['stanfordNews'] = [
'hideSocial' => (bool) $node->get('su_news_hide_social')->getString(),
];
}
$attachments['#attached']['library'][] = 'stanford_news/news_node';
}
/**
* 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($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($variables['button']['#value']) && $variables['button']['#value'] != $child_element['#stanford_news_topics_add_more']) {
$variables['button']['#value'] = $child_element['#stanford_news_topics_add_more'];
}
}
}
}