diff --git a/token.tokens.inc b/token.tokens.inc index 3dc2d3b..3ed9169 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -17,6 +17,23 @@ function token_token_info_alter(&$info) { 'description' => t('Tokens related to the current date and time.'), 'type' => 'date', ); + if (module_exists('block')) { + $info['types']['block'] = array( + 'name' => t('Blocks'), + 'description' => t('Use the blocks in your site as tokens.'), + ); + foreach (module_implements('block_info') as $module) { + $module_blocks = module_invoke($module, 'block_info'); + if ($module_blocks) { + foreach ($module_blocks as $delta => $block) { + $info['tokens']['block']["$module:$delta"] = array( + 'name' => $block['info'], + 'description' => '', + ); + } + } + } + } // Add a 'dynamic' key to any tokens that have chained but dynamic tokens. $info['tokens']['date']['custom']['dynamic'] = TRUE; @@ -913,6 +930,35 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar } } + if (module_exists('block') && $type == 'block') { + $blocks = array(); + // Build the list of block tokens to render. + foreach ($tokens as $name => $original) { + list($module, $delta) = explode(':', $name); + $blocks[] = block_load($module, $delta); + } + + // Render the blocks. + $blocks = _block_render_blocks($blocks); + + // Check through and put in the replacement values. + foreach ($tokens as $name => $original) { + list($module, $delta) = explode(':', $name); + if (isset($blocks["{$module}_{$delta}"])) { + $block = array(); + $block["{$module}_{$delta}"] = $blocks["{$module}_{$delta}"]; + // Get all that blocky goodness (such as titles and contextual links) for this particular block + $build = _block_get_renderable_array($block); + if (is_array($build)) { + $replacements[$original] = drupal_render($build); + } + elseif (isset($build['#markup'])) { + $replacements[$original] = $build['#markup']; + } + } + } + } + return $replacements; }