You are here

block_class.module in Block Class 6

Provides core logic for adding block classes.

File

block_class.module
View source
<?php

/**
 * @file
 * Provides core logic for adding block classes.
 */

/**
 * Implements hook_perm().
 *
 * Define user permissions.
 */
function block_class_perm() {
  return array(
    'administer block classes',
  );
}

/**
 * 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.
 */
function block_class($block) {
  $attributes = block_class_attributes($block);
  return check_plain($attributes->css_class);
}

/**
 * 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 css_class, module, delta FROM {block_class}");
    while ($row = db_fetch_object($result)) {
      $blocks[$row->module][$row->delta] = $row;
    }
  }

  // Do we have info for this block:
  if (isset($blocks[$block->module][$block->delta])) {
    return $blocks[$block->module][$block->delta];
  }
  else {
    $undef = (object) NULL;
    $undef->css_class = '';
    return $undef;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * In this case FORM_ID is block_admin_configure.
 * Adds the block_class controls for block configurations.
 */
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'];
    $attributes = block_class_attributes($block);

    // More technical description for users with administer blocks permission.
    $description = t('Customize the styling of this block by adding CSS classes. You can add multiple classes.');
    if (user_access('administer blocks')) {
      $description .= ' ' . t("IMPORTANT: You must add <code>&lt;?php print {$block_classes}; ?&gt;</code> to your theme's block.tpl.php file to make the classes appear. See the module\\'s README.txt for more details.");
    }
    $form['block_class'] = array(
      '#type' => 'fieldset',
      '#title' => t('Block Class settings'),
      '#collapsible' => TRUE,
      '#weight' => 0,
      '#description' => $description,
    );
    $form['block_class']['css_class'] = array(
      '#type' => 'textfield',
      '#title' => t('CSS class(es)'),
      '#default_value' => $attributes->css_class,
      '#description' => t('Separate classes with a space.'),
      '#maxlength' => 255,
    );
    $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.
 */
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'];

      // Adding new block (for the "Add Block" form) requires special handling.
      // $delta is null if this is the "Add Block" form.
      if ($form_state['values']['form_id'] == 'block_add_block_form') {
        $last_block = db_last_insert_id('blocks', 'bid');
        $delta = $last_block;
      }
      db_query("DELETE FROM {block_class} WHERE module = '%s' AND delta = '%s'", $module, $delta);
      if (!empty($class)) {
        db_query("INSERT INTO {block_class} (module, delta, css_class) VALUES ('%s', '%s', '%s')", $module, $delta, $class);
      }
    }
  }
}

Functions

Namesort descending 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_alter Implements hook_form_FORM_ID_alter().
block_class_form_submit Handle submission of the Block Class data.
block_class_perm Implements hook_perm().
block_class_preprocess_block Implementation of template_preprocess_block().