umami.theme 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Symfony\Cmf\Component\Routing\RouteObjectInterface;
  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 form 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. $request = \Drupal::request();
  65. if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
  66. // Search page titles aren't resolved using the title_resolver service - it
  67. // will always return 'Search' instead of 'Search for [term]', which would
  68. // give us a breadcrumb of Home >> Search >> Search.
  69. // @see https://www.drupal.org/project/drupal/issues/2359901
  70. // @see https://www.drupal.org/project/drupal/issues/2403359
  71. if (($entity = $request->attributes->get('entity')) && $entity->getEntityTypeId() === 'search_page') {
  72. $variables['current_page_title'] = $entity->getPlugin()->suggestedTitle();
  73. }
  74. else {
  75. $variables['current_page_title'] = \Drupal::service('title_resolver')->getTitle($request, $route);
  76. }
  77. }
  78. // Since we are printing the 'Current Page Title', add the URL cache context.
  79. // If we don't, then we might end up with something like
  80. // "Home > Articles" on the Recipes page, which should read "Home > Recipes".
  81. $variables['#cache']['contexts'][] = 'url';
  82. }
  83. /**
  84. * Implements hook_form_FORM_ID_alter().
  85. */
  86. function umami_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
  87. $form['keys']['#attributes']['placeholder'] = t('Search by keyword, ingredient, dish');
  88. }