annotate core/profiles/demo_umami/themes/umami/umami.theme @ 5:12f9dff5fda9 tip

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