seven.theme 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the Seven theme.
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. /**
  8. * Implements hook_preprocess_HOOK() for HTML document templates.
  9. */
  10. function seven_preprocess_html(&$variables) {
  11. // If on a node add or edit page, add a node-layout class.
  12. $path_args = explode('/', \Drupal::request()->getPathInfo());
  13. if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
  14. foreach ($suggestions as $suggestion) {
  15. if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
  16. $variables['attributes']['class'][] = 'node-form-layout';
  17. }
  18. }
  19. }
  20. }
  21. /**
  22. * Implements hook_pre_render_HOOK() for menu-local-tasks templates.
  23. *
  24. * Use preprocess hook to set #attached to child elements
  25. * because they will be processed by Twig and drupal_render will
  26. * be invoked.
  27. */
  28. function seven_preprocess_menu_local_tasks(&$variables) {
  29. if (!empty($variables['primary'])) {
  30. $variables['primary']['#attached'] = [
  31. 'library' => [
  32. 'seven/drupal.nav-tabs',
  33. ],
  34. ];
  35. }
  36. elseif (!empty($variables['secondary'])) {
  37. $variables['secondary']['#attached'] = [
  38. 'library' => [
  39. 'seven/drupal.nav-tabs',
  40. ],
  41. ];
  42. }
  43. }
  44. /**
  45. * Implements hook_preprocess_HOOK() for menu-local-task templates.
  46. */
  47. function seven_preprocess_menu_local_task(&$variables) {
  48. $variables['attributes']['class'][] = 'tabs__tab';
  49. }
  50. /**
  51. * Implements hook_preprocess_HOOK() for list of available node type templates.
  52. */
  53. function seven_preprocess_node_add_list(&$variables) {
  54. if (!empty($variables['content'])) {
  55. /** @var \Drupal\node\NodeTypeInterface $type */
  56. foreach ($variables['content'] as $type) {
  57. $variables['types'][$type->id()]['label'] = $type->label();
  58. $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
  59. }
  60. }
  61. }
  62. /**
  63. * Implements hook_preprocess_HOOK() for block content add list templates.
  64. *
  65. * Displays the list of available custom block types for creation, adding
  66. * separate variables for the label and url.
  67. */
  68. function seven_preprocess_block_content_add_list(&$variables) {
  69. if (!empty($variables['content'])) {
  70. foreach ($variables['content'] as $type) {
  71. $variables['types'][$type->id()]['label'] = $type->label();
  72. $options = ['query' => \Drupal::request()->query->all()];
  73. $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', ['block_content_type' => $type->id()], $options);
  74. }
  75. }
  76. }
  77. /**
  78. * Implements hook_preprocess_block() for block content.
  79. *
  80. * Disables contextual links for all blocks.
  81. */
  82. function seven_preprocess_block(&$variables) {
  83. if (isset($variables['title_suffix']['contextual_links'])) {
  84. unset($variables['title_suffix']['contextual_links']);
  85. unset($variables['elements']['#contextual_links']);
  86. $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
  87. }
  88. }
  89. /**
  90. * Implements hook_preprocess_HOOK() for block admin page templates.
  91. */
  92. function seven_preprocess_admin_block_content(&$variables) {
  93. if (!empty($variables['content'])) {
  94. foreach ($variables['content'] as $key => $item) {
  95. $variables['content'][$key]['url'] = $item['url']->toString();
  96. }
  97. }
  98. }
  99. /**
  100. * Implements hook_preprocess_HOOK() for menu-local-action templates.
  101. */
  102. function seven_preprocess_menu_local_action(array &$variables) {
  103. $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  104. $variables['link']['#options']['attributes']['class'][] = 'button--small';
  105. // We require Modernizr's touch test for button styling.
  106. $variables['#attached']['library'][] = 'core/modernizr';
  107. }
  108. /**
  109. * Implements hook_element_info_alter().
  110. */
  111. function seven_element_info_alter(&$type) {
  112. // We require Modernizr for button styling.
  113. if (isset($type['button'])) {
  114. $type['button']['#attached']['library'][] = 'core/modernizr';
  115. }
  116. }
  117. /**
  118. * Implements hook_preprocess_install_page().
  119. */
  120. function seven_preprocess_install_page(&$variables) {
  121. // Seven has custom styling for the install page.
  122. $variables['#attached']['library'][] = 'seven/install-page';
  123. }
  124. /**
  125. * Implements hook_preprocess_maintenance_page().
  126. */
  127. function seven_preprocess_maintenance_page(&$variables) {
  128. // Seven has custom styling for the maintenance page.
  129. $variables['#attached']['library'][] = 'seven/maintenance-page';
  130. }
  131. /**
  132. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
  133. *
  134. * Changes vertical tabs to container and adds meta information.
  135. */
  136. function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
  137. /** @var \Drupal\node\NodeInterface $node */
  138. $node = $form_state->getFormObject()->getEntity();
  139. $form['#theme'] = ['node_edit_form'];
  140. $form['#attached']['library'][] = 'seven/node-form';
  141. $form['advanced']['#type'] = 'container';
  142. $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
  143. $form['meta'] = [
  144. '#attributes' => ['class' => ['entity-meta__header']],
  145. '#type' => 'container',
  146. '#group' => 'advanced',
  147. '#weight' => -100,
  148. 'published' => [
  149. '#type' => 'html_tag',
  150. '#tag' => 'h3',
  151. '#value' => $node->isPublished() ? t('Published') : t('Not published'),
  152. '#access' => !$node->isNew(),
  153. '#attributes' => [
  154. 'class' => ['entity-meta__title'],
  155. ],
  156. ],
  157. 'changed' => [
  158. '#type' => 'item',
  159. '#wrapper_attributes' => ['class' => ['entity-meta__last-saved', 'container-inline']],
  160. '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
  161. ],
  162. 'author' => [
  163. '#type' => 'item',
  164. '#wrapper_attributes' => ['class' => ['author', 'container-inline']],
  165. '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
  166. ],
  167. ];
  168. $form['revision_information']['#type'] = 'container';
  169. $form['revision_information']['#group'] = 'meta';
  170. }