sample files from a smithsonian project
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Block-related functions.
|
||||
*/
|
||||
|
||||
use Drupal\block\Entity\Block;
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_block().
|
||||
* http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_block(&$variables) {
|
||||
if(isset($variables['attributes']['id'])) {
|
||||
$variables['content']['#attributes']['block'] = $variables['attributes']['id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme_suggestions_HOOK_alter().
|
||||
*/
|
||||
function cdesai_emulsify_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
|
||||
// Remove the block and replace dashes with underscores in the block ID to
|
||||
// use for the hook name.
|
||||
if (isset($variables['attributes']['block'])) {
|
||||
$hook = str_replace(array('block-', '-'), array('', '_'), $variables['attributes']['block']);
|
||||
$suggestions[] = $variables['theme_hook_original'] . '__' . $hook;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements hook_theme_suggestions_field_alter().
|
||||
*/
|
||||
function cdesai_emulsify_theme_suggestions_field_alter(array &$suggestions, array $variables) {
|
||||
$suggestions[] = 'field__' . $variables['element']['#entity_type'] . '__' . $variables['element']['#field_name'] . '__' . $variables['element']['#view_mode'];
|
||||
$suggestions[] = 'field__' . $variables['element']['#entity_type'] . '__' . $variables['element']['#field_name'] . '__' . $variables['element']['#bundle'] . '__' . $variables['element']['#view_mode'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_field__HOOK().
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_field__paragraph__field_title__horizontal_tab(&$variables) {
|
||||
if (!empty($variables['element']['#id_prefix'])) {
|
||||
$variables['id_prefix'] = $variables['element']['#id_prefix'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Node-related functions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_node__BUNDLE().
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_node__event(&$variables) {
|
||||
// final date to be used - if no end_date available, then use start_date
|
||||
$date_to_use = NULL;
|
||||
$content = $variables['content'];
|
||||
$view_mode = $variables['view_mode'];
|
||||
|
||||
// check to see if a date has been entered at all
|
||||
if (isset($content['field_daterange'][0])) {
|
||||
|
||||
if (isset($start_date)) {
|
||||
// set date to use as start date, but if end date is available use that instead.
|
||||
$date_to_use = $start_date;
|
||||
if (!empty($end_date)) {
|
||||
$date_to_use = $end_date;
|
||||
}
|
||||
|
||||
// convert date_to_use string to date object
|
||||
$dateToUseObj = DateTime::createFromFormat("l, F j, Y - h:ia", $date_to_use);
|
||||
if ($dateToUseObj) {
|
||||
// convert date object to timestamp
|
||||
$date_timestamp = $dateToUseObj->getTimestamp();
|
||||
// convert current time to timestamp
|
||||
$current_timestamp = strtotime("now");
|
||||
// returns TRUE if past event, FALSE otherwise
|
||||
$variables['event_is_past'] = $current_timestamp > $date_timestamp ?: FALSE;
|
||||
|
||||
// break up the start and end dates
|
||||
$startDateObj = DateTime::createFromFormat("l, F j, Y - h:ia", $start_date);
|
||||
$event_start_date = $startDateObj->format($dateFormat);
|
||||
$event_start_time = $startDateObj->format('g:ia');
|
||||
|
||||
if (!empty($end_date)) {
|
||||
$endDateObj = DateTime::createFromFormat("l, F j, Y - h:ia", $end_date);
|
||||
$event_end_date = $endDateObj->format($dateFormat . ', Y');
|
||||
$event_end_time = $endDateObj->format('g:ia');
|
||||
|
||||
// default to display both the start and end dates
|
||||
$variables['event_date'] = $event_start_date . ' - ' . $event_end_date;
|
||||
// if the start date and end date are equal, display only the end date
|
||||
if ($startDateObj->format($dateFormat . ', Y') == $event_end_date) {
|
||||
$variables['event_date'] = $event_end_date;
|
||||
}
|
||||
// append the start and end times.
|
||||
$variables['event_time'] = $event_start_time . ' - ' . $event_end_time;
|
||||
// If the start and end time are the same, display "All Day" instead.
|
||||
if ($event_start_time == $event_end_time && $event_start_date == $event_end_date) {
|
||||
$variables['event_time'] = t('All Day');
|
||||
}
|
||||
} else {
|
||||
// If no end date, show only the start date and the text "All Day".
|
||||
$variables['event_date'] = $event_start_date;
|
||||
$variables['event_time'] = t('All Day');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements_hook_preprocess_html().
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_html(&$vars) {
|
||||
if (!empty($vars['head_title']['title'])) {
|
||||
$vars['page']['#share_title'] = $vars['head_title']['title'];
|
||||
} elseif (!empty($vars['head_title']['name'])) {
|
||||
$vars['page']['#share_title'] = $vars['head_title']['name'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?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'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_views_view().
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_views_view(&$vars) {
|
||||
// Add the view title to selected views.
|
||||
if (in_array($vars['id'], [
|
||||
'page_teaser_topic',
|
||||
'media_resources_tar',
|
||||
'events_by_series',
|
||||
'events_teaser_small',
|
||||
'news_teaser_small',
|
||||
'news_related_card',
|
||||
])) {
|
||||
$vars['title'] = [
|
||||
'#markup' => $vars['view']->getTitle(),
|
||||
];
|
||||
}
|
||||
|
||||
// Add classes to the more link to selected views.
|
||||
if (in_array($vars['id'], [
|
||||
'events_teaser_small',
|
||||
'news_teaser_small',
|
||||
'media_resources_tar',
|
||||
'page_teaser_topic',
|
||||
]) && !empty($vars['more'])) {
|
||||
$vars['more']['#options']['attributes']['class'][] = 'arrow-link';
|
||||
$vars['more']['#options']['attributes']['class'][] = 'view__more-link';
|
||||
}
|
||||
elseif (in_array($vars['id'], [
|
||||
'publication_teaser',
|
||||
'news_teaser',
|
||||
'news_related_card',
|
||||
'page_teaser',
|
||||
]) && !empty($vars['more'])) {
|
||||
$vars['more']['#options']['attributes']['class'][] = 'button';
|
||||
$vars['more']['#options']['attributes']['class'][] = 'button--secondary';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_views_view__HOOK().
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_views_view__events_by_series(&$vars) {
|
||||
if (!empty($vars['attachment_before']) &&
|
||||
!empty($vars['attachment_before'][0])
|
||||
) {
|
||||
$attachment_before = $vars['attachment_before'][0]['#view'];
|
||||
$vars['attachment_before_rows'] = $attachment_before->total_rows;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
function cdesai_emulsify_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $formState) {
|
||||
// Group start and end date within a fieldset.
|
||||
if (!empty($form['#id']) &&
|
||||
in_array($form['#id'], ['views-exposed-form-news-teaser-list-search-news']) &&
|
||||
!empty($form['published_date'])
|
||||
) {
|
||||
$published_date_range = [
|
||||
'#type' => 'fieldset',
|
||||
'#name' => 'published_date_range',
|
||||
'#title' => t('Published Date'),
|
||||
'published_date' => $form['published_date'],
|
||||
];
|
||||
// Since the order of the displayed exposed form options more or less
|
||||
// follows the array order, insert the new fieldset next to the start date.
|
||||
$published_date_index = array_search('published_date', array_keys($form));
|
||||
$form = array_merge(
|
||||
array_slice($form, 0, $published_date_index),
|
||||
['published_date_range' => $published_date_range],
|
||||
array_slice($form, $published_date_index)
|
||||
);
|
||||
unset($form['published_date']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_views_exposed_form
|
||||
*/
|
||||
function cdesai_emulsify_preprocess_views_exposed_form(&$vars) {
|
||||
$form = &$vars['form'];
|
||||
if (!empty($form['published_date_range'])) {
|
||||
// Views doesn't add labels to exposed form elements until the preprocess
|
||||
// function, so we do the same thing here for elements moved into a fieldset.
|
||||
// @see template_preprocess_views_exposed_form
|
||||
$form['published_date_range']['published_date']['min']['#title'] = t('Start');
|
||||
$form['published_date_range']['published_date']['max']['#title'] = t('End');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user