bartik.theme 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the Bartik theme.
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Template\Attribute;
  8. /**
  9. * Implements hook_preprocess_HOOK() for HTML document templates.
  10. *
  11. * Adds body classes if certain regions have content.
  12. */
  13. function bartik_preprocess_html(&$variables) {
  14. // Add information about the number of sidebars.
  15. if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
  16. $variables['attributes']['class'][] = 'layout-two-sidebars';
  17. }
  18. elseif (!empty($variables['page']['sidebar_first'])) {
  19. $variables['attributes']['class'][] = 'layout-one-sidebar';
  20. $variables['attributes']['class'][] = 'layout-sidebar-first';
  21. }
  22. elseif (!empty($variables['page']['sidebar_second'])) {
  23. $variables['attributes']['class'][] = 'layout-one-sidebar';
  24. $variables['attributes']['class'][] = 'layout-sidebar-second';
  25. }
  26. else {
  27. $variables['attributes']['class'][] = 'layout-no-sidebars';
  28. }
  29. if (!empty($variables['page']['featured_top'])) {
  30. $variables['attributes']['class'][] = 'has-featured-top';
  31. }
  32. }
  33. /**
  34. * Implements hook_preprocess_HOOK() for page title templates.
  35. */
  36. function bartik_preprocess_page_title(&$variables) {
  37. // Since the title and the shortcut link are both block level elements,
  38. // positioning them next to each other is much simpler with a wrapper div.
  39. if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
  40. // Add a wrapper div using the title_prefix and title_suffix render
  41. // elements.
  42. $variables['title_prefix']['shortcut_wrapper'] = [
  43. '#markup' => '<div class="shortcut-wrapper clearfix">',
  44. '#weight' => 100,
  45. ];
  46. $variables['title_suffix']['shortcut_wrapper'] = [
  47. '#markup' => '</div>',
  48. '#weight' => -99,
  49. ];
  50. // Make sure the shortcut link is the first item in title_suffix.
  51. $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
  52. }
  53. }
  54. /**
  55. * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
  56. */
  57. function bartik_preprocess_maintenance_page(&$variables) {
  58. // By default, site_name is set to Drupal if no db connection is available
  59. // or during site installation. Setting site_name to an empty string makes
  60. // the site and update pages look cleaner.
  61. // @see template_preprocess_maintenance_page
  62. if (!$variables['db_is_active']) {
  63. $variables['site_name'] = '';
  64. }
  65. // Bartik has custom styling for the maintenance page.
  66. $variables['#attached']['library'][] = 'bartik/maintenance_page';
  67. }
  68. /**
  69. * Implements hook_preprocess_HOOK() for node.html.twig.
  70. */
  71. function bartik_preprocess_node(&$variables) {
  72. // Remove the "Add new comment" link on teasers or when the comment form is
  73. // displayed on the page.
  74. if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
  75. unset($variables['content']['links']['comment']['#links']['comment-add']);
  76. }
  77. }
  78. /**
  79. * Implements hook_preprocess_HOOK() for block.html.twig.
  80. */
  81. function bartik_preprocess_block(&$variables) {
  82. // Add a clearfix class to system branding blocks.
  83. if ($variables['plugin_id'] == 'system_branding_block') {
  84. $variables['attributes']['class'][] = 'clearfix';
  85. }
  86. }
  87. /**
  88. * Implements hook_preprocess_HOOK() for menu.html.twig.
  89. */
  90. function bartik_preprocess_menu(&$variables) {
  91. $variables['attributes']['class'][] = 'clearfix';
  92. }
  93. /**
  94. * Implements hook_theme_suggestions_HOOK_alter() for form templates.
  95. */
  96. function bartik_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  97. if ($variables['element']['#form_id'] == 'search_block_form') {
  98. $suggestions[] = 'form__search_block_form';
  99. }
  100. }
  101. /**
  102. * Implements hook_form_alter() to add classes to the search form.
  103. */
  104. function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  105. if (in_array($form_id, ['search_block_form', 'search_form'])) {
  106. $key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
  107. if (!isset($form[$key]['submit']['#attributes'])) {
  108. $form[$key]['submit']['#attributes'] = new Attribute();
  109. }
  110. $form[$key]['submit']['#attributes']->addClass('search-form__submit');
  111. }
  112. }