You are here

copyright_block.module in Copyright Block module 8

File

copyright_block.module
View source
<?php

use Drupal\Core\Render\BubbleableMetadata;

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

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

Functions