1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Enhanced control over the CSS Classes of any Block.
|
6
|
*
|
7
|
* Block Class allows users to add classes to any block through the block's
|
8
|
* configuration interface. This implementation is based on an alteration of
|
9
|
* the Core block database table to leverage the Core Block API functions,
|
10
|
* objects and structure.
|
11
|
*/
|
12
|
|
13
|
/**
|
14
|
* Implements hook_permission().
|
15
|
*/
|
16
|
function block_class_permission() {
|
17
|
return array(
|
18
|
'administer block classes' => array(
|
19
|
'title' => t('Administer block classes'),
|
20
|
'description' => t('Set CSS classes for blocks.'),
|
21
|
),
|
22
|
);
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Implements theme_preprocess_block().
|
27
|
*
|
28
|
* Extend block's classes with any user defined classes.
|
29
|
*/
|
30
|
function block_class_preprocess_block(&$vars) {
|
31
|
$block = $vars['block'];
|
32
|
if (!empty($block->css_class)) {
|
33
|
$classes_array = explode(' ', $block->css_class);
|
34
|
foreach ($classes_array as $class) {
|
35
|
$vars['classes_array'][] = drupal_clean_css_identifier($class, array());
|
36
|
}
|
37
|
}
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Implements hook_preprocess_HOOK().
|
42
|
*
|
43
|
* Extend panel block's classes with any user defined classes.
|
44
|
*/
|
45
|
function block_class_preprocess_panels_pane(&$vars) {
|
46
|
if ($vars['pane']->type != 'block') {
|
47
|
return;
|
48
|
}
|
49
|
// Infer the block's $module and $delta from the pane subtype.
|
50
|
$block_parts = explode('-', $vars['pane']->subtype);
|
51
|
// Load the block based on the block parts.
|
52
|
$block = block_load($block_parts[0], $block_parts[1]);
|
53
|
// Add a generic 'module type' pane class.
|
54
|
$vars['classes_array'][] = drupal_html_class('pane-' . $block->module);
|
55
|
// Add $css_class to the $classes_array.
|
56
|
if (!empty($block->css_class)) {
|
57
|
$classes_array = explode(' ', $block->css_class);
|
58
|
foreach ($classes_array as $class) {
|
59
|
$vars['classes_array'][] = drupal_clean_css_identifier($class, array());
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Implements hook_form_alter().
|
66
|
*
|
67
|
* Alter block edit form to add configuration field.
|
68
|
*/
|
69
|
function block_class_form_alter(&$form, &$form_state, $form_id) {
|
70
|
if (user_access('administer block classes') && ($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form')) {
|
71
|
// Load statically cached block object used to display the form.
|
72
|
$block = block_load($form['module']['#value'], $form['delta']['#value']);
|
73
|
|
74
|
$form['settings']['css_class'] = array(
|
75
|
'#type' => 'textfield',
|
76
|
'#title' => t('CSS class(es)'),
|
77
|
'#default_value' => isset($block->css_class) ? $block->css_class : '',
|
78
|
'#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'),
|
79
|
'#maxlength' => 255,
|
80
|
);
|
81
|
|
82
|
$form['#submit'][] = 'block_class_form_submit';
|
83
|
}
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Helper function: additional submit callback for block configuration pages.
|
88
|
*
|
89
|
* Save supplied CSS classes.
|
90
|
*/
|
91
|
function block_class_form_submit($form, &$form_state) {
|
92
|
if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
|
93
|
// Only save if value has changed.
|
94
|
if (isset($form_state['values']['css_class']) && $form['settings']['css_class']['#default_value'] != $form_state['values']['css_class'] && user_access('administer blocks')) {
|
95
|
db_update('block')
|
96
|
->fields(array('css_class' => $form_state['values']['css_class']))
|
97
|
->condition('module', $form_state['values']['module'])
|
98
|
->condition('delta', $form_state['values']['delta'])
|
99
|
->execute();
|
100
|
// Flush all context module cache to use the updated css_class.
|
101
|
if (module_exists('context')) {
|
102
|
cache_clear_all('context', 'cache', TRUE);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
}
|