annotate core/profiles/demo_umami/themes/umami/umami.theme @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /**
Chris@14 4 * @file
Chris@14 5 * Functions to support theming in the Umami theme.
Chris@14 6 */
Chris@14 7
Chris@14 8 use Drupal\Component\Utility\Html;
Chris@14 9 use Drupal\Core\Form\FormStateInterface;
Chris@18 10 use Drupal\search\SearchPageInterface;
Chris@14 11
Chris@14 12 /**
Chris@14 13 * Implements hook_preprocess_HOOK() for HTML document templates.
Chris@14 14 *
Chris@14 15 * Adds body classes if certain regions have content.
Chris@14 16 */
Chris@14 17 function umami_preprocess_html(&$variables) {
Chris@14 18 // Add a sidebar class if the sidebar has content in it.
Chris@14 19 if (!empty($variables['page']['sidebar'])) {
Chris@14 20 $variables['attributes']['class'][] = 'two-columns';
Chris@14 21 $variables['#attached']['library'][] = 'umami/two-columns';
Chris@14 22 }
Chris@14 23 else {
Chris@14 24 $variables['attributes']['class'][] = 'one-column';
Chris@14 25 }
Chris@14 26 }
Chris@14 27
Chris@14 28 /**
Chris@14 29 * Implements hook_preprocess_field().
Chris@14 30 */
Chris@14 31 function umami_preprocess_field(&$variables, $hook) {
Chris@14 32 $element = $variables['element'];
Chris@14 33 // Add class to label and items fields to be styled using the meta styles.
Chris@14 34 if (isset($element['#field_name'])) {
Chris@14 35 if (
Chris@14 36 $element['#field_name'] == 'field_recipe_category' ||
Chris@14 37 $element['#field_name'] == 'field_tags' ||
Chris@14 38 $element['#field_name'] == 'field_difficulty') {
Chris@14 39 $variables['attributes']['class'] = 'label-items';
Chris@14 40 }
Chris@14 41 }
Chris@14 42 }
Chris@14 43
Chris@14 44 /**
Chris@14 45 * Implements hook_preprocess_block().
Chris@14 46 */
Chris@14 47 function umami_preprocess_block(&$variables) {
Chris@14 48 $variables['title_attributes']['class'][] = 'block__title';
Chris@14 49 // Add a class indicating the custom block bundle.
Chris@14 50 if (isset($variables['elements']['content']['#block_content'])) {
Chris@14 51 $variables['attributes']['class'][] = Html::getClass('block-type-' . $variables['elements']['content']['#block_content']->bundle());
Chris@14 52 }
Chris@14 53 }
Chris@14 54
Chris@14 55 /**
Chris@14 56 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
Chris@14 57 */
Chris@14 58 function umami_theme_suggestions_block_alter(array &$suggestions, array $variables) {
Chris@14 59 // Block suggestions for custom block bundles.
Chris@14 60 if (isset($variables['elements']['content']['#block_content'])) {
Chris@14 61 array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
Chris@14 62 }
Chris@14 63 }
Chris@14 64
Chris@14 65 /**
Chris@14 66 * Implements hook_preprocess_breadcrumb().
Chris@14 67 */
Chris@14 68 function umami_preprocess_breadcrumb(&$variables) {
Chris@14 69 // We are creating a variable for the Current Page Title, to allow us to print
Chris@14 70 // it after the breadcrumbs loop has run.
Chris@18 71 $route_match = \Drupal::routeMatch();
Chris@18 72 // Search page titles aren't resolved using the title_resolver service - it
Chris@18 73 // will always return 'Search' instead of 'Search for [term]', which would
Chris@18 74 // give us a breadcrumb of Home >> Search >> Search.
Chris@18 75 // @todo Revisit after https://www.drupal.org/project/drupal/issues/2359901
Chris@18 76 // @todo Revisit after https://www.drupal.org/project/drupal/issues/2403359
Chris@18 77 $entity = $route_match->getParameter('entity');
Chris@18 78 if ($entity instanceof SearchPageInterface) {
Chris@18 79 $variables['current_page_title'] = $entity->getPlugin()->suggestedTitle();
Chris@18 80 }
Chris@18 81 else {
Chris@18 82 $variables['current_page_title'] = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route_match->getRouteObject());
Chris@14 83 }
Chris@14 84 // Since we are printing the 'Current Page Title', add the URL cache context.
Chris@14 85 // If we don't, then we might end up with something like
Chris@14 86 // "Home > Articles" on the Recipes page, which should read "Home > Recipes".
Chris@14 87 $variables['#cache']['contexts'][] = 'url';
Chris@14 88 }
Chris@14 89
Chris@14 90 /**
Chris@14 91 * Implements hook_form_FORM_ID_alter().
Chris@14 92 */
Chris@14 93 function umami_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
Chris@14 94 $form['keys']['#attributes']['placeholder'] = t('Search by keyword, ingredient, dish');
Chris@14 95 }