diff --git a/prototype.theme b/prototype.theme index 5b237e1e860bfb2d10b72dff49cbc625eaffeca0..30324464228052c0428fd16a93c5b12fcb35bad8 100755 --- a/prototype.theme +++ b/prototype.theme @@ -7,53 +7,53 @@ use Drupal\block_content\BlockContentInterface; use Drupal\Component\Utility\Html; +use Drupal\views\ViewExecutable; /** * Implements hook_preprocess_page() for prototype theme. * - * @param array &$variables - * An associative array containing the variables for the page template. - * - * NOTE: If you want to use this outside the footer, you need to - * rename this function to prototype_preprocess_page and adjust the - * region--footer.html.twig file and add the backToTop variable to the - * page.html.twig file. + * This preprocess adds the current page title to the end of a breadcrumb list. */ -function prototype_preprocess_region__footer(array &$variables) { +function prototype_preprocess_breadcrumb(&$variables) { + if ($variables['breadcrumb']) { + $request = \Drupal::request(); + $route_match = \Drupal::routeMatch(); + $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); - if (theme_get_setting('display_back_to_top')) { - $variables['backToTop'] = [ - '#type' => 'component', - '#component' => 'prototype:back-to-top', - '#props' => [ - 'text' => t('Back to Top'), - ], - ]; + if (!empty($page_title)) { + $breadcrumb_link = [ + 'text' => $page_title, + 'url' => '', + 'attributes' => ['class' => 'active'], + 'is_current' => TRUE, + ]; + + // Add cache metadata. + $breadcrumb_link['#cache'] = [ + 'contexts' => ['url.path'], + 'tags' => ['config:system.site'], + ]; + + $variables['breadcrumb'][] = $breadcrumb_link; + } } } /** - * Implements hook_theme_suggestions_HOOK_alter(). + * Implements hook_preprocess_HOOK(). */ -function prototype_theme_suggestions_block_alter(array &$suggestions, array $variables) { - $content = $variables['elements']['content']; - - if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) { - $suggestions[] = 'block__block_content__' . $content['#block_content']->bundle(); - } +function prototype_preprocess_checkboxes(&$variables) { + $variables['attributes']['class'][] = 'form-boolean-group'; } /** - * Implements theme_theme_suggestions_input_alter(). + * Implements hook_preprocess_HOOK(). */ -function prototype_theme_suggestions_input_alter(&$suggestions, array $variables) { - $element = $variables['element']; +function prototype_preprocess_field(&$variables) { + $rich_field_types = ['text_with_summary', 'text', 'text_long']; - // Add Twig template suggestion based on data attributes. To use this, in a - // `hook_form_alter()` add an element to the `#attributes` array for - // `data-twig-suggestion`. `button` elements have a template already made. - if (isset($element['#attributes']['data-twig-suggestion'])) { - $suggestions[] = "input__{$element['#type']}__{$element['#attributes']['data-twig-suggestion']}"; + if (in_array($variables['field_type'], $rich_field_types, TRUE)) { + $variables['attributes']['class'][] = 'text-content'; } } @@ -128,50 +128,6 @@ function prototype_preprocess_input(&$variables) { } } -/** - * Implements hook_preprocess_HOOK(). - */ -function prototype_preprocess_textarea(&$variables) { - $variables['attributes']['class'][] = 'form-element'; - $variables['attributes']['class'][] = 'form-element--type-textarea'; - $variables['attributes']['class'][] = 'form-element--api-textarea'; -} - -/** - * Implements hook_preprocess_HOOK(). - */ -function prototype_preprocess_select(&$variables) { - $variables['attributes']['class'][] = 'form-element'; - $variables['attributes']['class'][] = $variables['element']['#multiple'] ? - 'form-element--type-select-multiple' : - 'form-element--type-select'; -} - -/** - * Implements hook_preprocess_HOOK(). - */ -function prototype_preprocess_checkboxes(&$variables) { - $variables['attributes']['class'][] = 'form-boolean-group'; -} - -/** - * Implements hook_preprocess_HOOK(). - */ -function prototype_preprocess_radios(&$variables) { - $variables['attributes']['class'][] = 'form-boolean-group'; -} - -/** - * Implements hook_preprocess_HOOK(). - */ -function prototype_preprocess_field(&$variables) { - $rich_field_types = ['text_with_summary', 'text', 'text_long']; - - if (in_array($variables['field_type'], $rich_field_types, TRUE)) { - $variables['attributes']['class'][] = 'text-content'; - } -} - /** * Implements hook_preprocess_menu(). */ @@ -186,28 +142,6 @@ function prototype_preprocess_menu(&$variables) { prototype_prepare_menu_items($variables['items']); } -/** - * Implements hook_theme_suggestions_HOOK_alter(). - */ -function prototype_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables) { - $view_mode = $variables['elements']['#view_mode']; - $suggestions[] = 'taxonomy_term__' . $view_mode; -} - -/** - * Implements hook_theme_suggestions_HOOK_alter(). - * - * @todo remove after these are resolved: - * https://www.drupal.org/project/drupal/issues/2270883 - * https://www.drupal.org/project/drupal/issues/2766379 - */ -function prototype_theme_suggestions_user_alter(array &$suggestions, array $variables) { - $view_mode = $variables['elements']['#view_mode'] ?? ''; - if ($view_mode) { - $suggestions[] = 'user__' . $view_mode; - } -} - /** * Helper function to standarize gaps in the menu markup. * @@ -254,35 +188,117 @@ function prototype_prepare_menu_items(&$items) { } } +/** + * Implements hook_preprocess_HOOK(). + */ +function prototype_preprocess_radios(&$variables) { + $variables['attributes']['class'][] = 'form-boolean-group'; +} + /** * Implements hook_preprocess_page() for prototype theme. * - * This preprocess adds the current page title to the end of a breadcrumb list. - * * @param array &$variables * An associative array containing the variables for the page template. + * + * NOTE: If you want to use this outside the footer, you need to + * rename this function to prototype_preprocess_page and adjust the + * region--footer.html.twig file and add the backToTop variable to the + * page.html.twig file. */ -function prototype_preprocess_breadcrumb(&$variables) { - if ($variables['breadcrumb']) { - $request = \Drupal::request(); - $route_match = \Drupal::routeMatch(); - $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); +function prototype_preprocess_region__footer(array &$variables) { - if (!empty($page_title)) { - $breadcrumb_link = [ - 'text' => $page_title, - 'url' => '', - 'attributes' => ['class' => 'active'], - 'is_current' => TRUE, - ]; + if (theme_get_setting('display_back_to_top')) { + $variables['backToTop'] = [ + '#type' => 'component', + '#component' => 'prototype:back-to-top', + '#props' => [ + 'text' => t('Back to Top'), + ], + ]; + } +} - // Add cache metadata. - $breadcrumb_link['#cache'] = [ - 'contexts' => ['url.path'], - 'tags' => ['config:system.site'], - ]; +/** + * Implements hook_preprocess_HOOK(). + */ +function prototype_preprocess_select(&$variables) { + $variables['attributes']['class'][] = 'form-element'; + $variables['attributes']['class'][] = $variables['element']['#multiple'] ? + 'form-element--type-select-multiple' : + 'form-element--type-select'; +} - $variables['breadcrumb'][] = $breadcrumb_link; - } +/** + * Implements hook_preprocess_HOOK(). + */ +function prototype_preprocess_textarea(&$variables) { + $variables['attributes']['class'][] = 'form-element'; + $variables['attributes']['class'][] = 'form-element--type-textarea'; + $variables['attributes']['class'][] = 'form-element--api-textarea'; +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function prototype_theme_suggestions_block_alter(array &$suggestions, array $variables) { + $content = $variables['elements']['content']; + + if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) { + $suggestions[] = 'block__block_content__' . $content['#block_content']->bundle(); + } +} + +/** + * Implements theme_theme_suggestions_input_alter(). + */ +function prototype_theme_suggestions_input_alter(&$suggestions, array $variables) { + $element = $variables['element']; + + // Add Twig template suggestion based on data attributes. To use this, in a + // `hook_form_alter()` add an element to the `#attributes` array for + // `data-twig-suggestion`. `button` elements have a template already made. + if (isset($element['#attributes']['data-twig-suggestion'])) { + $suggestions[] = "input__{$element['#type']}__{$element['#attributes']['data-twig-suggestion']}"; + } +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function prototype_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables) { + $view_mode = $variables['elements']['#view_mode']; + $suggestions[] = 'taxonomy_term__' . $view_mode; +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + * + * @todo remove after these are resolved: + * https://www.drupal.org/project/drupal/issues/2270883 + * https://www.drupal.org/project/drupal/issues/2766379 + */ +function prototype_theme_suggestions_user_alter(array &$suggestions, array $variables) { + $view_mode = $variables['elements']['#view_mode'] ?? ''; + if ($view_mode) { + $suggestions[] = 'user__' . $view_mode; + } +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function prototype_theme_suggestions_views_view_alter(array &$suggestions, array $variables) { + $view = $variables['view']; + + if ( + $view instanceof ViewExecutable + && $display = $view->current_display + ) { + $view_id = $view->id(); + $original = $variables['theme_hook_original']; + + $suggestions[] = "{$original}__$view_id"; + $suggestions[] = "{$original}__{$view_id}__$display"; } }