You are here

collapsiblock.module in Collapsiblock 7.2

Make blocks collapsible.

File

collapsiblock.module
View source
<?php

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

// Scalar (JSON) constant for php 5.*
define('COLLAPSIBLOCK_ACTION_OPTIONS', serialize(array(
  1 => t('None.'),
  2 => t('Collapsible, expanded by default.'),
  3 => t('Collapsible, collapsed by default.'),
  5 => t('Collapsible, expanded all the time.'),
  4 => t('Collapsible, collapsed all the time.'),
)));

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

  //load the settings
  drupal_add_library('system', 'jquery.cookie');
  drupal_add_js(array(
    'collapsiblock' => array(
      'active_pages' => variable_get('collapsiblock_active_pages', FALSE),
      'slide_type' => variable_get('collapsiblock_slide_type', 1),
      'slide_speed' => variable_get('collapsiblock_slide_speed', 200),
    ),
  ), 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' => unserialize(COLLAPSIBLOCK_ACTION_OPTIONS),
    '#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(
      '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 = system_settings_form($form);
  return $form;
}

/**
 * Implements hook_form_alter().
 */
function collapsiblock_form_block_admin_configure_alter(&$form, $form_state, $form_id) {

  // Load statically cached block object used to display the form.
  $block = block_load($form['module']['#value'], $form['delta']['#value']);

  //$settings = variable_get('collapsiblock_settings', array());
  $form['#submit'][] = 'collapsiblock_submit';
  $form['collapsiblock'] = array(
    '#type' => 'fieldset',
    '#title' => t('Collapsible'),
    '#collapsible' => TRUE,
    '#weight' => -5,
  );

  // Add the global default to the list of options for the per-block setting.
  $options = unserialize(COLLAPSIBLOCK_ACTION_OPTIONS);
  $default = variable_get('collapsiblock_default_state', 1);
  $default = $options[$default];
  $options = array(
    0 => 'Global default, currently set to: ' . $default,
  ) + $options;
  $form['collapsiblock']['collapsiblock_action'] = array(
    '#type' => 'radios',
    '#title' => t('Block collapse behavior'),
    '#options' => $options,
    '#default_value' => isset($block->collapsiblock_action) ? $block->collapsiblock_action : 0,
  );
}

/**
 * Form submission handler for block_admin_configure().
 *
 * @see block_admin_configure()
 * @see collapsiblock_form_alter()
 */
function collapsiblock_submit($form, &$form_state) {
  if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {

    // Only save if value has changed.
    if (isset($form_state['values']['collapsiblock_action']) && $form['collapsiblock']['collapsiblock_action']['#default_value'] != $form_state['values']['collapsiblock_action'] && user_access('administer blocks')) {
      db_update('block')
        ->fields(array(
        'collapsiblock_action' => $form_state['values']['collapsiblock_action'],
      ))
        ->condition('module', $form_state['values']['module'])
        ->condition('delta', $form_state['values']['delta'])
        ->execute();
    }
  }
}

/**
 * Implements hook_preprocess_block().
 *
 * Inject Collapsiblock values into blocks that have collapsing turned on.
 *
 * @param $variables
 */
function collapsiblock_preprocess_block(&$variables) {
  $block = $variables['block'];
  $action = $block->collapsiblock_action;
  if ($action == 0) {
    $action = variable_get('collapsiblock_default_state', 1);
  }
  if ($action != 1) {
    $id = 'collapsiblock-wrapper-' . $block->bid;
    $classes[] = 'collapsiblock';
    $variables['title_prefix']['collapsiblock'] = array(
      '#markup' => '<div id="' . $id . '" class="' . implode(' ', $classes) . '" data-collapsiblock-action="' . $action . '">',
    );

    // Make sure the div closes before the contextual links if they're already
    // in the title_suffix
    $closure = array(
      'collapsiblock' => array(
        '#markup' => '</div>',
      ),
    );
    $variables['title_suffix'] = $closure + $variables['title_suffix'];
  }
}