umami.theme 3.4 KB

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