annotate core/profiles/demo_umami/themes/umami/umami.theme @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 12f9dff5fda9
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@0 10 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
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@0 71 $request = \Drupal::request();
Chris@0 72 if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
Chris@0 73 // Search page titles aren't resolved using the title_resolver service - it
Chris@0 74 // will always return 'Search' instead of 'Search for [term]', which would
Chris@0 75 // give us a breadcrumb of Home >> Search >> Search.
Chris@0 76 // @see https://www.drupal.org/project/drupal/issues/2359901
Chris@0 77 // @see https://www.drupal.org/project/drupal/issues/2403359
Chris@0 78 if (($entity = $request->attributes->get('entity')) && $entity->getEntityTypeId() === 'search_page') {
Chris@0 79 $variables['current_page_title'] = $entity->getPlugin()->suggestedTitle();
Chris@0 80 }
Chris@0 81 else {
Chris@0 82 $variables['current_page_title'] = \Drupal::service('title_resolver')->getTitle($request, $route);
Chris@0 83 }
Chris@0 84 }
Chris@0 85 // Since we are printing the 'Current Page Title', add the URL cache context.
Chris@0 86 // If we don't, then we might end up with something like
Chris@0 87 // "Home > Articles" on the Recipes page, which should read "Home > Recipes".
Chris@0 88 $variables['#cache']['contexts'][] = 'url';
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Implements hook_form_FORM_ID_alter().
Chris@0 93 */
Chris@0 94 function umami_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
Chris@0 95 $form['keys']['#attributes']['placeholder'] = t('Search by keyword, ingredient, dish');
Chris@0 96 }