block_class.module in Block Class 6.2
Same filename and directory in other branches
Provides core logic for adding block classes.
Block Class provides a mechanism for adding specific classes to individual blocks. These classes can be selected from a set provided by a theme's .info file or manually entered in a text field.
Themes may define block classes in their .info file using the 'block-class' array. These classes will be shown in a fieldset for the theme in the Block Class' field set on each block administration page. An example set of these classes would take this form: block-class[] = 'red-border' block-class[] = 'pull-quote' block-class[] = 'drop-cap'
File
block_class.moduleView source
<?php
/**
* @file
* Provides core logic for adding block classes.
*
* Block Class provides a mechanism for adding specific classes to individual
* blocks. These classes can be selected from a set provided by a theme's
* .info file or manually entered in a text field.
*
* Themes may define block classes in their .info file using the 'block-class'
* array. These classes will be shown in a fieldset for the theme in the Block
* Class' field set on each block administration page. An example set of these
* classes would take this form:
* block-class[] = 'red-border'
* block-class[] = 'pull-quote'
* block-class[] = 'drop-cap'
*/
/**
* Implementation of template_preprocess_block().
*
* Make $block_class string available to block.tpl.php.
*/
function block_class_preprocess_block(&$variables) {
if (isset($variables['block_classes'])) {
$variables['block_classes'] .= ' ' . block_class($variables['block']);
}
else {
$variables['block_classes'] = block_class($variables['block']);
}
}
/**
* Get the HTML-ready classes for the given block.
*
* @param object $block
* The block object.
*
* @return string
* A string containing all the classes for the block.
*
* @TODO Verify correct behavior in theme. All classes from the arbitrary
* text field should be present and only the selected classes from
* the theme checkboxes.
*/
function block_class($block) {
global $theme;
if (!isset($block->theme)) {
$block->theme = $theme;
}
$attributes = block_class_attributes($block);
$classes = array_key_exists('_', $attributes) ? $attributes['_'] : '';
if (array_key_exists($theme, $attributes)) {
$classes .= ' ' . implode(' ', $attributes[$theme]);
}
return check_plain($classes);
}
/**
* Extract the custom CSS class data for the given block.
*
* @param object $block
* The block object containing the keys 'module', 'delta', and 'theme'.
*
* @return array
* An Array map containing custom class information for the block.
* The key '_' will map to any arbitrary class values that have been entered
* for the block. Additionally, there will be a key for each of the theme's
* block class group if a class from that group has been specified.
*/
function block_class_attributes($block, $reset = FALSE) {
static $blocks;
// If we've not fetched the data, or we're being reset, fetch all the data:
if (is_null($blocks) || $reset) {
$result = db_query("SELECT module, delta, css_class FROM {block_class}");
while ($row = db_fetch_object($result)) {
$blocks[$row->module][$row->delta] = unserialize($row->css_class);
}
}
// Do we have info for this block:
if (isset($blocks[$block->module][$block->delta])) {
return $blocks[$block->module][$block->delta];
}
else {
return array(
'_' => '',
);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* In this case FORM_ID is block_admin_configure.
* Adds the block_class controls for block configurations.
*
* @TODO: This needs to be extended to work with the "block_add_block_form"
* form ID. This may require reverting to the more general
* block_class_form_alter() implementation.
*/
function block_class_form_block_admin_configure_alter(&$form, &$form_state) {
// -- Load any existing classes for this block.
$block = new stdClass();
$block->module = $form['module']['#value'];
$block->delta = $form['delta']['#value'];
$block_info = module_invoke($block->module, 'block', 'list');
$attributes = block_class_attributes($block);
$themes = list_themes();
$form['theme_block_classes'] = array(
'#type' => 'fieldset',
'#title' => t('Block classes'),
'#collapsible' => TRUE,
'#weight' => -1,
'#description' => t("IMPORTANT: If your theme does not support Block Class out of the box, you must add <code><?php print {$block_classes}; ?></code> to its block.tpl.php file so that the classes will be added. See the module's README.txt for more details."),
);
// -- Loop through the themes, and for active themes with block-class
// -- entries in their .info files, display options for those.
foreach ($themes as $theme) {
if ($theme->status > 0) {
$info = $theme->info;
$block_classes = $info['block-class'];
if (count($block_classes) > 0) {
$form['theme_block_classes']['themes'][$theme->name] = array(
'#type' => 'fieldset',
'#title' => t('@theme Block Classes', array(
'@theme' => $info['name'],
)),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#description' => t('The selected classes will be applied to this block when it is displayed using the @theme theme.', array(
'@theme' => $info['name'],
)),
);
$options = drupal_map_assoc($block_classes);
$form['theme_block_classes']['themes'][$theme->name][$theme->name . '-classes'] = array(
'#type' => 'checkboxes',
'#title' => t('Classes'),
'#options' => $options,
'#default_value' => empty($attributes[$theme->name]) ? array() : $attributes[$theme->name],
'#element_validate' => array(
'',
),
);
}
}
}
// -- The arbitrary class text field.
$form['theme_block_classes']['css_class'] = array(
'#type' => 'textfield',
'#title' => t("Global CSS for '#block' block", array(
'#block' => $block_info[$block->delta]['info'],
)),
'#default_value' => empty($attributes['_']) ? '' : $attributes['_'],
'#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.'),
);
$form['#submit'][] = 'block_class_form_submit';
}
/**
* Handle submission of the Block Class data.
*
* @param array $form
* The array of form elements.
* @param array $form_state
* The form field data.
*
* @TODO (low priority) validate the text in the arbitrary text field to
* verify.
*/
function block_class_form_submit($form, &$form_state) {
$themes = list_themes();
foreach ($themes as $theme) {
if ($theme->status > 0) {
drupal_set_message(print_r($form_state['values'][$theme->name . '-classes'], TRUE));
}
}
drupal_set_message(print_r($form_state['values']['css_class'], TRUE));
}
Functions
Name | Description |
---|---|
block_class | Get the HTML-ready classes for the given block. |
block_class_attributes | Extract the custom CSS class data for the given block. |
block_class_form_block_admin_configure_alter | Implements hook_form_FORM_ID_alter(). |
block_class_form_submit | Handle submission of the Block Class data. |
block_class_preprocess_block | Implementation of template_preprocess_block(). |