5 * Functions to support theming in the Umami theme.
8 use Drupal\Component\Utility\Html;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
13 * Implements hook_preprocess_HOOK() for HTML document templates.
15 * Adds body classes if certain regions have content.
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';
24 $variables['attributes']['class'][] = 'one-column';
29 * Implements hook_preprocess_field().
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'])) {
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';
45 * Implements hook_preprocess_block().
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());
56 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
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());
66 * Implements hook_preprocess_breadcrumb().
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();
82 $variables['current_page_title'] = \Drupal::service('title_resolver')->getTitle($request, $route);
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';
92 * Implements hook_form_FORM_ID_alter().
94 function umami_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
95 $form['keys']['#attributes']['placeholder'] = t('Search by keyword, ingredient, dish');