View source
<?php
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', ':');
function block_token_token_name($module, $delta) {
return $module . BLOCK_TOKEN_SEPARATOR . $delta;
}
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);
}
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;
}
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,
),
);
}
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;
}
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.'),
),
);
}
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';
}
}
function block_token_form_submit($form, $form_state) {
$values = $form_state['values'];
if (isset($values['block_token'])) {
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();
cache_clear_all('*', 'cache_filter', TRUE);
}
}
}