You are here

regionclass.module in Region Class 7

A module providing a simple method for adding CSS classes to regions.

Region Class is a very simple module. It allows to assign CSS classes to regions on the theme-settings page. If the block groups module is enabled Region Class also exposes appropriate fields on the block configuration form for block groups.

The module stores its configuration data into the theme settings. Therefore it is possible to preseed those values from within a themes info file. E.g.:

region[footer] = Footer

settings[regionclass][footer][] = first-footer-class settings[regionclass][footer][] = second-footer-class

File

regionclass.module
View source
<?php

/**
 * @file
 * A module providing a simple method for adding CSS classes to regions.
 *
 * Region Class is a very simple module. It allows to assign CSS classes to
 * regions on the theme-settings page. If the block groups module is enabled
 * Region Class also exposes appropriate fields on the block configuration form
 * for block groups.
 *
 * The module stores its configuration data into the theme settings. Therefore
 * it is possible to preseed those values from within a themes info file. E.g.:
 *
 *     region[footer] = Footer
 *
 *     settings[regionclass][footer][] = first-footer-class
 *     settings[regionclass][footer][] = second-footer-class
 */

/**
 * Implements hook_preprocess_HOOK().
 *
 * Add user defined CSS classes to regions.
 */
function regionclass_preprocess_region(&$variables) {
  global $theme;

  // Check if we have settings for this region.
  $region = $variables['region'];
  $regionclass = theme_get_setting('regionclass', $theme);

  // Add static css classes to region.
  if (!empty($regionclass[$region])) {
    $variables['classes_array'] = array_merge($variables['classes_array'], $regionclass[$region]);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Inject region-class settings into theme settings page.
 */
function regionclass_form_system_theme_settings_alter(&$form, &$form_state) {
  $theme_key = substr($form['var']['#value'], 6, -9);
  if (empty($theme_key)) {
    return;
  }
  $form['regionclass'] = array(
    '#type' => 'fieldset',
    '#title' => t('Region Class'),
    '#description' => t('Assign CSS-classes to regions. Use space as a separator when specifying multiple classes.'),
    '#tree' => TRUE,
  );
  $regionclass = theme_get_setting('regionclass', $theme_key);
  $regions = system_region_list($theme_key);
  foreach ($regions as $region => $label) {
    $default_value = isset($regionclass[$region]) ? $regionclass[$region] : array();
    $form['regionclass'][$region] = array(
      '#type' => 'textfield',
      '#title' => t('CSS classes for region %label', array(
        '%label' => $label,
      )),
      '#default_value' => implode(' ', $default_value),
    );
  }
  $form['#validate'][] = 'regionclass_settings_validate';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function regionclass_form_blockgroup_add_form_alter(&$form, &$form_state) {
  _regionclass_augment_blockgroup_edit_form($form, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function regionclass_form_block_admin_configure_alter(&$form, &$form_state) {
  if ($form['module']['#value'] === 'blockgroup' && function_exists('blockgroup_get_region')) {
    _regionclass_augment_blockgroup_edit_form($form, $form_state);
  }
}

/**
 * Inject regionclass settings into blockgroup block edit form.
 */
function _regionclass_augment_blockgroup_edit_form(&$form, &$form_state) {

  // Region class settings.
  $form['settings']['regionclass'] = array(
    '#type' => 'fieldset',
    '#title' => t('Region class'),
    '#collapsible' => FALSE,
    '#description' => t('Assign CSS-classes to the region provided by this block group. Use space as a separator when specifying multiple classes.'),
    '#tree' => TRUE,
  );
  $theme_default = variable_get('theme_default', 'bartik');
  $admin_theme = variable_get('admin_theme');
  $region = blockgroup_get_region($form['delta']['#value']);
  foreach (list_themes() as $key => $theme) {

    // Only display enabled themes.
    if ($theme->status) {
      if (!empty($form['delta']['#value'])) {
        $regionclass = theme_get_setting('regionclass', $key);
        $default_value = isset($regionclass[$region]) ? $regionclass[$region] : array();
      }
      else {
        $default_value = array();
      }

      // Use a meaningful title for the main site theme and administrative
      // theme.
      $theme_title = t('CSS classes for @theme', array(
        '@theme' => $theme->info['name'],
      ));
      if ($key == $theme_default) {
        $theme_title = t('!prefix (default theme)', array(
          '!prefix' => $theme_title,
        ));
      }
      elseif ($admin_theme && $key == $admin_theme) {
        $theme_title = t('!prefix (administration theme)', array(
          '!prefix' => $theme_title,
        ));
      }
      $form['settings']['regionclass'][$key] = array(
        '#type' => 'textfield',
        '#title' => $theme_title,
        '#default_value' => implode(' ', $default_value),
        '#weight' => $key == $theme_default ? 9 : 10,
      );
    }
  }
  $form['#validate'][] = 'regionclass_settings_validate';

  // Our submit handler should be executed before blockgroup submit handler.
  array_unshift($form['#submit'], 'regionclass_blockgroup_edit_form_submit');
}

/**
 * Validation callback: Explode space-separated class names into array.
 */
function regionclass_settings_validate($form, &$form_state) {
  foreach ($form_state['values']['regionclass'] as $key => $classes_string) {
    $classes = array_filter(explode(" ", $classes_string));
    $form_state['values']['regionclass'][$key] = $classes;
  }

  // Remove empty entries.
  $form_state['values']['regionclass'] = array_filter($form_state['values']['regionclass']);
}

/**
 * Submit callback: Store CSS classes added to a block group as theme settings.
 */
function regionclass_blockgroup_edit_form_submit($form, &$form_state) {
  $machine_name = $form_state['values']['machine_name'];
  $region = blockgroup_get_region($machine_name);
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      $var = 'theme_' . $key . '_settings';
      $settings = variable_get($var);
      if (!empty($form_state['values']['regionclass'][$key])) {
        $settings['regionclass'][$region] = $form_state['values']['regionclass'][$key];
      }
      else {
        unset($settings['regionclass']);
      }
      variable_set($var, $settings);
    }
  }
}

Functions

Namesort descending Description
regionclass_blockgroup_edit_form_submit Submit callback: Store CSS classes added to a block group as theme settings.
regionclass_form_blockgroup_add_form_alter Implements hook_form_FORM_ID_alter().
regionclass_form_block_admin_configure_alter Implements hook_form_FORM_ID_alter().
regionclass_form_system_theme_settings_alter Implements hook_form_FORM_ID_alter().
regionclass_preprocess_region Implements hook_preprocess_HOOK().
regionclass_settings_validate Validation callback: Explode space-separated class names into array.
_regionclass_augment_blockgroup_edit_form Inject regionclass settings into blockgroup block edit form.