seven.theme 5.8 KB

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