Projet

Général

Profil

Paste
Télécharger (9,13 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / theme / alter.inc @ 87dbc3bf

1
<?php
2
/**
3
 * @file
4
 * alter.inc
5
 *
6
 * Contains various implementations of hook_*_alter().
7
 */
8

    
9
/**
10
 * Implements hook_css_alter().
11
 */
12
function bootstrap_css_alter(&$css) {
13
  $theme_path = drupal_get_path('theme', 'bootstrap');
14
  // Exclude specified CSS files from theme.
15
  $excludes = bootstrap_get_theme_info(NULL, 'exclude][css');
16
  // Add Bootstrap CDN file and overrides.
17
  $bootstrap_cdn = theme_get_setting('bootstrap_cdn');
18
  if ($bootstrap_cdn) {
19
    // Add CDN.
20
    if (theme_get_setting('bootstrap_bootswatch')) {
21
      $cdn = '//netdna.bootstrapcdn.com/bootswatch/' . $bootstrap_cdn  . '/' . theme_get_setting('bootstrap_bootswatch') . '/bootstrap.min.css';
22
    }
23
    else {
24
      $cdn = '//netdna.bootstrapcdn.com/bootstrap/' . $bootstrap_cdn  . '/css/bootstrap.min.css';
25
    }
26
    $css[$cdn] = array(
27
      'data' => $cdn,
28
      'type' => 'external',
29
      'every_page' => TRUE,
30
      'media' => 'all',
31
      'preprocess' => FALSE,
32
      'group' => CSS_THEME,
33
      'browsers' => array('IE' => TRUE, '!IE' => TRUE),
34
      'weight' => -2,
35
    );
36
    // Add overrides.
37
    $override = $theme_path . '/css/overrides.css';
38
    $css[$override] = array(
39
      'data' => $override,
40
      'type' => 'file',
41
      'every_page' => TRUE,
42
      'media' => 'all',
43
      'preprocess' => TRUE,
44
      'group' => CSS_THEME,
45
      'browsers' => array('IE' => TRUE, '!IE' => TRUE),
46
      'weight' => -1,
47
    );
48
  }
49
  if (!empty($excludes)) {
50
    $css = array_diff_key($css, drupal_map_assoc($excludes));
51
  }
52
}
53

    
54
/**
55
 * Implements hook_element_info_alter().
56
 */
57
function bootstrap_element_info_alter(&$elements) {
58
  foreach ($elements as &$element) {
59
    // Process all elements.
60
    $element['#process'][] = '_bootstrap_process_element';
61
    // Process input elements.
62
    if (!empty($element['#input'])) {
63
      $element['#process'][] = '_bootstrap_process_input';
64
    }
65
    // Process core's fieldset element.
66
    if (!empty($element['#type']) && $element['#type'] === 'fieldset') {
67
      $element['#theme_wrappers'] = array('bootstrap_panel');
68
    }
69
    if (!empty($element['#theme']) && $element['#theme'] === 'fieldset') {
70
      $element['#theme'] = 'bootstrap_panel';
71
    }
72
    // Replace #process function.
73
    if (!empty($element['#process']) && ($key = array_search('form_process_fieldset', $element['#process'])) !== FALSE) {
74
      $element['#process'][$key] = '_bootstrap_process_fieldset';
75
    }
76
    // Replace #pre_render function.
77
    if (!empty($element['#pre_render']) && ($key = array_search('form_pre_render_fieldset', $element['#pre_render'])) !== FALSE) {
78
      $element['#pre_render'][$key] = '_bootstrap_pre_render_fieldset';
79
    }
80
    // Replace #theme_wrappers function.
81
    if (!empty($element['#theme_wrappers']) && ($key = array_search('fieldset', $element['#theme_wrappers'])) !== FALSE) {
82
      $element['#theme_wrappers'][$key] = 'bootstrap_panel';
83
    }
84
  }
85
}
86

    
87
/**
88
 * Implements hook_form_alter().
89
 */
90
function bootstrap_form_alter(array &$form, array &$form_state = array(), $form_id = NULL) {
91
  if ($form_id) {
92
    // IDs of forms that should be ignored. Make this configurable?
93
    // @todo is this still needed?
94
    $form_ids = array(
95
      'node_form',
96
      'system_site_information_settings',
97
      'user_profile_form',
98
      'node_delete_confirm',
99
    );
100
    // Only wrap in container for certain form.
101
    if (!in_array($form_id, $form_ids) && !isset($form['#node_edit_form']) && isset($form['actions']) && isset($form['actions']['#type']) && ($form['actions']['#type'] == 'actions')) {
102
      $form['actions']['#theme_wrappers'] = array();
103
    }
104

    
105
    switch ($form_id) {
106
      case 'system_theme_settings':
107
        // Include the settings form here.
108
        bootstrap_include('bootstrap', 'theme/settings.inc');
109
        _bootstrap_settings_form($form, $form_state);
110
        break;
111

    
112
      case 'search_form':
113
        // Add a clearfix class so the results don't overflow onto the form.
114
        $form['#attributes']['class'][] = 'clearfix';
115

    
116
        // Remove container-inline from the container classes.
117
        $form['basic']['#attributes']['class'] = array();
118

    
119
        // Hide the default button from display.
120
        $form['basic']['submit']['#attributes']['class'][] = 'element-invisible';
121

    
122
        // Implement a theme wrapper to add a submit button containing a search
123
        // icon directly after the input element.
124
        $form['basic']['keys']['#theme_wrappers'] = array('bootstrap_search_form_wrapper');
125
        $form['basic']['keys']['#title'] = '';
126
        $form['basic']['keys']['#attributes']['placeholder'] = t('Search');
127
        break;
128

    
129
      case 'search_block_form':
130
        $form['#attributes']['class'][] = 'form-search';
131

    
132
        $form['search_block_form']['#title'] = '';
133
        $form['search_block_form']['#attributes']['placeholder'] = t('Search');
134

    
135
        // Hide the default button from display and implement a theme wrapper
136
        // to add a submit button containing a search icon directly after the
137
        // input element.
138
        $form['actions']['submit']['#attributes']['class'][] = 'element-invisible';
139
        $form['search_block_form']['#theme_wrappers'] = array('bootstrap_search_form_wrapper');
140

    
141
        // Apply a clearfix so the results don't overflow onto the form.
142
        $form['#attributes']['class'][] = 'content-search';
143
        break;
144
    }
145

    
146
  }
147
}
148

    
149
/**
150
 * Implements hook_js_alter().
151
 */
152
function bootstrap_js_alter(&$js) {
153
  // Exclude specified JavaScript files from theme.
154
  $excludes = bootstrap_get_theme_info(NULL, 'exclude][js');
155

    
156
  $theme_path = drupal_get_path('theme', 'bootstrap');
157

    
158
  // Add or replace JavaScript files when matching paths are detected.
159
  // Replacement files must begin with '_', like '_node.js'.
160
  $files = file_scan_directory($theme_path . '/js', '/\.js$/');
161
  foreach ($files as $file) {
162
    $path = str_replace($theme_path . '/js/', '', $file->uri);
163
    // Detect if this is a replacement file.
164
    $replace = FALSE;
165
    if (preg_match('/^[_]/', $file->filename)) {
166
      $replace = TRUE;
167
      $path = dirname($path) . '/' . preg_replace('/^[_]/', '', $file->filename);
168
    }
169
    $matches = array();
170
    if (preg_match('/^modules\/([^\/]*)/', $path, $matches)) {
171
      if (!module_exists($matches[1])) {
172
        continue;
173
      }
174
      else {
175
        $path = str_replace('modules/' . $matches[1], drupal_get_path('module', $matches[1]), $path);
176
      }
177
    }
178
    // Path should always exist to either add or replace JavaScript file.
179
    if (!empty($js[$path])) {
180
      // Replace file.
181
      if ($replace) {
182
        $js[$file->uri] = $js[$path];
183
        $js[$file->uri]['data'] = $file->uri;
184
        unset($js[$path]);
185
      }
186
      // Add file.
187
      else {
188
        $js[$file->uri] = drupal_js_defaults($file->uri);
189
        $js[$file->uri]['group'] = JS_THEME;
190
      }
191
    }
192
  }
193

    
194
  // Always add bootstrap.js last.
195
  $bootstrap = $theme_path . '/js/bootstrap.js';
196
  $js[$bootstrap] = drupal_js_defaults($bootstrap);
197
  $js[$bootstrap]['group'] = JS_THEME;
198
  $js[$bootstrap]['scope'] = 'footer';
199

    
200
  if (!empty($excludes)) {
201
    $js = array_diff_key($js, drupal_map_assoc($excludes));
202
  }
203

    
204
  // Add Bootstrap settings.
205
  $js['settings']['data'][]['bootstrap'] = array(
206
    'anchorsFix' => theme_get_setting('bootstrap_anchors_fix'),
207
    'anchorsSmoothScrolling' => theme_get_setting('bootstrap_anchors_smooth_scrolling'),
208
    'popoverEnabled' => theme_get_setting('bootstrap_popover_enabled'),
209
    'popoverOptions' => array(
210
      'animation' => (int) theme_get_setting('bootstrap_popover_animation'),
211
      'html' => (int) theme_get_setting('bootstrap_popover_html'),
212
      'placement' => theme_get_setting('bootstrap_popover_placement'),
213
      'selector' => theme_get_setting('bootstrap_popover_selector'),
214
      'trigger' => implode(' ', array_filter(array_values((array) theme_get_setting('bootstrap_popover_trigger')))),
215
      'title' => theme_get_setting('bootstrap_popover_title'),
216
      'content' => theme_get_setting('bootstrap_popover_content'),
217
      'delay' => (int) theme_get_setting('bootstrap_popover_delay'),
218
      'container' => theme_get_setting('bootstrap_popover_container'),
219
    ),
220
    'tooltipEnabled' => theme_get_setting('bootstrap_tooltip_enabled'),
221
    'tooltipOptions' => array(
222
      'animation' => (int) theme_get_setting('bootstrap_tooltip_animation'),
223
      'html' => (int) theme_get_setting('bootstrap_tooltip_html'),
224
      'placement' => theme_get_setting('bootstrap_tooltip_placement'),
225
      'selector' => theme_get_setting('bootstrap_tooltip_selector'),
226
      'trigger' => implode(' ', array_filter(array_values((array) theme_get_setting('bootstrap_tooltip_trigger')))),
227
      'delay' => (int) theme_get_setting('bootstrap_tooltip_delay'),
228
      'container' => theme_get_setting('bootstrap_tooltip_container'),
229
    ),
230
  );
231

    
232
  // Add CDN.
233
  if (theme_get_setting('bootstrap_cdn')) {
234
    $cdn = '//netdna.bootstrapcdn.com/bootstrap/' . theme_get_setting('bootstrap_cdn')  . '/js/bootstrap.min.js';
235
    $js[$cdn] = drupal_js_defaults();
236
    $js[$cdn]['data'] = $cdn;
237
    $js[$cdn]['type'] = 'external';
238
    $js[$cdn]['every_page'] = TRUE;
239
    $js[$cdn]['weight'] = -100;
240
  }
241
}
242

    
243
/**
244
 * Include #pre_render callbacks for elements.
245
 */
246
bootstrap_include('bootstrap', 'theme/pre-render.inc');
247

    
248
/**
249
 * Include #process callbacks for elements.
250
 */
251
bootstrap_include('bootstrap', 'theme/process.inc');