123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * Implements hook_permission().
- */
- function block_class_permission() {
- return array(
- 'administer block classes' => array(
- 'title' => t('Administer block classes'),
- 'description' => t('Set CSS classes for blocks.'),
- ),
- );
- }
- /*
- * Extend block's classes with any user defined classes.
- */
- function block_class_preprocess_block(&$vars) {
- $block = $vars['block'];
- /* The original code: */
- //$classes = block_class($block);
- //$vars['classes_array'] = array_merge($vars['classes_array'], explode(' ', $classes));
- /* Replaced with the following so it doesn't fire that many queries if you have a lot of blocks */
- static $_block_class_blocks_classes;
- if (! isset ($_block_class_blocks_classes)) {
- $_block_class_blocks_classes = array();
- $result = db_query('SELECT css_class, module, delta FROM {block_class}');
- while ($record = $result->fetchObject()) {
- $_block_class_blocks_classes[$record->module][$record->delta] = $record->css_class;
- }
- }
- if (isset($_block_class_blocks_classes[$block->module][$block->delta])) {
- $classes = $_block_class_blocks_classes[$block->module][$block->delta];
- $vars['classes_array'] = array_merge($vars['classes_array'], explode(' ', $classes));
- }
- }
- /*
- * Extend panel block's classes with any user defined classes.
- * Implements hook_preprocess_hook()
- */
- function block_class_preprocess_panels_pane(&$vars) {
- if ($vars['pane']->type != 'block') {
- return;
- }
- // Infer the block's $module and $delta from the pane subtype.
- $block_parts = explode('-', $vars['pane']->subtype);
- // Load the block based on the block parts.
- $block = block_load($block_parts[0], $block_parts[1]);
- // Return block_class classes as string.
- $css_class = block_class($block);
- // Add a generic 'module type' pane class.
- $vars['classes_array'][] = drupal_html_class('pane-' . $block->module);
- // Add $css_class to the $classes_array.
- $vars['classes_array'][] = $css_class;
- }
- /**
- * Return classes as string
- */
- function block_class($block) {
- $ret = db_query('SELECT css_class FROM {block_class} WHERE module = :module AND delta = :delta', array(':module' => $block->module, ':delta' => $block->delta))->fetchField();
- return $ret ? check_plain ($ret) : '';
- }
- /**
- * Alter block edit form
- */
- function block_class_form_alter(&$form, &$form_state, $form_id) {
- if (user_access('administer block classes') && $form_id == 'block_admin_configure' || $form_id == 'block_add_block_form') {
- $block = new stdClass;
- $block->module = $form['module']['#value'];
- $block->delta = $form['delta']['#value'];
- $css_class = block_class($block);
- // Create a more technical description for users with administer blocks permission.
- $description = t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.');
- $form['settings']['css_class'] = array(
- '#type' => 'textfield',
- '#title' => t('CSS class(es)'),
- '#default_value' => $css_class,
- '#description' => t('Separate classes with a space.'),
- '#maxlength' => 255,
- );
- $form['#submit'][] = 'block_class_form_submit';
- }
- }
- /**
- * Save supplied class.
- */
- function block_class_form_submit($form, &$form_state) {
- if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
- if (isset($form_state['values']['css_class']) && user_access('administer blocks')) {
- $module = $form_state['values']['module'];
- $delta = $form_state['values']['delta'];
- $class = $form_state['values']['css_class'];
- db_delete('block_class')->condition('module', $module)->condition('delta', $delta)->execute();
- if (!empty($class)) {
- $id = db_insert('block_class')->fields(array('module' => $module, 'delta' => $delta, 'css_class' => $class))->execute();
- }
- }
- }
- }
- /**
- * Implements hook_features_api().
- */
- function block_class_features_api() {
- return array(
- 'block_class' => array(
- 'name' => t('Block class'),
- 'feature_source' => TRUE,
- 'default_hook' => 'block_class_features_default_class',
- 'file' => drupal_get_path('module', 'block_class') . '/block_class.features.inc',
- ),
- );
- }
|