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

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

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

70
/**
71
 * Implements hook_preprocess_HOOK() for block content add list templates.
72
 *
73
 * Displays the list of available custom block types for creation, adding
74
 * separate variables for the label and url.
75
 */
76 77 78
function seven_preprocess_block_content_add_list(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
79
      $variables['types'][$type->id()]['label'] = $type->label();
80 81
      $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);
82 83 84 85
    }
  }
}

86
/**
87
 * Implements hook_preprocess_HOOK() for block admin page templates.
88
 */
89 90 91
function seven_preprocess_admin_block_content(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $key => $item) {
92
      $variables['content'][$key]['url'] = $item['url']->toString();
93 94 95 96
    }
  }
}

97
/**
98
 * Implements hook_preprocess_HOOK() for menu-local-action templates.
99
 */
100 101 102
function seven_preprocess_menu_local_action(array &$variables) {
  $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  $variables['link']['#options']['attributes']['class'][] = 'button--small';
103 104

  // We require Modernizr's touch test for button styling.
105
  $variables['#attached']['library'][] = 'core/modernizr';
106 107 108 109 110 111 112 113
}

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

118 119 120 121
/**
 * Implements hook_preprocess_install_page().
 */
function seven_preprocess_install_page(&$variables) {
122 123
  // Seven has custom styling for the install page.
  $variables['#attached']['library'][] = 'seven/install-page';
124 125 126 127 128 129
}

/**
 * Implements hook_preprocess_maintenance_page().
 */
function seven_preprocess_maintenance_page(&$variables) {
130 131
  // Seven has custom styling for the maintenance page.
  $variables['#attached']['library'][] = 'seven/maintenance-page';
132
}
133 134 135 136 137 138

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

143
  $form['#theme'] = array('node_edit_form');
144
  $form['#attached']['library'][] = 'seven/node-form';
145

146
  $form['advanced']['#type'] = 'container';
147
  $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
148
  $form['meta'] = array(
149
    '#attributes' => array('class' => array('entity-meta__header')),
150 151 152 153
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => array(
154 155 156
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $node->isPublished() ? t('Published') : t('Not published'),
157
      '#access' => !$node->isNew(),
158 159 160
      '#attributes' => array(
        'class' => 'entity-meta__title',
      ),
161 162 163
    ),
    'changed' => array(
      '#type' => 'item',
164
      '#wrapper_attributes' => array('class' => array('entity-meta__last-saved', 'container-inline')),
165
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
166 167 168 169
    ),
    'author' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('author', 'container-inline')),
170
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
171 172 173 174 175
    ),
  );
  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';
}
176 177 178 179

/**
 * Implements hook_library_info_alter().
 */
180 181
function seven_library_info_alter(&$libraries, $extension) {
  if ($extension == 'core' && isset($libraries['drupal.vertical-tabs'])) {
182 183 184
    unset($libraries['drupal.vertical-tabs']['css']['component']['misc/vertical-tabs.css']);
    $libraries['drupal.vertical-tabs']['dependencies'][] = 'seven/vertical-tabs';
  }
185
  if ($extension == 'core' && isset($libraries['jquery.ui'])) {
186 187 188
    unset($libraries['jquery.ui']['css']['theme']['assets/vendor/jquery.ui/themes/base/theme.css']);
    $libraries['jquery.ui']['dependencies'][] = 'seven/seven.jquery.ui';
  }
189
  if ($extension == 'core' && isset($libraries['drupal.dialog'])) {
190 191 192 193
    unset($libraries['drupal.dialog']['css']['theme']['misc/dialog.theme.css']);
    $libraries['drupal.dialog']['dependencies'][] = 'seven/seven.drupal.dialog';
  }
}