You are here

copyright_block.module in Copyright Block module 7.2

Creates a dynamic copyright information block.

File

copyright_block.module
View source
<?php

/**
 * @file
 * Creates a dynamic copyright information block.
 */

/**
 * Implements hook_block_info().
 */
function copyright_block_block_info() {
  $blocks['copyright_block'] = array(
    'info' => t('Copyright statement'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function copyright_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'copyright_block':
      $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['separator'] = array(
        '#title' => t('Separator'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#default_value' => variable_get('copyright_block_separator', '-'),
      );
      $copyright_block_text = variable_get('copyright_block_text');
      $form['text'] = array(
        '#title' => t('Copyright statement text.'),
        '#type' => 'text_format',
        '#required' => TRUE,
        '#default_value' => $copyright_block_text['value'],
        '#format' => $copyright_block_text['format'],
      );
      $form['view']['token_help'] = array(
        '#title' => t('Replacement patterns'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['view']['token_help']['help'] = array(
        '#markup' => theme('token_tree', array(
          'token_types' => array(
            'node',
          ),
        )),
      );
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function copyright_block_block_save($delta = '', $edit = array()) {

  // Update the start year variable.
  variable_set('copyright_block_start_year', $edit['start_year']);
  variable_set('copyright_block_separator', $edit['separator']);
  variable_set('copyright_block_text', $edit['text']);
}

/**
 * Implements hook_block_view().
 */
function copyright_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'copyright_block':
      $block['subject'] = t('Copyright statement');
      $block['content'] = copyright_block_block_content();
      break;
  }
  return $block;
}

/**
 * Display the copyright message.
 */
function copyright_block_block_content() {
  $copyright_block_text = variable_get('copyright_block_text');
  return token_replace($copyright_block_text['value']);
}

/**
 * Validate the start year.
 */
function copyright_block_validate_start_year($element, &$form_state, $form) {
  $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.'));
  }
}

/**
 * Implements hook_token_values().
 */
function copyright_block_token_info() {
  $types['copyright_statement'] = array(
    'name' => t('Copyright statement'),
    'description' => t('Tokens for the copyright statement block'),
  );
  $copyright_statement['dates'] = array(
    'name' => t('Dates'),
    'description' => t('The start and end dates for the copyright statement.'),
  );
  return array(
    'types' => $types,
    'tokens' => array(
      'copyright_statement' => $copyright_statement,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function copyright_block_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'copyright_statement') {
    $separator = check_plain(variable_get('copyright_block_separator', '-'));
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'dates':
          $start_year = variable_get('copyright_block_start_year', date('Y'));
          $current_year = date('Y');
          $replacements[$original] = $start_year < $current_year ? $start_year . $separator . $current_year : $start_year;
          break;
      }
    }
  }
  return $replacements;
}