5 * Functions to support theming in the Seven theme.
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\media\MediaForm;
12 * Implements hook_preprocess_HOOK() for HTML document templates.
14 function seven_preprocess_html(&$variables) {
15 // If on a node add or edit page, add a node-layout class.
16 $path_args = explode('/', \Drupal::request()->getPathInfo());
17 if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
18 foreach ($suggestions as $suggestion) {
19 if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
20 $variables['attributes']['class'][] = 'node-form-layout';
27 * Implements hook_preprocess_HOOK() for menu-local-tasks templates.
29 * Use preprocess hook to set #attached to child elements
30 * because they will be processed by Twig and drupal_render will
33 function seven_preprocess_menu_local_tasks(&$variables) {
34 if (!empty($variables['primary'])) {
35 $variables['primary']['#attached'] = [
37 'seven/drupal.nav-tabs',
41 elseif (!empty($variables['secondary'])) {
42 $variables['secondary']['#attached'] = [
44 'seven/drupal.nav-tabs',
51 * Implements hook_preprocess_HOOK() for menu-local-task templates.
53 function seven_preprocess_menu_local_task(&$variables) {
54 $variables['attributes']['class'][] = 'tabs__tab';
58 * Implements hook_preprocess_HOOK() for list of available node type templates.
60 function seven_preprocess_node_add_list(&$variables) {
61 if (!empty($variables['content'])) {
62 /** @var \Drupal\node\NodeTypeInterface $type */
63 foreach ($variables['content'] as $type) {
64 $variables['types'][$type->id()]['label'] = $type->label();
65 $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
71 * Implements hook_preprocess_HOOK() for block content add list templates.
73 * Displays the list of available custom block types for creation, adding
74 * separate variables for the label and url.
76 function seven_preprocess_block_content_add_list(&$variables) {
77 if (!empty($variables['content'])) {
78 foreach ($variables['content'] as $type) {
79 $variables['types'][$type->id()]['label'] = $type->label();
80 $options = ['query' => \Drupal::request()->query->all()];
81 $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', ['block_content_type' => $type->id()], $options);
87 * Implements hook_preprocess_block() for block content.
89 * Disables contextual links for all blocks.
91 function seven_preprocess_block(&$variables) {
92 if (isset($variables['title_suffix']['contextual_links'])) {
93 unset($variables['title_suffix']['contextual_links']);
94 unset($variables['elements']['#contextual_links']);
96 $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
101 * Implements hook_preprocess_HOOK() for block admin page templates.
103 function seven_preprocess_admin_block_content(&$variables) {
104 if (!empty($variables['content'])) {
105 foreach ($variables['content'] as $key => $item) {
106 $variables['content'][$key]['url'] = $item['url']->toString();
112 * Implements hook_preprocess_HOOK() for menu-local-action templates.
114 function seven_preprocess_menu_local_action(array &$variables) {
115 $variables['link']['#options']['attributes']['class'][] = 'button--primary';
116 $variables['link']['#options']['attributes']['class'][] = 'button--small';
118 // We require Modernizr's touch test for button styling.
119 $variables['#attached']['library'][] = 'core/modernizr';
123 * Implements hook_element_info_alter().
125 function seven_element_info_alter(&$type) {
126 // We require Modernizr for button styling.
127 if (isset($type['button'])) {
128 $type['button']['#attached']['library'][] = 'core/modernizr';
133 * Implements hook_preprocess_install_page().
135 function seven_preprocess_install_page(&$variables) {
136 // Seven has custom styling for the install page.
137 $variables['#attached']['library'][] = 'seven/install-page';
141 * Implements hook_preprocess_maintenance_page().
143 function seven_preprocess_maintenance_page(&$variables) {
144 // Seven has custom styling for the maintenance page.
145 $variables['#attached']['library'][] = 'seven/maintenance-page';
149 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
151 * Changes vertical tabs to container.
153 function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
154 $form['#theme'] = ['node_edit_form'];
155 $form['#attached']['library'][] = 'seven/node-form';
157 $form['advanced']['#type'] = 'container';
158 $form['meta']['#type'] = 'container';
159 $form['meta']['#access'] = TRUE;
160 $form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
161 $form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
163 $form['revision_information']['#type'] = 'container';
164 $form['revision_information']['#group'] = 'meta';
168 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\media\MediaForm.
170 function seven_form_media_form_alter(&$form, FormStateInterface $form_state) {
171 // Only attach CSS from core if this form comes from Media core, and not from
172 // the contrib Media Entity 1.x branch.
173 if (\Drupal::moduleHandler()->moduleExists('media') && $form_state->getFormObject() instanceof MediaForm) {
174 // @todo Revisit after https://www.drupal.org/node/2892304 is in. It
175 // introduces a footer region to these forms which will allow for us to
176 // display a top border over the published checkbox by defining a
177 // media-edit-form.html.twig template the same way node does.
178 $form['#attached']['library'][] = 'seven/media-form';