thunder_fia.module in Thunder 8.2
Same filename and directory in other branches
Thunder Article module hooks.
File
modules/thunder_article/modules/thunder_fia/thunder_fia.moduleView source
<?php
/**
* @file
* Thunder Article module hooks.
*/
use Drupal\Core\Render\Element;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Implements hook_theme().
*/
function thunder_fia_theme($existing, $type, $render, $path) {
$theme = [];
$files = scandir(DRUPAL_ROOT . '/' . $path . '/templates');
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$file = str_replace('-', '_', $file);
$file = basename($file, '.html.twig');
$base = [];
preg_match('/([a-z]*)_/', $file, $base);
$theme[$file] = [
'path' => $path . '/templates',
'base hook' => $base[1],
];
}
return $theme;
}
/**
* Implements hook_preprocess_views_view_row_fia().
*/
function thunder_fia_preprocess_views_view_row_fia(&$variables) {
if ($variables['row']['#node'] instanceof Node) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['row']['#node'];
// Add teaser image to fia.
if ($media = thunder_fia_get_first_paragraph_image_entity($node)) {
$render_controller = \Drupal::entityTypeManager()
->getViewBuilder($media
->getEntityTypeId());
$entityRender = $render_controller
->view($media, 'facebook_instant_articles_rss', NULL);
$variables['options']['figure'] = $entityRender;
}
if (empty($variables['options']['figure']) && ($field_teaser_media = $node->field_teaser_media)) {
/** @var \Drupal\media_entity\Entity\Media $entity */
$entity = $field_teaser_media->entity;
if (isset($entity)) {
$render_controller = \Drupal::entityTypeManager()
->getViewBuilder($entity
->getEntityTypeId());
$entityRender = $render_controller
->view($entity, 'facebook_instant_articles_rss', NULL);
$variables['options']['figure'] = $entityRender;
}
}
// Change author name to "firstname lastname".
$owner = $node
->getOwner();
if (isset($owner->first_name, $owner->last_name)) {
$name = $owner->first_name->value . ' ' . $owner->last_name->value;
if ($name = trim($name) && !empty($variables['options']['author'])) {
$variables['options']['author'] = [
'#markup' => '<a>' . $name . '</a>',
];
}
}
}
}
/**
* Implements hook_preprocess_node().
*/
function thunder_fia_preprocess_node(&$variables) {
/*
* @var string $view_mode
* the view mode name
*/
$view_mode = $variables['view_mode'] = $variables['elements']['#view_mode'];
if ($view_mode == 'fb_instant_articles_rss') {
/*
* If we are looking at a node using the fia rss view-mode, then only show
* field values for entities that are also in an fia view mode
*/
if (is_array($variables['content'])) {
foreach ($variables['content'] as $key => $content) {
if (isset($content['#theme']) && $content['#theme'] == 'field') {
foreach (Element::children($content) as $index) {
$value = $content[$index];
if (empty($value['#view_mode'])) {
continue;
}
switch ($value['#view_mode']) {
case 'facebook_instant_articles_rss':
break;
default:
$variables['content'][$key][$index]['#access'] = FALSE;
}
}
}
}
// If first paragraph is the teaser image, don't render it.
if (thunder_fia_get_first_paragraph_image_entity($variables['node'])) {
$variables['content']['field_paragraphs'][0]['#access'] = FALSE;
}
}
}
}
/**
* Implements hook_preprocess_paragraph().
*/
function thunder_fia_preprocess_paragraph(&$variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
if ($variables['view_mode'] == "facebook_instant_articles_rss" && $paragraph instanceof Paragraph) {
// Force 'basic_html' for text paragraphs.
if ($paragraph
->getType() == 'text') {
$variables['content']['field_text'][0]['#format'] = "basic_html";
}
}
}
/**
* Checks, if the first paragraph of a node is an image and returns it.
*
* @param \Drupal\node\Entity\Node $node
* The node, which should be searched.
*
* @return bool|Drupal\media_entity\Entity\Media
* Returns false, if first paragraph is not an image,
* or the according media entity.
*/
function thunder_fia_get_first_paragraph_image_entity(Node $node) {
if (isset($node->field_paragraphs[0]) && ($paragraph = $node->field_paragraphs[0]->entity)) {
if ($paragraph
->getType() == 'image' && isset($paragraph->field_image->entity) && $paragraph->field_image->entity
->bundle() == 'image') {
return $paragraph->field_image->entity;
}
}
return FALSE;
}
Functions
Name | Description |
---|---|
thunder_fia_get_first_paragraph_image_entity | Checks, if the first paragraph of a node is an image and returns it. |
thunder_fia_preprocess_node | Implements hook_preprocess_node(). |
thunder_fia_preprocess_paragraph | Implements hook_preprocess_paragraph(). |
thunder_fia_preprocess_views_view_row_fia | Implements hook_preprocess_views_view_row_fia(). |
thunder_fia_theme | Implements hook_theme(). |