View source
<?php
use Drupal\block\Entity\Block;
use Drupal\block\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
define('BLOCK_TOKEN_SEPARATOR', ':');
function block_token_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.block_token':
$filepath = dirname(__FILE__) . '/README.txt';
$readme = file_get_contents($filepath);
if (!isset($readme)) {
return NULL;
}
if (\Drupal::moduleHandler()
->moduleExists('markdown')) {
$filters = \Drupal::moduleHandler()
->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;
}
}
function block_token_block_render($bid) {
$block = Block::load($bid);
$block_content = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
return \Drupal::service('renderer')
->render($block_content);
}
function block_token_blocks($token = NULL) {
static $block_token;
if (is_null($block_token)) {
$block_token = array();
$blocks = \Drupal::database()
->select('config', 'b')
->fields('b', array(
'name',
'data',
))
->condition('name', \Drupal::database()
->escapeLike("block.block") . '%', 'LIKE')
->execute();
foreach ($blocks as $block) {
if (isset(unserialize($block->data)['third_party_settings']['block_token']['token_value'])) {
$bid = explode('.', $block->name)[2];
$block = Block::load($bid);
$module = end($block
->getDependencies()['module']);
$block_token[block_token_token_name($module, $bid)] = $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) {
$module = end($block
->getDependencies()['module']);
$name = t('Block from module %module with id %delta', array(
'%module' => $module,
'%delta' => $block
->getOriginalId(),
));
$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, $id) = explode(BLOCK_TOKEN_SEPARATOR, $token, 2);
$replacement = block_token_block_render($id);
}
$replacements[$original] = $replacement;
}
}
return $replacements;
}
function block_token_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (\Drupal::currentUser()
->hasPermission('administer block token')) {
$block = $form_state
->getFormObject()
->getEntity();
$form['third_party_settings']['#tree'] = TRUE;
$form['third_party_settings']['block_token']['token_value'] = 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' => $block
->getThirdPartySetting('block_token', 'token_value'),
);
$id = $form['id']['#default_value'];
$module = $form['settings']['provider']['#value'];
$token = block_token_token_name($module, $id);
if ($token) {
if (!$block
->getThirdPartySetting('block_token', 'token_value')) {
$form['third_party_settings']['block_token']['token_value']['#description'] = t('Token will be @token', array(
'@token' => sprintf('[block_token:%s]', $token),
));
}
else {
$form['third_party_settings']['block_token']['token_value']['#description'] = t('Token is @token', array(
'@token' => sprintf('[block_token:%s]', $token),
));
}
}
}
}
function block_token_token_name($module, $block_id) {
return $module . BLOCK_TOKEN_SEPARATOR . $block_id;
}
function block_token_route_access() {
if (Drupal::currentUser()
->hasPermission('administer taxonomy')) {
return AccessResult::allowed();
}
if (Drupal::currentUser()
->hasPermission('administer block token')) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}