157 lines
5.9 KiB
PHP
157 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* @file
|
|
* Paragraph-related functions.
|
|
*/
|
|
|
|
use Drupal\node\NodeInterface;
|
|
use Drupal\taxonomy\Entity\Term;
|
|
|
|
/**
|
|
* Implements Implements hook_preprocess_paragraph().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph(&$variables) {
|
|
// allow page_type and sub_page_type variables
|
|
// to be used within Twig to control styling on a per
|
|
// paragraph basis, e.g., currently used for TAR pages.
|
|
$paragraph = $variables['elements']['#paragraph'];
|
|
$renderer = \Drupal::service('renderer');
|
|
$parent = $paragraph->getParentEntity();
|
|
$renderer->addCacheableDependency($variables, $parent);
|
|
// Check if the node is on a page with the page type field.
|
|
if ($parent instanceof NodeInterface &&
|
|
$parent->bundle() == 'page' &&
|
|
$parent->hasField('field_page_type') &&
|
|
!$parent->get('field_page_type')->isEmpty()
|
|
) {
|
|
$pageTypes = $parent->get('field_page_type')->referencedEntities();
|
|
$pageTypeTerm = reset($pageTypes);
|
|
$renderer->addCacheableDependency($variables, $pageTypeTerm);
|
|
// If the term is a child term, set the parent to page type.
|
|
if ($pageTypeTerm->parent->target_id !== "0") {
|
|
$parentTerm = Term::load($pageTypeTerm->parent->target_id);
|
|
$renderer->addCacheableDependency($variables, $parentTerm);
|
|
$variables['page_type'] = $parentTerm->getName();
|
|
$variables['sub_page_type'] = $pageTypeTerm->getName();
|
|
}
|
|
// Otherwise, set the term to page type.
|
|
else {
|
|
$variables['page_type'] = $pageTypeTerm->getName();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_paragraph__PARAGRAPH_TYPE().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph__excerpt(&$variables) {
|
|
$content = &$variables['content'];
|
|
|
|
// check for field_url and update the link text to 'View Source'
|
|
// when the link text value contains the same value as the URL,
|
|
// which is the default behavior when no link text is provided.
|
|
if (isset($content['field_url']) & !empty($content['field_url'])) {
|
|
if (isset($content['field_url'][0]['#title'])) {
|
|
$urlString = $content['field_url'][0]['#url']->toString();
|
|
if ($content['field_url'][0]['#title'] == $urlString) {
|
|
$content['field_url'][0]['#title'] = 'View Source';
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_paragraph__PARAGRAPH_TYPE().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph__media_item(&$variables) {
|
|
$content = &$variables['content'];
|
|
$parent = $variables['elements']['#paragraph']->getParentEntity();
|
|
if (isset($content['field_caption_hide'][0]['#markup'])) {
|
|
switch ($content['field_caption_hide'][0]['#markup']) {
|
|
case 'No':
|
|
if (!isset($content['field_caption'][0])) {
|
|
// check for media and if the field body is not empty
|
|
if (isset($content['field_media'][0])) {
|
|
$media = $content['field_media'][0]['#media'];
|
|
if ($media->hasField('field_body') && !$media->get('field_body')->isEmpty()) {
|
|
$field_body = $media->get('field_body')->getValue();
|
|
$paragraph = &$variables['paragraph'];
|
|
// copy field body to field caption
|
|
$paragraph->field_caption->value = $field_body[0]['value'];
|
|
// render field_caption with parent view_mode
|
|
$content['field_caption'] = $paragraph->field_caption->view($variables['view_mode']);
|
|
$content['field_caption'][0]['#format'] = 'limited_html';
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case 'Yes':
|
|
// hide caption is checked
|
|
if (isset($content['field_caption'][0])) {
|
|
unset($content['field_caption'][0]);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
// Remove the default alignment from media items unless added to a Basic
|
|
// paragraph.
|
|
if ($parent->getEntityTypeId() != 'paragraph' ||
|
|
$parent->bundle() != 'basic') {
|
|
$content['field_media_alignment'] = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_paragraph__PARAGRAPH_TYPE().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph__header(&$variables) {
|
|
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
|
|
$paragraph = $variables['paragraph'];
|
|
$parent = $paragraph->getParentEntity();
|
|
|
|
if ($parent->hasField('field_date') && $parent->get('field_date')->count() > 0) {
|
|
$date = $parent->get('field_date')->get(0)->getValue();
|
|
// See /admin/config/regional/date-time for date formats.
|
|
$variables['header_date'] = \Drupal::service('date.formatter')->format(strtotime($date['value']), 'header_date');
|
|
}
|
|
|
|
// Get the URL of the parent node for teaser headers.
|
|
if ($variables['view_mode'] == 'teaser_header') {
|
|
if ($parent instanceof NodeInterface) {
|
|
$variables['node_url'] = $parent->toUrl()->toString();
|
|
$renderer = \Drupal::service('renderer');
|
|
$renderer->addCacheableDependency($variables, $parent);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_paragraph__PARAGRAPH_TYPE().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph__horizontal_tabs(&$variables) {
|
|
// Get title fields of all nested tabs for use in the tab list,
|
|
// and set a unique prefix for tab element IDs to avoid collisions when
|
|
// multiple tabs are included on a page.
|
|
$tabs = \Drupal\Core\Render\Element::children($variables['content']['field_tabs']);
|
|
$tabId = \Drupal\Component\Utility\Html::getUniqueId('tab');
|
|
foreach ($tabs as $tabIndex) {
|
|
$paragraph = $variables['content']['field_tabs'][$tabIndex]['#paragraph'];
|
|
if ($paragraph->hasField('field_title')) {
|
|
$title = $paragraph->get('field_title');
|
|
$variables['content']['tab_titles'][$tabIndex] = $title->view('horizontal_tab');
|
|
$variables['content']['tab_titles'][$tabIndex]['#id_prefix'] = $tabId;
|
|
}
|
|
$variables['content']['field_tabs'][$tabIndex]['#id_prefix'] = $tabId;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_paragraph__PARAGRAPH_TYPE().
|
|
*/
|
|
function cdesai_emulsify_preprocess_paragraph__tab__horizontal_tab(&$variables) {
|
|
if (!empty($variables['elements']['#id_prefix'])) {
|
|
$variables['id_prefix'] = $variables['elements']['#id_prefix'];
|
|
}
|
|
}
|