administrationPage(); _uw_fdsu_theme_resp_add_favicons($variables); // Add Javascript only on the user login page. $route_name = \Drupal::routeMatch()->getRouteName(); if ($route_name == 'user.login') { $variables['#attached']['library'][] = 'uw_fdsu_theme_resp/user.login'; } } /** * 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[] = [ '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']; // Variables that we want to have access to regardless of region. $variables['branding_level'] = theme_get_setting('wcms_branding_level', 'uw_fdsu_theme_resp') ? theme_get_setting('wcms_branding_level', 'uw_fdsu_theme_resp') : 'full'; // 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") { // Get the home page link. $variables['home_link'] = Url::fromRoute('')->setAbsolute()->toString(); // Set the main menu variable. $variables['main_menu'] = \Drupal::service('uw_cfg_common.uw_service')->uwGetMenu('main', TRUE, TRUE); // Set the secondary menu variable. $variables['secondary_menu'] = \Drupal::service('uw_cfg_common.uw_service')->uwGetMenu('uw-menu-audience-menu', TRUE, TRUE); // The class that is used for the header. $variables['classes'][] = 'uw-header'; // Get the site name for placing inside the menu. $config = \Drupal::config('system.site'); $variables['site_name'] = $config->get('name'); $variables['faculty'] = theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') ? theme_get_setting('wcms_colour_scheme', 'uw_fdsu_theme_resp') : 'org-default'; // Global message content. $global_message = file_get_contents('https://uwaterloo.ca/global-message.html'); // If there is something to display, make sure to include css file, and // pass message as render array, then use render twig filter to avoid using // raw twig filter. Trimming message before sending to template, this // will remove any empty lines/spaces that message may have. if (!empty($global_message)) { $variables['global_message'] = ['#markup' => trim($global_message)]; } } // ISTWCMS-4847. // If we are on the main content section, the check if not a node and add // appropriate classes to display proper width. if ($region == 'content') { // Get the route name. $route_match = \Drupal::routeMatch(); // If we are on not on a node, add appropriate classes to show // proper width. $route_name = $route_match->getRouteName(); $exemptions = [ 'entity.node.canonical', 'entity.node.latest_version', 'layout_builder.overrides.node.view', ]; if (!in_array($route_name, $exemptions)) { $variables['classes'][] = 'layout'; $variables['classes'][] = 'uw-contained-width'; } } } /** * Implements hook_FORM_ID_alter(). * * Add settings for colour scheme. */ function uw_fdsu_theme_resp_form_system_theme_settings_alter(&$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'] = [ '#type' => 'details', '#open' => TRUE, '#title' => t('Colour scheme'), ]; // Colour scheme select list. $form['colour_scheme']['wcms_colour_scheme'] = [ '#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' => t('Yellow/red (Default)'), 'org-art' => t('Orange (Arts)'), 'org-eng' => t('Purple (Engineering)'), 'org-env' => t('Green (Environment)'), 'org-ahs' => t('Teal (Health)'), 'org-mat' => t('Pink (Mathematics)'), 'org-sci' => t('Blue (Science)'), 'org-school' => t('Red (School)'), ], ]; // Fieldset for branding options. $form['branding_options'] = [ '#type' => 'details', '#open' => TRUE, '#title' => t('Branding options'), ]; // Branding select option. $form['branding_options']['wcms_branding_level'] = [ '#type' => 'select', '#options' => [ 'full' => t('Full University branding'), 'generic' => t('Generic with University wordmark'), 'generic_barebones' => t('Fully generic'), ], '#default_value' => theme_get_setting('wcms_branding_level', 'uw_fdsu_theme_resp') ?: 'full', ]; } /** * Add favicons to the page when called from hook_preprocess_html(). * * @param array $variables * The variables array. */ function _uw_fdsu_theme_resp_add_favicons(array &$variables) { // Remove Drupal's favicon. foreach ($variables['page']['#attached']['html_head_link'] as $id => $html_head_link) { if (isset($html_head_link[0]['rel']) && $html_head_link[0]['rel'] == 'shortcut icon') { unset($variables['page']['#attached']['html_head_link'][$id]); break; } } // Specify new favicon locations. // Based on https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs. $favicon_base_path = base_path() . drupal_get_path('theme', 'uw_fdsu_theme_resp'); $favicon = [ 'rel' => 'icon', 'href' => $favicon_base_path . '/favicon.ico', ]; $variables['page']['#attached']['html_head_link'][] = [$favicon]; $favicon_svg = [ 'rel' => 'icon', 'href' => $favicon_base_path . '/icon.svg', 'type' => 'image/svg+xml', ]; $variables['page']['#attached']['html_head_link'][] = [$favicon_svg]; $favicon_apple = [ 'rel' => 'apple-touch-icon', 'href' => $favicon_base_path . '/apple-touch-icon.png', ]; $variables['page']['#attached']['html_head_link'][] = [$favicon_apple]; // Manifest needs a full URL. $favicon_base_path = \Drupal::request()->getSchemeAndHttpHost() . $favicon_base_path; $manifest = [ 'icons' => [ [ 'src' => $favicon_base_path . '/icon-192.png', 'type' => 'image/png', 'sizes' => '192x192', ], [ 'src' => $favicon_base_path . '/icon-512.png', 'type' => 'image/png', 'sizes' => '512x512', ], ], ]; $manifest = urlencode(json_encode($manifest)); $favicon_manifest = [ 'rel' => 'manifest', 'href' => 'data:application/manifest+json,' . $manifest, ]; $variables['page']['#attached']['html_head_link'][] = [$favicon_manifest]; } /** * 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 \Drupal\image\Plugin\Field\FieldType\ImageItem $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, 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 \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $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(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(), ]; } } } /** * Implements hook_preprocess_node(). * * Set variables for node and teaser. */ function uw_fdsu_theme_resp_preprocess_node(&$variables) { // The UW service object. $uwService = \Drupal::service('uw_cfg_common.uw_service'); $nodeContent = \Drupal::service('uw_cfg_common.uw_node_content'); // The types of nodes the need preprocessing. $nodes_to_preprocess = $uwService->uwGetNodePreprocessing('full'); // Teaser to be preprocessed. $teasers_to_preprocess = $uwService->uwGetNodePreprocessing('teaser'); // If there is a node that needs preprocessing, // set the appropriate variables. if (in_array($variables['node']->getType(), $nodes_to_preprocess) || in_array($variables['node']->getType(), $teasers_to_preprocess)) { // If on a teaser page get the variables for teaser. if ($variables['view_mode'] == 'teaser' && in_array($variables['node']->getType(), $teasers_to_preprocess)) { $variables['teaser'] = $nodeContent->getNodeContent($variables['node'], 'teaser', 'all'); } // If on a node page get the variables for now. if ($variables['view_mode'] == 'full' && in_array($variables['node']->getType(), $nodes_to_preprocess )) { $variables['node_data'] = $nodeContent->getNodeContent($variables['node'], 'full', 'all'); $variables['node_data']['content'] = $variables['content']; } // Unset the content variable, so that we do not get // a second print of all the content. unset($variables['content']); } } /** * Implements hook_preprocess_block(). * * Add the admin_label and css classes if we are in layout builder. */ function uw_fdsu_theme_resp_preprocess_block(&$variables) { // Look at page title block to see if we have a featured image. // If we do then, set variable to not show page title. if ($variables['plugin_id'] == 'page_title_block') { // Set the featured image variable to no, we will only // change if there is a featured image. $variables['featured_image'] = 'no'; // Load the node. $node = \Drupal::routeMatch()->getParameter('node'); // ISTWCMS-4943: ensure that we get a node object. // If node is not object by this point, probably on // a revision page where node is an integer, so load // the node. if ($node && !is_object($node)) { $node = Node::load($node); } // If there is a node, check that it has a featured image. if ($node) { // The UW service object. $uwService = \Drupal::service('uw_cfg_common.uw_service'); $variables['featured_image'] = $uwService->uwCheckNodeForFeaturedImage($node); } } // If we are in layout builder (this is set much earlier in // the page load process), then continue to look if we need // to add the admin_label and css classes. if (isset($variables['in_layout_builder']) && $variables['in_layout_builder'] == TRUE) { // Get the block manager object. $blockManager = \Drupal::service('plugin.manager.block'); // Get the plugin definitions for the block, using the // plugin_id of the block. $plugin_definitions = $blockManager->getDefinition($variables['plugin_id']); // The admin labels to exclude, these are ones that we do not // want the admin label to appear on the layout builder page. $admin_labels_to_exclude = [ 'Messages', 'Tabs', 'Page title', 'Global header', 'Main navigation', 'Site Footer block', ]; // Check if the admin_label is an object, we need to do this // because some admin labels are translatable and are therefore // translatable markup objects (usually through code way of // building blocks. Others are not, if translations are not turned // on for this block (usually through the GUI way of creating a block, // where the translation option is not set). if (is_object($plugin_definitions['admin_label'])) { // The admin label is a translatable markup, so just render // the object to set the admin label variable.. $admin_label = $plugin_definitions['admin_label']->render(); } else { // The admin label is not an object so just set the // admin label variable. $admin_label = $plugin_definitions['admin_label']; } // If the admin label is not in the ones to exclude add it // to the template variables and set the class to be used // to display the admin label. if (!in_array($admin_label, $admin_labels_to_exclude)) { $variables['admin_label'] = $admin_label; $variables['attributes']['class'][] = 'in-layout-builder'; } } } /** * Implements template_preprocess_container(). */ function uw_fdsu_theme_resp_preprocess_container(&$variables) { // Ensure that we are on a layout page. if (\Drupal::routeMatch()->getRouteName() == 'layout_builder.overrides.node.view') { // Ensure that we are on the first layout builder container. if ( isset($variables['attributes']['class']) && $variables['attributes']['class'][0] == 'layout-builder' ) { // The UW service object. $uwService = \Drupal::service('uw_cfg_common.uw_service'); // The UW node content service. $uwNodeContent = \Drupal::service('uw_cfg_common.uw_node_content'); // The list of content types that will have header // and footer in layout builder pages. $content_types = $uwService->uwGetNodePreprocessing('layout_container'); // Get the node object. $node = \Drupal::routeMatch()->getParameter('node'); // If there is a node object, and it is a content // type that has header and footer for layout pages, // get the header and footer. if ($node && in_array($node->getType(), $content_types)) { // Get the node object. $node = \Drupal::routeMatch()->getParameter('node'); // Set variables for featured image. $variables['node_type'] = str_replace('_', '-', $node->getType()); $variables['featured_image'] = $uwService->uwCheckNodeForFeaturedImage($node); // If there is a node object, get the header and footer data. if ($node) { $variables['header_data'] = $uwNodeContent->getNodeContent($node, 'teaser', 'header'); $variables['footer_data'] = $uwNodeContent->getNodeContent($node, 'teaser', 'footer'); } } } } }