| 1 |
<?php
|
| 2 |
// $Id: block_class.module,v 1.6 2009/04/21 15:39:31 shannonlucas Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file block_class.module
|
| 6 |
* Provides core logic for adding block classes.
|
| 7 |
*
|
| 8 |
* Block Class provides a mechanism for adding specific classes to individual
|
| 9 |
* blocks. These classes can be selected from a set provided by a theme's
|
| 10 |
* .info file or manually entered in a text field.
|
| 11 |
*
|
| 12 |
* Themes may define block classes in their .info file using the 'block-class'
|
| 13 |
* array. These classes will be shown in a fieldset for the theme in the Block
|
| 14 |
* Class' field set on each block administration page. An example set of these
|
| 15 |
* classes would take this form:
|
| 16 |
* block-class[] = 'red-border'
|
| 17 |
* block-class[] = 'pull-quote'
|
| 18 |
* block-class[] = 'drop-cap'
|
| 19 |
*/
|
| 20 |
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Get the HTML-ready classes for the given block.
|
| 24 |
*
|
| 25 |
* @param $block object The block.
|
| 26 |
*
|
| 27 |
* @return string All the classes for the block.
|
| 28 |
*
|
| 29 |
* @TODO Verify correct behavior in theme. All classes from the arbitrary
|
| 30 |
* text field should be present and only the selected classes from
|
| 31 |
* the theme checkboxes.
|
| 32 |
*/
|
| 33 |
function block_class($block) {
|
| 34 |
global $theme;
|
| 35 |
|
| 36 |
if (!isset($block->theme)) {
|
| 37 |
$block->theme = $theme;
|
| 38 |
}
|
| 39 |
|
| 40 |
$attributes = block_class_attributes($block);
|
| 41 |
$classes = array_key_exists('_', $attributes) ? $attributes['_'] : '';
|
| 42 |
|
| 43 |
if (array_key_exists($theme, $attributes)) {
|
| 44 |
$classes .= ' ' . implode(' ', $attributes[$theme]);
|
| 45 |
}
|
| 46 |
|
| 47 |
return $classes;
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Extract the custom CSS class data for the given block.
|
| 53 |
*
|
| 54 |
* @param $block object Contains the keys 'module', 'delta', and 'theme'.
|
| 55 |
*
|
| 56 |
* @return array A map containing custom class information for the block.
|
| 57 |
* The key '_' will map to any arbitrary class values that have
|
| 58 |
* been entered for the block. Additionally, there will be a key
|
| 59 |
* for each of the theme's block class group if a class from that
|
| 60 |
* group has been specified.
|
| 61 |
*/
|
| 62 |
function block_class_attributes($block) {
|
| 63 |
$result = db_fetch_object(
|
| 64 |
db_query("SELECT `css_class` FROM {block_class}
|
| 65 |
WHERE `module` = '%s' AND `delta` = '%s'",
|
| 66 |
$block->module, $block->delta));
|
| 67 |
$block_class = array('_' => '');
|
| 68 |
|
| 69 |
if ($result !== FALSE) {
|
| 70 |
$block_class = unserialize($result);
|
| 71 |
}
|
| 72 |
|
| 73 |
return $block_class;
|
| 74 |
}
|
| 75 |
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Implementation of hook_form_FORM_ID_alter() for block_admin_configure.
|
| 79 |
* Adds the block_class controls for block configurations.
|
| 80 |
*
|
| 81 |
* @param $form Nested array of form elements that comprise the form.
|
| 82 |
* @param $form_state A keyed array containing the current state of the form.
|
| 83 |
*/
|
| 84 |
function block_class_form_block_admin_configure_alter(&$form, &$form_state) {
|
| 85 |
//-- Load any existing classes for this block.
|
| 86 |
$block = new stdClass();
|
| 87 |
$block->module = $form['module']['#value'];
|
| 88 |
$block->delta = $form['delta']['#value'];
|
| 89 |
$block_info = module_invoke($block->module, 'block', 'list');
|
| 90 |
$attributes = block_class_attributes($block);
|
| 91 |
$themes = list_themes();
|
| 92 |
|
| 93 |
$form['theme_block_classes'] = array(
|
| 94 |
'#type' => 'fieldset',
|
| 95 |
'#title' => t('Block Classes Settings'),
|
| 96 |
'#collapsible' => TRUE,
|
| 97 |
'#weight' => -1,
|
| 98 |
'#description' => t('IMPORTANT: If your theme does not support Block Class out of the box, you must add <?php print block_class($block); ?> to its block.tpl.php file so that the classes will be added. See the module documentation for more details.'),
|
| 99 |
);
|
| 100 |
|
| 101 |
//-- Loop through the themes, and for active themes with block-class
|
| 102 |
//-- entries in their .info files, display options for those.
|
| 103 |
foreach($themes as $theme) {
|
| 104 |
if ($theme->status > 0) {
|
| 105 |
$info = $theme->info;
|
| 106 |
$block_classes = $info['block-class'];
|
| 107 |
|
| 108 |
if (count($block_classes) > 0) {
|
| 109 |
$form['theme_block_classes']['themes'][$theme->name] = array(
|
| 110 |
'#type' => 'fieldset',
|
| 111 |
'#title' => t('@theme Block Classes', array('@theme' => $info['name'])),
|
| 112 |
'#collapsed' => TRUE,
|
| 113 |
'#collapsible' => TRUE,
|
| 114 |
'#description' => t('The selected classes will be applied to this block when it is displayed using the @theme theme.',
|
| 115 |
array('@theme' => $info['name'])),
|
| 116 |
);
|
| 117 |
|
| 118 |
$options = drupal_map_assoc($block_classes);
|
| 119 |
|
| 120 |
$form['theme_block_classes']['themes'][$theme->name][$theme->name . '-classes'] = array(
|
| 121 |
'#type' => 'checkboxes',
|
| 122 |
'#title' => t('Classes'),
|
| 123 |
'#options' => $options,
|
| 124 |
'#default_value' => empty($attributes[$theme->name]) ? array() : $attributes[$theme->name],
|
| 125 |
'#element_validate' => array(''),
|
| 126 |
);
|
| 127 |
}
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
//-- The arbitrary class text field.
|
| 132 |
$form['theme_block_classes']['css_class'] = array(
|
| 133 |
'#type' => 'textfield',
|
| 134 |
'#title' => t("Global CSS for '#block' block", array('#block' => $block_info[$block->delta]['info'])),
|
| 135 |
'#default_value' => empty($attributes['_']) ? '' : $attributes['_'],
|
| 136 |
'#description' => t('Enter CSS classes not defined in the theme here. These will be applied to the block in every Block Class enabled theme. Separate classes with a space.'),
|
| 137 |
);
|
| 138 |
|
| 139 |
$form['#validate'][] = 'block_class_validate';
|
| 140 |
$form['#submit'][] = 'block_class_submit';
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Handle submission of the Block Class data.
|
| 146 |
*
|
| 147 |
* @param $form The array of form elements.
|
| 148 |
* @param &$form_state The form field data.
|
| 149 |
*/
|
| 150 |
function block_class_submit($form, &$form_state) {
|
| 151 |
$themes = list_themes();
|
| 152 |
foreach($themes as $theme) {
|
| 153 |
if ($theme->status > 0) {
|
| 154 |
drupal_set_message(print_r($form_state['values'][$theme->name . '-classes'], TRUE));
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
drupal_set_message(print_r($form_state['values']['css_class'], TRUE));
|
| 159 |
}
|
| 160 |
|
| 161 |
|
| 162 |
/**
|
| 163 |
* Validate the arbitrary CSS class field.
|
| 164 |
*
|
| 165 |
* @param $form The array of form elements.
|
| 166 |
* @param &$form_state The form field data.
|
| 167 |
*
|
| 168 |
* @TODO (low priority) validate the text in the arbitrary text field to verify
|
| 169 |
*/
|
| 170 |
function block_class_validate($form, &$form_state) {
|
| 171 |
}
|