Something went wrong on our end
-
Eric Bremner authoredEric Bremner authored
uw_fdsu_theme_resp.theme 5.88 KiB
<?php
/**
* @file
* Theme file for uw_fdsu_theme_resp.
*/
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Datetime;
use Drupal\Core\DateTimeZone;
/**
* @file
* Functions to support theming.
*/
require_once dirname(__FILE__) . '/includes/block.inc';
require_once dirname(__FILE__) . '/includes/field.inc';
require_once dirname(__FILE__) . '/includes/form.inc';
require_once dirname(__FILE__) . '/includes/html.inc';
require_once dirname(__FILE__) . '/includes/navigation.inc';
require_once dirname(__FILE__) . '/includes/taxonomy.inc';
require_once dirname(__FILE__) . '/includes/views.inc';
/**
* Implements hook_preprocess_HOOK().
*
* Setting the faculty class colour.
*/
function uw_fdsu_theme_resp_preprocess_html(&$variables) {
// Adding the faculty colour class to the body.
$variables['attributes']['class'][] = theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') ? theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') : 'org-default';
}
/**
* Implements hook_preprocess_responsive_image().
*/
function uw_fdsu_theme_resp_preprocess_responsive_image(&$variables) {
// Step through each of the responsive image objects and get out source info.
foreach ($variables['sources'] as $source) {
// Set the a variable with the actual source info that we need.
$new_sources[] = array(
'srcset' => $source['srcset']->value(),
'media' => $source['media']->value(),
'type' => $source['type']->value(),
);
}
// Set the sources variable to the new sources.
$variables['sources'] = $new_sources;
}
/**
* Implements hook_preprocess_region().
*/
function uw_fdsu_theme_resp_preprocess_region(&$variables) {
// Get the region from variables.
$region = $variables['elements']['#region'];
// If we are on the header, add the correct header classes
// TO DO: Store the colour scheme used for the theme
// (i.e. faculty colour and get the correct class here).
if ($region == "header") {
// The class that is used for the header.
$variables['classes'][] = 'uw-header';
$variables['faculty'] = theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') ? theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') : 'org-default';
}
}
/**
* Implements hook_FORM_ID_alter.
*
* Add settings for colour scheme.
*/
function uw_fdsu_theme_resp_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
// Work-around for a core bug affecting admin themes. See issue #943212.
if (isset($form_id)) {
return;
}
// Fieldset for colour scheme.
$form['colour_scheme'] = array(
'#type' => 'details',
'#open' => TRUE,
'#title' => t('Colour scheme'),
);
// Colour scheme select list.
$form['colour_scheme']['wcms_colour_scheme'] = array(
'#type' => 'select',
'#default_value' => theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') ? theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') : 'default',
'#description' => t("Select a color scheme to use"),
'#options' => [
'org-default' => 'Default (yellow/red)',
'org-ahs' => 'Applied Health Sciences (teal)',
'org-art' => 'Arts (orange)',
'org-eng' => 'Engineering (purple)',
'org-env' => 'Environment (green)',
'org-mat' => 'Mathematics (pink)',
'org-sci' => 'Science (blue)',
'org-school' => 'School (red)',
],
);
}
/**
* Get the specified field value from a paragraph.
*
* @param object $paragraph
* The paragraph object.
* @param string $field
* The name of the field.
*
* @return mixed
* The field value.
*/
function _uw_fdsu_theme_resp_get_field_value_from_paragraph($paragraph, $field) {
// Get the field value.
$field_value = $paragraph->get($field)->first();
return $field_value->getValue()['value'];
}
/**
* Set the variables required for a responsive image.
*
* @param array $variables
* The variables array.
* @param object $field
* The field object.
* @param string $responsive_image_style
* The ID of the responsive image style.
*
* @return array|null
* An array with keys:
* - sources: An array of image sources.
* - alt: The alternative text.
*/
function _uw_fdsu_theme_resp_add_responsive_image_variables(array &$variables, Drupal\image\Plugin\Field\FieldType\ImageItem $field, string $responsive_image_style) : ?array {
// If there is a file present, set responsive image variables.
if ($file = $field->entity) {
// Set uri and image style id.
$variables['uri'] = $file->getFileUri();
$variables['responsive_image_style_id'] = $responsive_image_style;
// Call template function from responsive image core module.
// It sets variables for srcset, media, type and img_element
// for the responsive image style.
template_preprocess_responsive_image($variables);
// Step through each source and get string values.
$sources = [];
foreach ($variables['sources'] as $source) {
$sources[] = [
'srcset' => $source['srcset']->value(),
'media' => $source['media']->value(),
'type' => $source['type']->value(),
];
}
return [
'sources' => $sources,
'alt' => $field->get('alt')->getValue(),
];
}
return NULL;
}
/**
* Get image properties from an image field.
*
* @param object $field
* The image field.
*
* @return array|null
* An array of image properties or NULL if $field does not contain the
* required information.
*/
function _uw_fdsu_theme_resp_get_image_info(Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field) {
// If there is an image, process it.
if ($img_entity = $field->first()) {
// If we can load a file, grab the info about the file.
if ($file_entity = $img_entity->get('entity')->getTarget()) {
return [
'src' => file_create_url($file_entity->get('uri')->getString()),
'alt' => $img_entity->get('alt')->getString(),
];
}
}
}