You are here

block_refresh.module in Block Refresh 8

File

block_refresh.module
View source
<?php

define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE);

// autorefresh disabled by default
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE);

// manual refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_INIT', FALSE);

// inital refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_PANELS', FALSE);

// panels support disabled by default
define('BLOCK_REFRESH_DEFAULT_ARGUMENTS', TRUE);

// send arguments by default
define('BLOCK_REFRESH_DEFAULT_BYPASS_PAGE_CACHE', FALSE);

// page cache bypass disabled by default
define('BLOCK_REFRESH_DEFAULT_BYPASS_EXTERNAL_CACHE', '');

// external cache bypass disabled by default
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120);

// default refreshes every two minutes, if enabled
define('BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION', 'access block refresh content');

/**
 * Implements hook_form_alter.
 */
function block_refresh_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'block_form') {
    $block_id = $form['id']['#default_value'];
    $form = $form + block_refresh_settings_form_elements($block_id);

    // add submit handler
  }
}

/**
 * Generates the form elements for block refresh settings.
 */
function block_refresh_settings_form_elements($block_id) {

  // @todo: variable_get is deprecated - load settings here

  //$settings = variable_get('block_refresh_settings', array());
  $settings = array();
  $form['block_refresh'] = array(
    '#type' => 'fieldset',
    '#title' => t('Block refresh settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -1,
  );
  $form['block_refresh']['block_refresh_auto'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed automatically'),
    '#description' => t('If checked, then the content of this block will be refresh automatically every x seconds defined below.'),
    '#default_value' => isset($settings[$block_id]['auto']) ? $settings[$block_id]['auto'] : BLOCK_REFRESH_DEFAULT_AUTOMATIC,
  );
  $form['block_refresh']['block_refresh_timer'] = array(
    '#type' => 'textfield',
    '#title' => t('Block refresh timer'),
    '#description' => t('If this block is set to be refreshed automatically (checkbox above is checked), enter the number of <strong>seconds</strong> between each refresh.'),
    '#default_value' => isset($settings[$block_id]['timer']) ? $settings[$block_id]['timer'] : BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER,
  );
  $form['block_refresh']['block_refresh_manual'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed manually'),
    '#description' => t('If checked, then the content of this block may be refreshed manually by the user, by clicking on a provided (themeable) button in the block\'s subject header.'),
    '#default_value' => isset($settings[$block_id]['manual']) ? $settings[$block_id]['manual'] : BLOCK_REFRESH_DEFAULT_MANUAL,
  );
  $form['block_refresh']['block_refresh_init'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed on page load'),
    '#description' => t('If checked, then the content of this block will be refreshed when the page loads.'),
    '#default_value' => isset($settings[$block_id]['init']) ? $settings[$block_id]['init'] : BLOCK_REFRESH_DEFAULT_INIT,
  );
  $form['block_refresh']['block_refresh_arguments'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send page path arguments to block'),
    '#description' => t('If checked, the block will receive the current page path when called. Uncheck this if the block does not need it for a potential performance boost.'),
    '#default_value' => isset($settings[$block_id]['arguments']) ? $settings[$block_id]['arguments'] : BLOCK_REFRESH_DEFAULT_ARGUMENTS,
  );
  $form['block_refresh']['block_refresh_panels'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be used in Panels'),
    '#description' => t('If checked, then the content of this block will be refreshed when used inside the Panels module.'),
    '#default_value' => isset($settings[$block_id]['panels']) ? $settings[$block_id]['panels'] : BLOCK_REFRESH_DEFAULT_PANELS,
  );
  $form['block_refresh']['block_refresh_bypass_page_cache'] = array(
    '#type' => 'checkbox',
    '#title' => t('Bypass Drupal page cache'),
    '#description' => t('If checked, then the refreshed content of this block will bypass drupal\'s page cache. If stale content is being served to you due to caching, try checking this box. Warning! this can have a performance impact.'),
    '#default_value' => isset($settings[$block_id]['bypass_page_cache']) ? $settings[$block_id]['bypass_page_cache'] : BLOCK_REFRESH_DEFAULT_BYPASS_PAGE_CACHE,
  );
  $form['block_refresh']['block_refresh_bypass_external_cache'] = array(
    '#type' => 'textfield',
    '#title' => t('External cache max age'),
    '#description' => t('If you wish to override the max age of refreshed data served from an external cache (eg Varnish), enter a value in seconds here. Leave blank to use your sitewide default value.'),
    '#default_value' => isset($settings[$block_id]['bypass_external_cache']) ? $settings[$block_id]['bypass_external_cache'] : BLOCK_REFRESH_DEFAULT_BYPASS_EXTERNAL_CACHE,
  );
  return $form;
}