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