<?php
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\NestedArray;
const BLOCK_REGION_NONE = -1;
const BLOCK_CUSTOM_FIXED = 0;
const BLOCK_CUSTOM_ENABLED = 1;
const BLOCK_CUSTOM_DISABLED = 2;
const BLOCK_VISIBILITY_NOTLISTED = 0;
const BLOCK_VISIBILITY_LISTED = 1;
const BLOCK_VISIBILITY_PHP = 2;
const BLOCK_LABEL_VISIBLE = 'visible';
function block_help($path, $arg) {
switch ($path) {
case 'admin/help#block':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Block module allows you to create boxes of content, which are rendered into an area, or region, of one or more pages of a website. The core Seven administration theme, for example, implements the regions "Content" and "Help", and a block may appear in either of these regions. The <a href="@blocks">Blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. For more information, see the online handbook entry for <a href="@block">Block module</a>.', array(
'@block' => 'http://drupal.org/documentation/modules/block',
'@blocks' => url('admin/structure/block'),
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Positioning content') . '</dt>';
$output .= '<dd>' . t('When working with blocks, remember that all themes do <em>not</em> implement the same regions, or display regions in the same way. Blocks are positioned on a per-theme basis. Users with the <em>Administer blocks</em> permission can disable blocks. Disabled blocks are listed on the <a href="@blocks">Blocks administration page</a>, but are not displayed in any region.', array(
'@block' => 'http://drupal.org/documentation/modules/block',
'@blocks' => url('admin/structure/block'),
)) . '</dd>';
$output .= '<dt>' . t('Controlling visibility') . '</dt>';
$output .= '<dd>' . t('Blocks can be configured to be visible only on certain pages, only to users of certain roles, or only on pages displaying certain <a href="@content-type">content types</a>. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.', array(
'@content-type' => url('admin/structure/types'),
'@user' => url('user'),
)) . '</dd>';
if (module_exists('custom_block')) {
$output .= '<dt>' . t('Creating custom blocks') . '</dt>';
$output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can <a href="@block-add">add custom blocks</a>, which are then listed on the <a href="@blocks">Blocks administration page</a>. Once created, custom blocks behave just like default and module-generated blocks.', array(
'@blocks' => url('admin/structure/block'),
'@block-add' => url('admin/structure/block/list/block_plugin_ui:' . config('system.theme')
->get('default') . '/add/custom_blocks'),
)) . '</dd>';
}
$output .= '</dl>';
return $output;
}
if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list') && empty($arg[5])) {
if (!empty($arg[4])) {
list(, $demo_theme) = explode(':', $arg[4]);
}
else {
$demo_theme = config('system.theme')
->get('default');
}
$themes = list_themes();
$output = '<p>' . t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page. Click the <em>configure</em> link next to each block to configure its specific title and visibility settings.') . '</p>';
$output .= '<p>' . l(t('Demonstrate block regions (@theme)', array(
'@theme' => $themes[$demo_theme]->info['name'],
)), 'admin/structure/block/demo/' . $demo_theme) . '</p>';
return $output;
}
}
function block_theme() {
return array(
'block' => array(
'render element' => 'elements',
'template' => 'block',
),
);
}
function block_permission() {
return array(
'administer blocks' => array(
'title' => t('Administer blocks'),
),
);
}
function block_menu() {
$default_theme = config('system.theme')
->get('default');
$items['admin/structure/block'] = array(
'title' => 'Blocks',
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
'page callback' => 'block_admin_display',
'page arguments' => array(
$default_theme,
),
'access arguments' => array(
'administer blocks',
),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/add/%/%'] = array(
'title' => 'Configure block',
'page callback' => 'block_admin_add',
'page arguments' => array(
4,
5,
),
'access arguments' => array(
'administer blocks',
),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/manage/%block'] = array(
'title' => 'Configure block',
'page callback' => 'block_admin_edit',
'page arguments' => array(
4,
),
'access arguments' => array(
'administer blocks',
),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/manage/%block/configure'] = array(
'title' => 'Configure block',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
$items['admin/structure/block/manage/%block/delete'] = array(
'title' => 'Delete block',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_NONE,
'route_name' => 'block_admin_block_delete',
);
$themes = list_themes();
foreach (drupal_container()
->get('plugin.manager.system.plugin_ui')
->getDefinitions() as $plugin_id => $plugin) {
list($plugin_base, $key) = explode(':', $plugin_id);
if ($plugin_base == 'block_plugin_ui') {
$theme = $themes[$key];
$items['admin/structure/block/list/' . $plugin_id] = array(
'title' => check_plain($theme->info['name']),
'page arguments' => array(
$key,
),
'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
'access callback' => '_block_themes_access',
'access arguments' => array(
$key,
),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/demo/' . $key] = array(
'title' => check_plain($theme->info['name']),
'page callback' => 'block_admin_demo',
'page arguments' => array(
$key,
),
'type' => MENU_CALLBACK,
'access callback' => '_block_themes_access',
'access arguments' => array(
$key,
),
'theme callback' => '_block_custom_theme',
'theme arguments' => array(
$key,
),
'file' => 'block.admin.inc',
);
}
}
return $items;
}
function _block_themes_access($theme) {
return user_access('administer blocks') && drupal_theme_access($theme);
}
function _block_custom_theme($theme = NULL) {
return $theme;
}
function block_page_build(&$page) {
global $theme;
drupal_theme_initialize();
$all_regions = system_region_list($theme);
$item = menu_get_item();
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
foreach (array_keys($all_regions) as $region) {
if ($blocks = block_get_blocks_by_region($region)) {
$page[$region] = $blocks;
}
}
drupal_static_reset('block_list');
}
else {
$item = menu_get_item();
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
$visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
foreach ($visible_regions as $region) {
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
$page[$region]['block_description'] = array(
'#markup' => $description,
'#weight' => 15,
);
}
$page['page_top']['backlink'] = array(
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
'#href' => 'admin/structure/block' . (config('system.theme')
->get('default') == $theme ? '' : '/list/' . $theme),
'#options' => array(
'attributes' => array(
'class' => array(
'block-demo-backlink',
'overlay-restore',
),
),
),
'#weight' => -10,
);
}
}
}
function block_get_blocks_by_region($region) {
$build = array();
if ($list = block_list($region)) {
$build = _block_get_renderable_region($list);
}
return $build;
}
function _block_get_renderable_region($list = array()) {
$build = array();
$not_cacheable = $GLOBALS['user']->uid == 1 || count(module_implements('node_grants')) || !\Drupal::request()
->isMethodSafe();
foreach ($list as $key => $block) {
$settings = $block
->get('settings');
if ($not_cacheable || in_array($settings['cache'], array(
DRUPAL_NO_CACHE,
DRUPAL_CACHE_CUSTOM,
))) {
if ($block
->access()) {
$build[$key] = entity_view($block, 'block');
}
}
else {
$key_components = explode('.', $key);
$id = array_pop($key_components);
$build[$key] = array(
'#block' => $block,
'#weight' => $block
->get('weight'),
'#pre_render' => array(
'_block_get_renderable_block',
),
'#cache' => array(
'keys' => array(
$id,
$settings['module'],
),
'granularity' => $settings['cache'],
'bin' => 'block',
'tags' => array(
'content' => TRUE,
),
),
);
}
if (isset($build[$key]) && !in_array($block
->get('plugin'), array(
'system_help_block',
'system_main_block',
))) {
$build[$key]['#contextual_links']['block'] = array(
'admin/structure/block/manage',
array(
$key,
),
);
if (isset($build[$key]['content']['#contextual_links'])) {
$build[$key]['#contextual_links'] += $build[$key]['content']['#contextual_links'];
unset($build[$key]['content']['#contextual_links']);
}
}
}
return $build;
}
function _block_rehash($theme = NULL) {
$theme = $theme ? $theme : config('system.theme')
->get('default');
$regions = system_region_list($theme);
$blocks = entity_load_multiple_by_properties('block', array(
'theme' => $theme,
));
foreach ($blocks as $block_id => $block) {
$region = $block
->get('region');
$status = $block
->status();
if (!empty($region) && $region != BLOCK_REGION_NONE && !isset($regions[$region]) && $status) {
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array(
'%info' => $block_id,
'%region' => $region,
)), 'warning');
$block
->disable()
->save();
}
if (!$status) {
$block
->set('region', BLOCK_REGION_NONE);
$block
->save();
}
}
return $blocks;
}
function block_themes_enabled($theme_list) {
foreach ($theme_list as $theme) {
block_theme_initialize($theme);
}
}
function block_theme_initialize($theme) {
$has_blocks = entity_load_multiple_by_properties('block', array(
'theme' => $theme,
));
if (!$has_blocks) {
$default_theme = config('system.theme')
->get('default');
$regions = system_region_list($theme, REGIONS_VISIBLE);
$default_theme_blocks = entity_load_multiple_by_properties('block', array(
'theme' => $default_theme,
));
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
list(, $machine_name) = explode('.', $default_theme_block_id);
$block = $default_theme_block
->createDuplicate();
$block
->set('id', $theme . '.' . $machine_name);
if (!isset($regions[$block
->get('region')])) {
$block
->set('region', system_default_region($theme));
}
$block
->save();
}
}
}
function block_list($region) {
$blocks =& drupal_static(__FUNCTION__);
if (!isset($blocks)) {
global $theme;
$blocks = array();
foreach (entity_load_multiple_by_properties('block', array(
'theme' => $theme,
)) as $block_id => $block) {
$blocks[$block
->get('region')][$block_id] = $block;
}
}
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
return $blocks[$region];
}
function block_load($entity_id) {
return entity_load('block', $entity_id);
}
function _block_get_renderable_block($element) {
$block = $element['#block'];
if ($element['#access'] = $block
->access()) {
$element += entity_view($block, 'block');
}
return $element;
}
function block_rebuild() {
foreach (list_themes() as $name => $data) {
if ($data->status) {
_block_rehash($name);
}
}
}
function template_preprocess_block(&$variables) {
$block_counter =& drupal_static(__FUNCTION__, array());
$variables['configuration'] = $variables['elements']['#configuration'];
$variables['plugin_id'] = $variables['elements']['#plugin_id'];
$variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
$variables['content'] = $variables['elements']['content'];
$variables['attributes']['class'][] = 'block';
$variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['module']);
if (isset($variables['content']['#attributes'])) {
$variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']);
unset($variables['content']['#attributes']);
}
$variables['content_attributes']['class'][] = 'content';
$variables['theme_hook_suggestions'][] = 'block__' . $variables['configuration']['module'];
$parts = explode(':', $variables['plugin_id']);
$suggestion = 'block';
while ($part = array_shift($parts)) {
$variables['theme_hook_suggestions'][] = $suggestion .= '__' . strtr($part, '-', '_');
}
if ($id = $variables['elements']['#block']
->id()) {
$config_id = explode('.', $id);
$machine_name = array_pop($config_id);
$variables['attributes']['id'] = drupal_html_id('block-' . $machine_name);
$variables['theme_hook_suggestions'][] = 'block__' . $machine_name;
}
}
function block_user_role_delete($role) {
foreach (entity_load_multiple('block') as $block_id => $block) {
$visibility = $block
->get('visibility');
if (isset($visibility['roles']['roles'][$role
->id()])) {
unset($visibility['roles']['roles'][$role
->id()]);
$block
->set('visibility', $visibility);
$block
->save();
}
}
}
function block_menu_delete($menu) {
foreach (entity_load_multiple('block') as $block_id => $block) {
if ($block
->get('plugin') == 'menu_menu_block:' . $menu
->id()) {
$block
->delete();
}
}
}
function block_admin_paths() {
$paths = array(
'admin/structure/block/demo/*' => FALSE,
);
return $paths;
}
function block_language_delete($language) {
foreach (entity_load_multiple('block') as $block_id => $block) {
$visibility = $block
->get('visibility');
if (isset($visibility['language']['langcodes'][$language->langcode])) {
unset($visibility['language']['langcodes'][$language->langcode]);
$block
->set('visibility', $visibility);
$block
->save();
}
}
}
function block_library_info() {
$libraries['drupal.block'] = array(
'title' => 'Block',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'block') . '/block.js' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
),
);
return $libraries;
}