You are here

collapsiblock.module in Collapsiblock 7

Make blocks collapsible.

File

collapsiblock.module
View source
<?php

/**
 * @file
 * Make blocks collapsible.
 */

/**
 * Implements hook_init().
 */
function collapsiblock_init() {

  // Load the settings.
  global $theme;
  $current_theme = $theme ? $theme : variable_get('theme_default', 'bartik');
  $theme_settings = variable_get(str_replace('/', '_', 'theme_' . $current_theme . '_settings'), array());
  $theme_settings = array_merge(collapsiblock_default_settings(), $theme_settings, array());
  drupal_add_library('system', 'jquery.cookie');
  drupal_add_js(array(
    'collapsiblock' => array(
      'blocks' => array_change_key_case(variable_get('collapsiblock_settings', array()), CASE_LOWER),
      'default_state' => variable_get('collapsiblock_default_state', 1),
      'active_pages' => variable_get('collapsiblock_active_pages', FALSE),
      'slide_type' => variable_get('collapsiblock_slide_type', 1),
      'slide_speed' => variable_get('collapsiblock_slide_speed', 200),
      'block_title' => $theme_settings['collapsiblock_title'],
      'block' => $theme_settings['collapsiblock_block'],
      'block_content' => $theme_settings['collapsiblock_content'],
    ),
  ), array(
    'type' => 'setting',
    'scope' => JS_DEFAULT,
  ));
}

/**
 * Implements hook_menu().
 */
function collapsiblock_menu() {
  $items = array();
  $items['admin/config/user-interface/collapsiblock'] = array(
    'title' => 'Collapsiblock',
    'description' => 'Configuration for collapsible blocks',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'collapsiblock_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Admin settings.
 */
function collapsiblock_admin_settings($form, &$form_state) {
  $form = array();
  $form['collapsiblock_default_state'] = array(
    '#type' => 'radios',
    '#title' => t('Default block collapse behavior'),
    '#options' => array(
      1 => t('None.'),
      2 => t('Collapsible, expanded by default.'),
      3 => t('Collapsible, collapsed by default.'),
      4 => t('Collapsible, collapsed all the time.'),
    ),
    '#default_value' => variable_get('collapsiblock_default_state', 1),
  );
  $form['collapsiblock_active_pages'] = array(
    '#type' => 'checkbox',
    '#title' => t('Remember collapsed state on active pages'),
    '#default_value' => variable_get('collapsiblock_active_pages', FALSE),
    '#description' => t('Block can collapse even if it contains an active link (such as in menu blocks).'),
  );
  $form['collapsiblock_slide_type'] = array(
    '#type' => 'radios',
    '#title' => t('Default animation type'),
    '#options' => array(
      1 => t('Slide'),
      2 => t('Fade and slide'),
    ),
    '#description' => t('Slide is the Drupal default while Fade and slide adds a nice fade effect.'),
    '#default_value' => variable_get('collapsiblock_slide_type', 1),
  );
  $form['collapsiblock_slide_speed'] = array(
    '#type' => 'select',
    '#title' => t('Animation speed'),
    '#options' => drupal_map_assoc(array(
      '0',
      '50',
      '100',
      '200',
      '300',
      '400',
      '500',
      '700',
      '1000',
      '1300',
    )),
    '#description' => t('The animation speed in milliseconds.'),
    '#default_value' => variable_get('collapsiblock_slide_speed', 200),
  );
  $form['collapsiblock_help'] = array(
    '#markup' => t('If collapsiblock doesn\'t work out of the box, you can force CSS selectors on <a href="!url">appearance settings</a>.', array(
      '!url' => url('admin/appearance/settings'),
    )),
  );
  $form = system_settings_form($form);
  return $form;
}

/**
 * Default theme's settings.
 */
function collapsiblock_default_settings() {
  $defaults = array(
    'collapsiblock_block' => 'div.block',
    'collapsiblock_content' => 'div.content',
    'collapsiblock_title' => ':header:first',
  );
  return $defaults;
}

/**
 * Implements hook_form_alter().
 */
function collapsiblock_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'system_theme_settings') {
    $settings = variable_get($form['var']['#value'], array());
    $settings = array_merge(collapsiblock_default_settings(), $settings, array());
    $form['collapsiblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Collapsiblock selectors'),
      '#description' => t("Force <a href='http://api.jquery.com/category/selectors/'>CSS selector</a> if collapsiblock doesn't work out of the box"),
      '#weight' => 0,
      '#attributes' => array(
        'id' => 'collapsiblock_form',
      ),
    );
    $form['collapsiblock']['collapsiblock_block'] = array(
      '#type' => 'textfield',
      '#title' => t('Block'),
      '#default_value' => $settings['collapsiblock_block'],
    );
    $form['collapsiblock']['collapsiblock_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $settings['collapsiblock_title'],
    );
    $form['collapsiblock']['collapsiblock_content'] = array(
      '#type' => 'textfield',
      '#title' => t('Block content'),
      '#default_value' => $settings['collapsiblock_content'],
    );
  }
  if ($form_id == 'block_admin_configure') {
    $settings = variable_get('collapsiblock_settings', array());
    $form['#submit'][] = 'collapsiblock_submit';
    $form['settings']['collapsiblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Collapsible'),
      '#collapsible' => TRUE,
    );
    $block_id = 'block-' . str_replace('_', '-', $form['module']['#value']);
    $block_id .= '-' . drupal_strtolower(str_replace('_', '-', $form['delta']['#value']));
    $default_value = variable_get('collapsiblock_default_state', 1);
    if (isset($settings[$block_id])) {
      $default_value = $settings[$block_id];
    }
    $form['settings']['collapsiblock']['collapse_type'] = array(
      '#type' => 'radios',
      '#title' => t('Block collapse behavior'),
      '#options' => array(
        1 => t('None.'),
        2 => t('Collapsible, expanded by default.'),
        3 => t('Collapsible, collapsed by default.'),
        4 => t('Collapsible, collapsed all the time.'),
      ),
      '#default_value' => $default_value,
    );
  }
}

/**
 * Form submission handler for block_admin_configure().
 *
 * @see block_admin_configure()
 * @see collapsiblock_form_alter()
 */
function collapsiblock_submit($form, &$form_state) {
  $settings = variable_get('collapsiblock_settings', array());
  $settings['block-' . str_replace('_', '-', $form_state['values']['module']) . '-' . drupal_strtolower(str_replace('_', '-', $form_state['values']['delta']))] = $form_state['values']['collapse_type'];
  variable_set('collapsiblock_settings', $settings);
}

Functions

Namesort descending Description
collapsiblock_admin_settings Admin settings.
collapsiblock_default_settings Default theme's settings.
collapsiblock_form_alter Implements hook_form_alter().
collapsiblock_init Implements hook_init().
collapsiblock_menu Implements hook_menu().
collapsiblock_submit Form submission handler for block_admin_configure().