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

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

8
use Drupal\Component\Utility\Xss;
9
use Drupal\Core\Form\FormStateInterface;
10

11
/**
12
 * Implements hook_preprocess_HOOK() for HTML document templates.
13
 */
14 15 16 17 18 19
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) {
20
        $variables['attributes']['class'][] = 'node-form-layout';
21 22 23 24 25 26 27 28 29
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK() for page templates.
 */
function seven_preprocess_page(&$variables) {
30 31 32
  $variables['primary_local_tasks'] = $variables['tabs'];
  unset($variables['primary_local_tasks']['#secondary']);
  $variables['secondary_local_tasks'] = array(
33
    '#theme' => 'menu_local_tasks',
34
    '#secondary' => isset($variables['tabs']['#secondary']) ? $variables['tabs']['#secondary'] : '',
35
  );
36 37
}

38
/**
39
 * Implements hook_pre_render_HOOK() for menu-local-tasks templates.
40
 *
41
 * Use preprocess hook to set #attached to child elements
42 43
 * because they will be processed by Twig and drupal_render will
 * be invoked.
44
 */
45
function seven_preprocess_menu_local_tasks(&$variables) {
46 47 48
  if (!empty($variables['primary'])) {
    $variables['primary']['#attached'] = array(
      'library' => array(
49
        'seven/drupal.nav-tabs',
50 51 52
      ),
    );
  }
53
  elseif (!empty($variables['secondary'])) {
54 55
    $variables['secondary']['#attached'] = array(
      'library' => array(
56
        'seven/drupal.nav-tabs',
57 58 59 60 61 62
      ),
    );
  }
}

/**
63
 * Implements hook_preprocess_HOOK() for menu-local-task templates.
64
 */
65 66
function seven_preprocess_menu_local_task(&$variables) {
  $variables['attributes']['class'][] = 'tabs__tab';
67 68
}

69
/**
70
 * Implements hook_preprocess_HOOK() for list of available node type templates.
71
 */
72 73
function seven_preprocess_node_add_list(&$variables) {
  if (!empty($variables['content'])) {
74
    /** @var \Drupal\node\NodeTypeInterface $type */
75
    foreach ($variables['content'] as $type) {
76
      $variables['types'][$type->id()]['label'] = $type->label();
77
      $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', array('node_type' => $type->id()));
78 79 80 81
    }
  }
}

82
/**
83
 * Implements hook_preprocess_HOOK() for block content add list templates.
84
 *
85
 * Displays the list of available custom block types for creation, adding
86
 * separate variables for the label and url.
87
 */
88 89 90
function seven_preprocess_block_content_add_list(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
91
      $variables['types'][$type->id()]['label'] = $type->label();
92 93
      $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);
94 95 96 97
    }
  }
}

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

/**
110
 * Implements hook_preprocess_HOOK() for tablesort indicator templates.
111
 *
112 113
 * Uses Seven's image versions, so the arrows show up as black and not gray on
 * gray.
114
 */
115
function seven_preprocess_tablesort_indicator(&$variables) {
116
  $theme_path = drupal_get_path('theme', 'seven');
117 118
  $variables['arrow_asc'] = file_create_url($theme_path . '/images/arrow-asc.png');
  $variables['arrow_desc'] = file_create_url($theme_path . '/images/arrow-desc.png');
119
}
120

121
/**
122
 * Implements hook_preprocess_HOOK() for menu-local-action templates.
123
 */
124 125 126
function seven_preprocess_menu_local_action(array &$variables) {
  $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  $variables['link']['#options']['attributes']['class'][] = 'button--small';
127 128

  // We require Modernizr's touch test for button styling.
129
  $variables['#attached']['library'][] = 'core/modernizr';
130 131 132 133 134 135 136 137
}

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

142 143 144 145
/**
 * Implements hook_preprocess_install_page().
 */
function seven_preprocess_install_page(&$variables) {
146 147
  // Seven has custom styling for the install page.
  $variables['#attached']['library'][] = 'seven/install-page';
148 149 150 151 152 153
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function seven_preprocess_maintenance_page(&$variables) {
154 155
  // Seven has custom styling for the maintenance page.
  $variables['#attached']['library'][] = 'seven/maintenance-page';
156
}
157 158 159 160 161 162

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Changes vertical tabs to container and adds meta information.
 */
163
function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
164
  /** @var \Drupal\node\NodeInterface $node */
165
  $node = $form_state->getFormObject()->getEntity();
166

167
  $form['#theme'] = array('node_edit_form');
168
  $form['#attached']['library'][] = 'seven/node-form';
169

170
  $form['advanced']['#type'] = 'container';
171
  $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
172
  $form['meta'] = array(
173
    '#attributes' => array('class' => array('entity-meta__header')),
174 175 176 177
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => array(
178 179 180
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $node->isPublished() ? t('Published') : t('Not published'),
181
      '#access' => !$node->isNew(),
182 183 184
      '#attributes' => array(
        'class' => 'entity-meta__title',
      ),
185 186 187
    ),
    'changed' => array(
      '#type' => 'item',
188
      '#wrapper_attributes' => array('class' => array('entity-meta__last-saved', 'container-inline')),
189
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
190 191 192 193
    ),
    'author' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('author', 'container-inline')),
194
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
195 196 197 198 199
    ),
  );
  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';
}
200 201 202 203

/**
 * Implements hook_library_info_alter().
 */
204 205
function seven_library_info_alter(&$libraries, $extension) {
  if ($extension == 'core' && isset($libraries['drupal.vertical-tabs'])) {
206 207 208
    unset($libraries['drupal.vertical-tabs']['css']['component']['misc/vertical-tabs.css']);
    $libraries['drupal.vertical-tabs']['dependencies'][] = 'seven/vertical-tabs';
  }
209
  if ($extension == 'core' && isset($libraries['jquery.ui'])) {
210 211 212
    unset($libraries['jquery.ui']['css']['theme']['assets/vendor/jquery.ui/themes/base/theme.css']);
    $libraries['jquery.ui']['dependencies'][] = 'seven/seven.jquery.ui';
  }
213
  if ($extension == 'core' && isset($libraries['drupal.dialog'])) {
214 215 216 217
    unset($libraries['drupal.dialog']['css']['theme']['misc/dialog.theme.css']);
    $libraries['drupal.dialog']['dependencies'][] = 'seven/seven.drupal.dialog';
  }
}