You are here

block_token.module in Block Token 7

Same filename and directory in other branches
  1. 8 block_token.module

The file contains Block Token module code.

File

block_token.module
View source
<?php

/**
 * @file
 * The file contains Block Token module code.
 */

/**
 * Implements hook_help().
 */
function block_token_help($path, $arg) {
  switch ($path) {
    case 'admin/help#block_token':
      $filepath = dirname(__FILE__) . '/README.txt';
      $readme = file_get_contents($filepath);
      if (!isset($readme)) {
        return NULL;
      }
      if (module_exists('markdown')) {
        $filters = module_invoke('markdown', 'filter_info');
        $info = $filters['filter_markdown'];
        if (function_exists($info['process callback'])) {
          $output = $info['process callback']($readme, NULL);
        }
        else {
          $output = '<pre>' . $readme . '</pre>';
        }
      }
      else {
        $output = '<pre>' . $readme . '</pre>';
      }
      return $output;
  }
}
define('BLOCK_TOKEN_SEPARATOR', ':');

/**
 * Creates the token string.
 *
 * @param string $module
 *   Module name.
 * @param string $delta
 *   Block delta.
 *
 * @return string
 *   Token name.
 */
function block_token_token_name($module, $delta) {

  // @todo use machine_name if available
  return $module . BLOCK_TOKEN_SEPARATOR . $delta;
}

/**
 * Renders the block.
 *
 * @param string $module
 *   Module name.
 * @param string $delta
 *   Block delta.
 *
 * @return string
 *   Rendered block.
 */
function block_token_block_render($module, $delta) {
  $block = block_load($module, $delta);
  foreach (array(
    'region',
    'title',
  ) as $key) {
    $block->{$key} = '';
  }
  $block = _block_render_blocks(array(
    $block,
  ));
  $block = _block_get_renderable_array($block);
  return drupal_render($block);
}

/**
 * Blocks with tokens.
 *
 * @param string $token
 *   Token name.
 *
 * @return array|bool
 *   If token name provided boolean indicating whether it is on or not
 *   otherwise the list of blocks with tokens.
 */
function block_token_blocks($token = NULL) {
  static $block_token;
  if (is_null($block_token)) {
    $block_token = array();
    $blocks = db_select('block', 'b')
      ->fields('b', array(
      'module',
      'delta',
    ))
      ->condition('block_token', 1)
      ->distinct()
      ->execute();
    foreach ($blocks as $block) {
      $block_token[block_token_token_name($block->module, $block->delta)] = $block;
    }
  }
  if (!is_null($token)) {
    return !empty($block_token[$token]);
  }
  return $block_token;
}

/**
 * Implements hook_token_info().
 */
function block_token_token_info() {
  $tokens = array();
  $blocks = block_token_blocks();
  foreach ($blocks as $token => $block) {
    $name = t('Block from module %module with delta %delta', array(
      '%module' => $block->module,
      '%delta' => $block->delta,
    ));
    $tokens[$token] = array(
      'name' => $name,
      'description' => $name,
    );
  }
  return array(
    'types' => array(
      'block_token' => array(
        'name' => t("Block Token"),
        'description' => t("Tokens containing blocks."),
      ),
    ),
    'tokens' => array(
      'block_token' => $tokens,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function block_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ('block_token' == $type) {
    foreach ($tokens as $token => $original) {
      $replacement = '';
      if (block_token_blocks($token)) {
        list($module, $delta) = explode(BLOCK_TOKEN_SEPARATOR, $token, 2);
        $replacement = block_token_block_render($module, $delta);
      }
      $replacements[$original] = $replacement;
    }
  }
  return $replacements;
}

/**
 * Implements hook_permission().
 */
function block_token_permission() {
  return array(
    'administer block token' => array(
      'title' => t('Administer block tokens'),
      'description' => t('Turn on/off the block token generation per block.'),
    ),
  );
}

/**
 * Implements hook_form_alter().
 *
 * Alter block edit form to add configuration field.
 */
function block_token_form_alter(&$form, &$form_state, $form_id) {
  if (user_access('administer block token') && ('block_admin_configure' == $form_id || 'block_add_block_form' == $form_id)) {
    $block = block_load($form['module']['#value'], $form['delta']['#value']);
    $form['settings']['block_token'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create the token for this block'),
      '#description' => t('Token string is not available until the block is saved.'),
      '#default_value' => isset($block->block_token) ? $block->block_token : '',
    );
    if (!is_null($block->delta)) {
      $token = block_token_token_name($block->module, $block->delta);
      $form['settings']['block_token']['#description'] = t('Token will be @token', array(
        '@token' => sprintf('[block_token:%s]', $token),
      ));
    }
    $form['#submit'][] = 'block_token_form_submit';
  }
}

/**
 * Submit callback.
 */
function block_token_form_submit($form, $form_state) {
  $values = $form_state['values'];
  if (isset($values['block_token'])) {

    // Execute the query only when value changes.
    if ($form['settings']['block_token']['#default_value'] != $values['block_token']) {
      db_update('block')
        ->fields(array(
        'block_token' => $values['block_token'],
      ))
        ->condition('module', $values['module'])
        ->condition('delta', $values['delta'])
        ->execute();

      // Flush all filter module cache to use the updated block_token values.
      // @todo flush token caches that prevent new token from displaying.
      cache_clear_all('*', 'cache_filter', TRUE);
    }
  }
}

Functions

Namesort descending Description
block_token_blocks Blocks with tokens.
block_token_block_render Renders the block.
block_token_form_alter Implements hook_form_alter().
block_token_form_submit Submit callback.
block_token_help Implements hook_help().
block_token_permission Implements hook_permission().
block_token_tokens Implements hook_tokens().
block_token_token_info Implements hook_token_info().
block_token_token_name Creates the token string.

Constants