5 * Functions to support theming in the Seven theme.
8 use Drupal\Core\Form\FormStateInterface;
11 * Implements hook_preprocess_HOOK() for HTML document templates.
13 function seven_preprocess_html(&$variables) {
14 // If on a node add or edit page, add a node-layout class.
15 $path_args = explode('/', \Drupal::request()->getPathInfo());
16 if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
17 foreach ($suggestions as $suggestion) {
18 if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
19 $variables['attributes']['class'][] = 'node-form-layout';
26 * Implements hook_pre_render_HOOK() for menu-local-tasks templates.
28 * Use preprocess hook to set #attached to child elements
29 * because they will be processed by Twig and drupal_render will
32 function seven_preprocess_menu_local_tasks(&$variables) {
33 if (!empty($variables['primary'])) {
34 $variables['primary']['#attached'] = [
36 'seven/drupal.nav-tabs',
40 elseif (!empty($variables['secondary'])) {
41 $variables['secondary']['#attached'] = [
43 'seven/drupal.nav-tabs',
50 * Implements hook_preprocess_HOOK() for menu-local-task templates.
52 function seven_preprocess_menu_local_task(&$variables) {
53 $variables['attributes']['class'][] = 'tabs__tab';
57 * Implements hook_preprocess_HOOK() for list of available node type templates.
59 function seven_preprocess_node_add_list(&$variables) {
60 if (!empty($variables['content'])) {
61 /** @var \Drupal\node\NodeTypeInterface $type */
62 foreach ($variables['content'] as $type) {
63 $variables['types'][$type->id()]['label'] = $type->label();
64 $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
70 * Implements hook_preprocess_HOOK() for block content add list templates.
72 * Displays the list of available custom block types for creation, adding
73 * separate variables for the label and url.
75 function seven_preprocess_block_content_add_list(&$variables) {
76 if (!empty($variables['content'])) {
77 foreach ($variables['content'] as $type) {
78 $variables['types'][$type->id()]['label'] = $type->label();
79 $options = ['query' => \Drupal::request()->query->all()];
80 $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', ['block_content_type' => $type->id()], $options);
86 * Implements hook_preprocess_block() for block content.
88 * Disables contextual links for all blocks.
90 function seven_preprocess_block(&$variables) {
91 if (isset($variables['title_suffix']['contextual_links'])) {
92 unset($variables['title_suffix']['contextual_links']);
93 unset($variables['elements']['#contextual_links']);
95 $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
100 * Implements hook_preprocess_HOOK() for block admin page templates.
102 function seven_preprocess_admin_block_content(&$variables) {
103 if (!empty($variables['content'])) {
104 foreach ($variables['content'] as $key => $item) {
105 $variables['content'][$key]['url'] = $item['url']->toString();
111 * Implements hook_preprocess_HOOK() for menu-local-action templates.
113 function seven_preprocess_menu_local_action(array &$variables) {
114 $variables['link']['#options']['attributes']['class'][] = 'button--primary';
115 $variables['link']['#options']['attributes']['class'][] = 'button--small';
117 // We require Modernizr's touch test for button styling.
118 $variables['#attached']['library'][] = 'core/modernizr';
122 * Implements hook_element_info_alter().
124 function seven_element_info_alter(&$type) {
125 // We require Modernizr for button styling.
126 if (isset($type['button'])) {
127 $type['button']['#attached']['library'][] = 'core/modernizr';
132 * Implements hook_preprocess_install_page().
134 function seven_preprocess_install_page(&$variables) {
135 // Seven has custom styling for the install page.
136 $variables['#attached']['library'][] = 'seven/install-page';
140 * Implements hook_preprocess_maintenance_page().
142 function seven_preprocess_maintenance_page(&$variables) {
143 // Seven has custom styling for the maintenance page.
144 $variables['#attached']['library'][] = 'seven/maintenance-page';
148 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
150 * Changes vertical tabs to container and adds meta information.
152 function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
153 /** @var \Drupal\node\NodeInterface $node */
154 $node = $form_state->getFormObject()->getEntity();
156 $form['#theme'] = ['node_edit_form'];
157 $form['#attached']['library'][] = 'seven/node-form';
159 $form['advanced']['#type'] = 'container';
160 $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
162 '#attributes' => ['class' => ['entity-meta__header']],
163 '#type' => 'container',
164 '#group' => 'advanced',
167 '#type' => 'html_tag',
169 '#value' => $node->isPublished() ? t('Published') : t('Not published'),
170 '#access' => !$node->isNew(),
172 'class' => ['entity-meta__title'],
177 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved', 'container-inline']],
178 '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
182 '#wrapper_attributes' => ['class' => ['author', 'container-inline']],
183 '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
186 $form['revision_information']['#type'] = 'container';
187 $form['revision_information']['#group'] = 'meta';