mediteran.theme 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the Mediteran theme.
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Url;
  8. /**
  9. * Implements hook_preprocess_HOOK() for HTML document templates.
  10. */
  11. function mediteran_preprocess_html(&$variables)
  12. {
  13. // If on a node add or edit page, add a node-layout class.
  14. $path_args = explode('/', \Drupal::request()->getPathInfo());
  15. if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
  16. foreach ($suggestions as $suggestion) {
  17. if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
  18. $variables['attributes']['class'][] = 'node-form-layout';
  19. }
  20. }
  21. }
  22. }
  23. /**
  24. * Implements hook_preprocess_HOOK() for menu-local-tasks templates.
  25. *
  26. * Use preprocess hook to set #attached to child elements
  27. * because they will be processed by Twig and drupal_render will
  28. * be invoked.
  29. */
  30. function mediteran_preprocess_menu_local_tasks(&$variables)
  31. {
  32. if (!empty($variables['primary'])) {
  33. $variables['primary']['#attached'] = [
  34. 'library' => [
  35. 'mediteran/drupal.nav-tabs',
  36. ],
  37. ];
  38. } elseif (!empty($variables['secondary'])) {
  39. $variables['secondary']['#attached'] = [
  40. 'library' => [
  41. 'mediteran/drupal.nav-tabs',
  42. ],
  43. ];
  44. }
  45. }
  46. /**
  47. * Implements hook_preprocess_HOOK() for menu-local-task templates.
  48. */
  49. function mediteran_preprocess_menu_local_task(&$variables)
  50. {
  51. $variables['attributes']['class'][] = 'tabs__tab';
  52. }
  53. /**
  54. * Implements hook_preprocess_HOOK() for list of available node type templates.
  55. */
  56. function mediteran_preprocess_node_add_list(&$variables)
  57. {
  58. if (!empty($variables['content'])) {
  59. /** @var \Drupal\node\NodeTypeInterface $type */
  60. foreach ($variables['content'] as $type) {
  61. $variables['types'][$type->id()]['label'] = $type->label();
  62. $variables['types'][$type->id()]['url'] = Url::fromRoute('node.add', ['node_type' => $type->id()]);
  63. }
  64. }
  65. }
  66. /**
  67. * Implements hook_preprocess_HOOK() for block content add list templates.
  68. *
  69. * Displays the list of available custom block types for creation, adding
  70. * separate variables for the label and url.
  71. */
  72. function mediteran_preprocess_block_content_add_list(&$variables)
  73. {
  74. if (!empty($variables['content'])) {
  75. foreach ($variables['content'] as $type) {
  76. $variables['types'][$type->id()]['label'] = $type->label();
  77. $options = ['query' => \Drupal::request()->query->all()];
  78. $variables['types'][$type->id()]['url'] = Url::fromRoute('block_content.add_form', ['block_content_type' => $type->id()], $options);
  79. }
  80. }
  81. }
  82. /**
  83. * Implements hook_preprocess_block() for block content.
  84. *
  85. * Disables contextual links for all blocks.
  86. */
  87. function mediteran_preprocess_block(&$variables)
  88. {
  89. if (isset($variables['title_suffix']['contextual_links'])) {
  90. unset($variables['title_suffix']['contextual_links']);
  91. unset($variables['elements']['#contextual_links']);
  92. $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
  93. }
  94. }
  95. /**
  96. * Implements hook_preprocess_HOOK() for block admin page templates.
  97. */
  98. function mediteran_preprocess_admin_block_content(&$variables)
  99. {
  100. if (!empty($variables['content'])) {
  101. foreach ($variables['content'] as $key => $item) {
  102. $variables['content'][$key]['url'] = $item['url']->toString();
  103. }
  104. }
  105. }
  106. /**
  107. * Implements hook_preprocess_HOOK() for menu-local-action templates.
  108. */
  109. function mediteran_preprocess_menu_local_action(array &$variables)
  110. {
  111. $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  112. $variables['link']['#options']['attributes']['class'][] = 'button--small';
  113. // We require Modernizr's touch test for button styling.
  114. $variables['#attached']['library'][] = 'core/modernizr';
  115. }
  116. /**
  117. * Implements hook_element_info_alter().
  118. */
  119. function mediteran_element_info_alter(&$type)
  120. {
  121. // We require Modernizr for button styling.
  122. if (isset($type['button'])) {
  123. $type['button']['#attached']['library'][] = 'core/modernizr';
  124. }
  125. }
  126. /**
  127. * Implements hook_preprocess_install_page().
  128. */
  129. function mediteran_preprocess_install_page(&$variables)
  130. {
  131. // Mediteran has custom styling for the install page.
  132. $variables['#attached']['library'][] = 'mediteran/install-page';
  133. }
  134. /**
  135. * Implements hook_preprocess_maintenance_page().
  136. */
  137. function mediteran_preprocess_maintenance_page(&$variables)
  138. {
  139. // Mediteran has custom styling for the maintenance page.
  140. $variables['#attached']['library'][] = 'mediteran/maintenance-page';
  141. }
  142. /**
  143. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
  144. *
  145. * Changes vertical tabs to container.
  146. */
  147. function mediteran_form_node_form_alter(&$form, FormStateInterface $form_state)
  148. {
  149. $form['#theme'] = ['node_edit_form'];
  150. $form['#attached']['library'][] = 'mediteran/node-form';
  151. $form['advanced']['#type'] = 'container';
  152. $form['meta']['#type'] = 'container';
  153. $form['meta']['#access'] = TRUE;
  154. $form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
  155. $form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
  156. $form['revision_information']['#type'] = 'container';
  157. $form['revision_information']['#group'] = 'meta';
  158. }
  159. /**
  160. * Implements template_preprocess_paragraph().
  161. */
  162. function mediteran_preprocess_paragraph(&$variables)
  163. {
  164. $variables['#attached']['library'][] = 'mediteran/paragraphs';
  165. }
  166. /**
  167. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\media\MediaForm.
  168. */
  169. function mediteran_form_media_form_alter(&$form, FormStateInterface $form_state)
  170. {
  171. // @todo Revisit after https://www.drupal.org/node/2892304 is in. It
  172. // introduces a footer region to these forms which will allow for us to
  173. // display a top border over the published checkbox by defining a
  174. // media-edit-form.html.twig template the same way node does.
  175. $form['#attached']['library'][] = 'mediteran/media-form';
  176. }