seven.theme 5.98 KB
Newer Older
1 2
<?php

3
/**
4 5 6 7
 * @file
 * Functions to support theming in the Seven theme.
 */

8
use Drupal\Core\Form\FormStateInterface;
9

10
/**
11
 * Implements hook_preprocess_HOOK() for HTML document templates.
12
 */
13 14 15 16 17 18
function seven_preprocess_html(&$variables) {
  // If on a node add or edit page, add a node-layout class.
  $path_args = explode('/', \Drupal::request()->getPathInfo());
  if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
    foreach ($suggestions as $suggestion) {
      if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
19
        $variables['attributes']['class'][] = 'node-form-layout';
20 21 22 23 24
      }
    }
  }
}

25
/**
26
 * Implements hook_pre_render_HOOK() for menu-local-tasks templates.
27
 *
28
 * Use preprocess hook to set #attached to child elements
29 30
 * because they will be processed by Twig and drupal_render will
 * be invoked.
31
 */
32
function seven_preprocess_menu_local_tasks(&$variables) {
33 34 35
  if (!empty($variables['primary'])) {
    $variables['primary']['#attached'] = array(
      'library' => array(
36
        'seven/drupal.nav-tabs',
37 38 39
      ),
    );
  }
40
  elseif (!empty($variables['secondary'])) {
41 42
    $variables['secondary']['#attached'] = array(
      'library' => array(
43
        'seven/drupal.nav-tabs',
44 45 46 47 48 49
      ),
    );
  }
}

/**
50
 * Implements hook_preprocess_HOOK() for menu-local-task templates.
51
 */
52 53
function seven_preprocess_menu_local_task(&$variables) {
  $variables['attributes']['class'][] = 'tabs__tab';
54 55
}

56
/**
57
 * Implements hook_preprocess_HOOK() for list of available node type templates.
58
 */
59 60
function seven_preprocess_node_add_list(&$variables) {
  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', array('node_type' => $type->id()));
65 66 67 68
    }
  }
}

69
/**
70
 * Implements hook_preprocess_HOOK() for block content add list templates.
71
 *
72
 * Displays the list of available custom block types for creation, adding
73
 * separate variables for the label and url.
74
 */
75 76 77
function seven_preprocess_block_content_add_list(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
78
      $variables['types'][$type->id()]['label'] = $type->label();
79 80
      $options = array('query' => \Drupal::request()->query->all());
      $variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', array('block_content_type' => $type->id()), $options);
81 82 83 84
    }
  }
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98
/**
 * Implements hook_preprocess_block() for block content.
 *
 * Disables contextual links for all blocks.
 */
function seven_preprocess_block(&$variables) {
  if (isset($variables['title_suffix']['contextual_links'])) {
    unset($variables['title_suffix']['contextual_links']);
    unset($variables['elements']['#contextual_links']);

    $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
  }
}

99
/**
100
 * Implements hook_preprocess_HOOK() for block admin page templates.
101
 */
102 103 104
function seven_preprocess_admin_block_content(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $key => $item) {
105
      $variables['content'][$key]['url'] = $item['url']->toString();
106 107 108 109
    }
  }
}

110
/**
111
 * Implements hook_preprocess_HOOK() for menu-local-action templates.
112
 */
113 114 115
function seven_preprocess_menu_local_action(array &$variables) {
  $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  $variables['link']['#options']['attributes']['class'][] = 'button--small';
116 117

  // We require Modernizr's touch test for button styling.
118
  $variables['#attached']['library'][] = 'core/modernizr';
119 120 121 122 123 124 125 126
}

/**
 * Implements hook_element_info_alter().
 */
function seven_element_info_alter(&$type) {
  // We require Modernizr for button styling.
  if (isset($type['button'])) {
127
    $type['button']['#attached']['library'][] = 'core/modernizr';
128 129 130
  }
}

131 132 133 134
/**
 * Implements hook_preprocess_install_page().
 */
function seven_preprocess_install_page(&$variables) {
135 136
  // Seven has custom styling for the install page.
  $variables['#attached']['library'][] = 'seven/install-page';
137 138 139 140 141 142
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function seven_preprocess_maintenance_page(&$variables) {
143 144
  // Seven has custom styling for the maintenance page.
  $variables['#attached']['library'][] = 'seven/maintenance-page';
145
}
146 147

/**
148
 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
149 150 151
 *
 * 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();
155

156
  $form['#theme'] = array('node_edit_form');
157
  $form['#attached']['library'][] = 'seven/node-form';
158

159
  $form['advanced']['#type'] = 'container';
160
  $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
161
  $form['meta'] = array(
162
    '#attributes' => array('class' => array('entity-meta__header')),
163 164 165 166
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => array(
167 168 169
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $node->isPublished() ? t('Published') : t('Not published'),
170
      '#access' => !$node->isNew(),
171 172 173
      '#attributes' => array(
        'class' => 'entity-meta__title',
      ),
174 175 176
    ),
    'changed' => array(
      '#type' => 'item',
177
      '#wrapper_attributes' => array('class' => array('entity-meta__last-saved', 'container-inline')),
178
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
179 180 181 182
    ),
    'author' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('author', 'container-inline')),
183
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
184 185 186 187 188
    ),
  );
  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';
}