You are here

copyright_block.module in Copyright Block module 6

Creates a dynamic customisable copyright message block.

File

copyright_block.module
View source
<?php

/**
 * @file
 * Creates a dynamic customisable copyright message block.
 */

/**
 * Implementation of hook_block().
 */
function copyright_block_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0] = array(
      'info' => t('Copyright statement'),
      'cache' => DRUPAL_CACHE_GLOBAL,
    );
    return $blocks;
  }
  elseif ($op == 'configure' && $delta == 0) {
    $form['start_year'] = array(
      '#title' => t('Start year'),
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => variable_get('copyright_block_start_year', date('Y')),
      '#element_validate' => array(
        'copyright_block_validate_start_year',
      ),
    );
    $form['text'] = array(
      '#title' => t('Copyright message'),
      '#type' => 'textarea',
      '#required' => TRUE,
      '#default_value' => variable_get('copyright_block_message', t('Copyright &copy; [site-name], [copyright-block-dates].')),
    );
    $form['view']['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['view']['token_help']['help'] = array(
      '#value' => theme('token_help', 'global'),
    );
    return $form;
  }
  elseif ($op == 'save' && $delta == 0) {
    variable_set('copyright_block_start_year', filter_xss($edit['start_year']));
    variable_set('copyright_block_message', filter_xss($edit['text']));
  }
  elseif ($op == 'view') {
    $block = array();
    switch ($delta) {
      case 0:
        $block['subject'] = t('Copyright statement');
        $block['content'] = copyright_block_block_1_content();
        break;
    }
    return $block;
  }
}

/**
 * Validates the start year.
 */
function copyright_block_validate_start_year($element, &$form_state) {
  $value = $element['#value'];
  $current_year = date('Y');
  if (!is_numeric($value)) {
    form_error($element, t('The start year must be numeric.'));
  }
  elseif (drupal_strlen($value) < 4) {
    form_error($element, t('The start year is too short.'));
  }
  elseif (drupal_strlen($value) > 4) {
    form_error($element, t('The start year is too long.'));
  }
  elseif ($value > $current_year) {
    form_error($element, t('The start year cannot be in the future.'));
  }
}

/**
 * Displays the copyright statement.
 */
function copyright_block_block_1_content() {
  return token_replace(variable_get('copyright_block_message', t('Copyright &copy; [copyright-block-dates], [site-name].')));
}

/**
 * Implementation of hook_token_values().
 */
function copyright_block_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  if ($type == 'global') {
    $start_year = variable_get('copyright_block_start_year', date('Y'));
    $current_year = date('Y');
    $values['copyright-block-dates'] = $current_year > $start_year ? $start_year . '-' . $current_year : $start_year;
  }
  return $values;
}

Functions

Namesort descending Description
copyright_block_block Implementation of hook_block().
copyright_block_block_1_content Displays the copyright statement.
copyright_block_token_values Implementation of hook_token_values().
copyright_block_validate_start_year Validates the start year.