2001-03-10 11:07:52 +00:00
<?php
2000-12-23 15:20:10 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
2009-08-28 19:44:05 +00:00
* Controls the visual building blocks a page is constructed with.
2004-08-21 06:42:38 +00:00
*/
2013-01-08 21:01:41 +00:00
use Drupal\Component\Plugin\Exception\PluginException;
2013-06-12 15:57:44 +00:00
use Drupal\Component\Utility\NestedArray;
2013-01-08 21:01:41 +00:00
2008-05-26 17:12:55 +00:00
/**
* Denotes that a block is not enabled in any region and should not be shown.
*/
2011-11-29 09:56:53 +00:00
const BLOCK_REGION_NONE = -1;
2006-11-08 19:27:59 +00:00
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block on every page except the listed pages.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_NOTLISTED = 0;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block on only the listed pages.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_LISTED = 1;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block if the associated PHP code returns TRUE.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_PHP = 2;
2010-06-25 20:25:54 +00:00
2013-03-25 18:00:41 +00:00
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-05-11 20:10:14 +00:00
*/
2007-06-30 19:46:58 +00:00
function block_help($path, $arg) {
switch ($path) {
2003-10-09 18:53:22 +00:00
case 'admin/help#block':
2009-11-19 05:54:42 +00:00
$output = '';
2009-11-22 21:24:37 +00:00
$output .= '<h3>' . t('About') . '</h3>';
2012-07-22 04:23:27 +00:00
$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>';
2009-11-19 05:54:42 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Positioning content') . '</dt>';
2012-03-21 05:51:30 +00:00
$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>';
2009-11-19 05:54:42 +00:00
$output .= '<dt>' . t('Controlling visibility') . '</dt>';
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
$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>';
2013-08-15 15:28:57 +00:00
$output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can add custom blocks, 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'))) . '</dd>';
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2009-11-19 05:54:42 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2003-08-05 18:33:39 +00:00
}
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list') && empty($arg[5])) {
if (!empty($arg[4])) {
2013-08-07 19:49:26 +00:00
$demo_theme = $arg[4];
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
else {
2013-09-16 03:58:06 +00:00
$demo_theme = \Drupal::config('system.theme')->get('default');
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2009-10-14 02:13:15 +00:00
$themes = list_themes();
2013-08-15 15:28:57 +00:00
$output = '<p>' . t('This page provides a drag-and-drop interface for adding a block to a region, and for controlling the order of blocks within regions. To add a block to a region, or to configure its specific title and visibility settings, click the block title under <em>Place blocks</em>. 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.') . '</p>';
2009-10-14 02:13:15 +00:00
$output .= '<p>' . l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), 'admin/structure/block/demo/' . $demo_theme) . '</p>';
return $output;
}
2001-01-13 16:33:19 +00:00
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function block_theme() {
return array(
2009-04-26 01:15:04 +00:00
'block' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2009-04-26 01:15:04 +00:00
'template' => 'block',
2009-05-24 17:39:35 +00:00
),
2013-09-04 20:42:50 +00:00
'block_list' => array(
'render element' => 'form',
'template' => 'block-list',
),
2007-04-06 13:27:23 +00:00
);
}
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2004-05-11 20:10:14 +00:00
*/
2009-07-05 18:00:11 +00:00
function block_permission() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer blocks' => array(
'title' => t('Administer blocks'),
),
2008-02-20 13:46:43 +00:00
);
2001-06-29 22:08:57 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
*
* @todo Clarify the documentation for the per-plugin block admin links.
2004-04-21 13:56:38 +00:00
*/
2007-01-24 14:48:36 +00:00
function block_menu() {
2013-09-16 03:58:06 +00:00
$default_theme = \Drupal::config('system.theme')->get('default');
2009-07-20 18:51:36 +00:00
$items['admin/structure/block'] = array(
2013-09-06 17:25:30 +00:00
'title' => 'Block layout',
2007-04-30 17:03:29 +00:00
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
2013-09-15 19:59:49 +00:00
'route_name' => 'block.admin_display',
2007-01-24 14:48:36 +00:00
);
2013-09-06 17:25:30 +00:00
$items['admin/structure/block/list'] = array(
'title' => 'Block layout',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
2013-01-17 04:03:30 +00:00
$items['admin/structure/block/manage/%block'] = array(
'title' => 'Configure block',
2013-09-15 19:59:49 +00:00
'route_name' => 'block.admin_edit',
2007-01-24 14:48:36 +00:00
);
2013-01-17 04:03:30 +00:00
$items['admin/structure/block/manage/%block/configure'] = array(
2009-10-17 05:50:29 +00:00
'title' => 'Configure block',
'type' => MENU_DEFAULT_LOCAL_TASK,
2013-09-18 13:16:27 +00:00
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
2009-10-17 05:50:29 +00:00
);
2013-08-10 06:52:52 +00:00
$items['admin/structure/block/add/%/%'] = array(
'title' => 'Place block',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
2013-09-15 19:59:49 +00:00
'route_name' => 'block.admin_add',
2013-08-10 06:52:52 +00:00
);
2013-02-28 11:35:27 +00:00
// Block administration is tied to the theme and plugin definition so
// that the plugin can appropriately attach to this URL structure.
// @todo D8: Use dynamic % arguments instead of static, hard-coded theme names
// and plugin IDs to decouple the routes from these dependencies and allow
// hook_menu_local_tasks() to check for the untranslated tab_parent path.
// @see http://drupal.org/node/1067408
2013-08-07 19:49:26 +00:00
foreach (list_themes() as $key => $theme) {
$items["admin/structure/block/list/$key"] = array(
'title' => check_plain($theme->info['name']),
'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
2013-09-15 19:59:49 +00:00
'route_name' => "block.admin_display_$key",
2013-08-07 19:49:26 +00:00
);
$items["admin/structure/block/demo/$key"] = array(
'title' => check_plain($theme->info['name']),
2013-09-18 18:30:30 +00:00
'route_name' => 'block.admin_demo',
2013-08-07 19:49:26 +00:00
'type' => MENU_CALLBACK,
'theme callback' => '_block_custom_theme',
'theme arguments' => array($key),
);
2004-09-16 07:17:56 +00:00
}
2004-06-18 15:04:37 +00:00
return $items;
2001-06-20 20:00:40 +00:00
}
2007-12-19 19:09:52 +00:00
/**
2012-08-12 18:48:54 +00:00
* Access callback: Only enabled themes can be accessed.
2011-11-03 10:53:06 +00:00
*
* Path:
* - admin/structure/block/list/% (for each theme)
* - admin/structure/block/demo/% (for each theme)
*
* @param $theme
* Either the name of a theme or a full theme object.
*
* @see block_menu()
2007-12-19 19:09:52 +00:00
*/
function _block_themes_access($theme) {
2009-09-30 13:09:30 +00:00
return user_access('administer blocks') && drupal_theme_access($theme);
}
/**
2011-11-03 10:53:06 +00:00
* Theme callback: Uses the theme specified in the parameter.
*
2009-09-30 13:09:30 +00:00
* @param $theme
* The theme whose blocks are being configured. If not set, the default theme
* is assumed.
2011-11-03 10:53:06 +00:00
*
2009-09-30 13:09:30 +00:00
* @return
* The theme that should be used for the block configuration page, or NULL
* to indicate that the default theme should be used.
2011-11-03 10:53:06 +00:00
*
* @see block_menu()
2009-09-30 13:09:30 +00:00
*/
function _block_custom_theme($theme = NULL) {
// We return exactly what was passed in, to guarantee that the page will
// always be displayed using the theme whose blocks are being configured.
return $theme;
2007-12-19 19:09:52 +00:00
}
2009-01-27 00:22:27 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_page_build().
2009-01-27 00:22:27 +00:00
*
2011-11-03 10:53:06 +00:00
* Renders blocks into their regions.
2009-01-27 00:22:27 +00:00
*/
2009-08-31 16:46:32 +00:00
function block_page_build(&$page) {
2009-01-27 00:22:27 +00:00
global $theme;
// The theme system might not yet be initialized. We need $theme.
2009-07-14 10:22:17 +00:00
drupal_theme_initialize();
2009-01-27 00:22:27 +00:00
2011-03-28 00:28:24 +00:00
// Fetch a list of regions for the current theme.
2009-07-18 02:36:01 +00:00
$all_regions = system_region_list($theme);
2009-01-27 00:22:27 +00:00
2009-10-14 02:13:15 +00:00
$item = menu_get_item();
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
// Load all region content assigned via blocks.
foreach (array_keys($all_regions) as $region) {
// Assign blocks to region.
if ($blocks = block_get_blocks_by_region($region)) {
$page[$region] = $blocks;
}
2009-08-26 10:53:45 +00:00
}
2010-06-12 08:42:46 +00:00
// Once we've finished attaching all blocks to the page, clear the static
// cache to allow modules to alter the block list differently in different
// contexts. For example, any code that triggers hook_page_build() more
// than once in the same page request may need to alter the block list
// differently each time, so that only certain parts of the page are
// actually built. We do not clear the cache any earlier than this, though,
// because it is used each time block_get_blocks_by_region() gets called
// above.
drupal_static_reset('block_list');
2009-10-14 02:13:15 +00:00
}
else {
// Append region description if we are rendering the regions demo page.
2013-08-10 09:03:40 +00:00
$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,
#655416 by casey, yoroy, BTMash, Bojhan, Gábor Hojtsy, sun, ksenzee, oseldman, jrguitar21, et al: 'Demonstrate block preview' should not open in the overlay, and should reflect the regions in the front-end theme.
2010-07-31 12:54:59 +00:00
);
2009-01-27 00:22:27 +00:00
}
2013-08-10 09:03:40 +00:00
$page['page_top']['backlink'] = array(
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
2013-09-16 03:58:06 +00:00
'#href' => 'admin/structure/block' . (\Drupal::config('system.theme')->get('default') == $theme ? '' : '/list/' . $theme),
2013-08-10 09:03:40 +00:00
// Add the "overlay-restore" class to indicate this link should restore
// the context in which the region demonstration page was opened.
'#options' => array('attributes' => array('class' => array('block-demo-backlink', 'overlay-restore'))),
'#weight' => -10,
);
2009-01-27 00:22:27 +00:00
}
}
/**
2011-11-03 10:53:06 +00:00
* Gets a renderable array of a region containing all enabled blocks.
2009-01-27 00:22:27 +00:00
*
* @param $region
* The requested region.
2011-11-03 10:53:06 +00:00
*
* @return
* A renderable array of a region containing all enabled blocks.
2009-01-27 00:22:27 +00:00
*/
function block_get_blocks_by_region($region) {
$build = array();
if ($list = block_list($region)) {
2011-09-29 03:29:59 +00:00
$build = _block_get_renderable_region($list);
2009-10-13 13:54:55 +00:00
}
return $build;
}
/**
2011-11-03 10:53:06 +00:00
* Gets an array of blocks suitable for drupal_render().
2009-10-13 13:54:55 +00:00
*
* @param $list
* A list of blocks such as that returned by block_list().
2011-11-03 10:53:06 +00:00
*
2009-10-13 13:54:55 +00:00
* @return
* A renderable array.
*/
2011-09-29 03:29:59 +00:00
function _block_get_renderable_region($list = array()) {
2009-10-13 13:54:55 +00:00
$build = array();
2012-02-29 16:49:38 +00:00
// Block caching is not compatible with node_access modules. We also
// preserve the submission of forms in blocks, by fetching from cache
// only if the request method is 'GET' (or 'HEAD'). User 1 being out of
// the regular 'roles define permissions' schema, it brings too many
// chances of having unwanted output get in the cache and later be served
// to other users. We therefore exclude user 1 from block caching.
2013-09-16 03:58:06 +00:00
$not_cacheable = \Drupal::currentUser()->id() == 1 ||
count(\Drupal::moduleHandler()->getImplementations('node_grants')) ||
!\Drupal::request()->isMethodSafe();
2012-02-29 16:49:38 +00:00
2009-10-13 13:54:55 +00:00
foreach ($list as $key => $block) {
2013-01-17 04:03:30 +00:00
$settings = $block->get('settings');
if ($not_cacheable || in_array($settings['cache'], array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) {
// Non-cached blocks get built immediately.
if ($block->access()) {
$build[$key] = entity_view($block, 'block');
}
2011-09-29 03:29:59 +00:00
}
else {
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
$key_components = explode('.', $key);
$id = array_pop($key_components);
2013-01-17 04:03:30 +00:00
$build[$key] = array(
'#block' => $block,
'#weight' => $block->get('weight'),
2011-09-29 03:29:59 +00:00
'#pre_render' => array('_block_get_renderable_block'),
'#cache' => array(
2013-05-11 10:01:25 +00:00
'keys' => array($id, $settings['module']),
2013-01-17 04:03:30 +00:00
'granularity' => $settings['cache'],
2011-09-29 03:29:59 +00:00
'bin' => 'block',
2012-06-13 01:37:07 +00:00
'tags' => array('content' => TRUE),
2011-09-29 03:29:59 +00:00
),
);
}
2009-10-17 05:50:29 +00:00
2010-09-12 00:20:36 +00:00
// Add contextual links for this block; skip the main content block, since
// contextual links are basically output as tabs/local tasks already. Also
// skip the help block, since we assume that most users do not need or want
// to perform contextual actions on the help block, and the links needlessly
// draw attention on it.
2013-04-12 06:57:55 +00:00
if (isset($build[$key]) && !in_array($block->get('plugin'), array('system_help_block', 'system_main_block'))) {
2013-01-17 04:03:30 +00:00
$build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($key));
2013-04-15 15:26:55 +00:00
// If there are any nested contextual links, move them to the top level.
if (isset($build[$key]['content']['#contextual_links'])) {
$build[$key]['#contextual_links'] += $build[$key]['content']['#contextual_links'];
unset($build[$key]['content']['#contextual_links']);
}
2009-10-17 05:50:29 +00:00
}
2009-01-27 00:22:27 +00:00
}
return $build;
}
2002-10-30 08:12:28 +00:00
/**
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
* Returns an array of block class instances by theme.
2002-11-01 10:47:20 +00:00
*
2011-05-24 01:07:02 +00:00
* @param $theme
2009-10-14 02:13:15 +00:00
* The theme to rehash blocks for. If not provided, defaults to the currently
* used theme.
2009-11-10 17:27:54 +00:00
*
2004-05-11 20:10:14 +00:00
* @return
2006-04-04 23:32:40 +00:00
* Blocks currently exported by modules.
2002-10-30 08:12:28 +00:00
*/
2009-10-14 02:13:15 +00:00
function _block_rehash($theme = NULL) {
2013-09-16 03:58:06 +00:00
$theme = $theme ? $theme : \Drupal::config('system.theme')->get('default');
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
$regions = system_region_list($theme);
2013-01-17 04:03:30 +00:00
$blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
foreach ($blocks as $block_id => $block) {
2013-09-20 10:21:32 +00:00
// Remove any invalid block from the list.
// @todo Remove this check as part of https://drupal.org/node/1776830.
if (!$block->getPlugin()) {
unset($blocks[$block_id]);
continue;
}
2013-01-17 04:03:30 +00:00
$region = $block->get('region');
2013-02-06 12:00:39 +00:00
$status = $block->status();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// Disable blocks in invalid regions.
2013-02-06 12:00:39 +00:00
if (!empty($region) && $region != BLOCK_REGION_NONE && !isset($regions[$region]) && $status) {
2013-01-17 04:03:30 +00:00
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');
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// Disabled modules are moved into the BLOCK_REGION_NONE later so no
// need to move the block to another region.
2013-02-06 12:00:39 +00:00
$block->disable()->save();
2010-06-15 16:19:28 +00:00
}
2013-02-06 12:00:39 +00:00
// Set region to none if not enabled.
if (!$status) {
2013-01-17 04:03:30 +00:00
$block->set('region', BLOCK_REGION_NONE);
$block->save();
2007-11-06 11:40:15 +00:00
}
2006-08-20 06:49:15 +00:00
}
2002-10-26 15:17:26 +00:00
return $blocks;
}
2001-01-26 13:38:46 +00:00
2009-04-21 09:31:31 +00:00
/**
2011-11-03 10:53:06 +00:00
* Initializes blocks for enabled themes.
*
* @param $theme_list
* An array of theme names.
2009-04-21 09:31:31 +00:00
*/
2009-12-01 00:39:35 +00:00
function block_themes_enabled($theme_list) {
foreach ($theme_list as $theme) {
block_theme_initialize($theme);
2009-04-21 09:31:31 +00:00
}
}
/**
2011-11-03 10:53:06 +00:00
* Assigns an initial, default set of blocks for a theme.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* This function is called the first time a new theme is enabled. The new theme
* gets a copy of the default theme's blocks, with the difference that if a
* particular region isn't available in the new theme, the block is assigned
* to the new theme's default region.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* @param $theme
* The name of a theme.
*/
2009-07-14 10:22:17 +00:00
function block_theme_initialize($theme) {
2009-04-21 09:31:31 +00:00
// Initialize theme's blocks if none already registered.
2013-01-17 04:03:30 +00:00
$has_blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
2009-05-16 15:23:16 +00:00
if (!$has_blocks) {
2013-09-16 03:58:06 +00:00
$default_theme = \Drupal::config('system.theme')->get('default');
2011-07-03 17:57:21 +00:00
// Apply only to new theme's visible regions.
$regions = system_region_list($theme, REGIONS_VISIBLE);
2013-01-17 04:03:30 +00:00
$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);
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
2013-01-17 04:03:30 +00:00
if (!isset($regions[$block->get('region')])) {
$block->set('region', system_default_region($theme));
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2013-01-17 04:03:30 +00:00
$block->save();
2009-04-21 09:31:31 +00:00
}
}
}
2005-08-16 18:06:18 +00:00
/**
2011-11-03 10:53:06 +00:00
* Returns all blocks in the specified region for the current user.
2005-08-16 18:06:18 +00:00
*
* @param $region
* The name of a region.
*
* @return
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
* An array of block objects, indexed with the configuration object name
* that represents the configuration. If you are displaying your blocks in
* one or two sidebars, you may check whether this array is empty to see
* how many columns are going to be displayed.
2005-08-16 18:06:18 +00:00
*/
function block_list($region) {
2009-11-11 08:52:53 +00:00
$blocks = &drupal_static(__FUNCTION__);
2003-11-19 16:13:07 +00:00
2009-11-11 08:52:53 +00:00
if (!isset($blocks)) {
2013-01-17 04:03:30 +00:00
global $theme;
$blocks = array();
foreach (entity_load_multiple_by_properties('block', array('theme' => $theme)) as $block_id => $block) {
2013-09-20 10:21:32 +00:00
// Onlye include valid blocks in the list.
// @todo Remove this check as part of https://drupal.org/node/1776830.
if ($block->getPlugin()) {
$blocks[$block->get('region')][$block_id] = $block;
}
2013-01-17 04:03:30 +00:00
}
2008-03-21 08:41:25 +00:00
}
2009-11-11 08:52:53 +00:00
// Create an empty array if there are no entries.
2008-03-21 08:41:25 +00:00
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
2013-08-03 09:47:26 +00:00
uasort($blocks[$region], function($first, $second) {
return $first->weight === $second->weight ? 0 : ($first->weight < $second->weight ? -1 : 1);
});
2008-03-21 08:41:25 +00:00
return $blocks[$region];
}
2009-10-16 23:48:38 +00:00
/**
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
* Loads a block instance.
2009-10-16 23:48:38 +00:00
*
2013-01-17 04:03:30 +00:00
* This should only be used when entity_load() cannot be used directly.
2009-10-16 23:48:38 +00:00
*
2013-01-17 04:03:30 +00:00
* @param string $entity_id
* The block ID.
2013-01-08 21:01:41 +00:00
*
2013-08-18 21:16:19 +00:00
* @return \Drupal\block\Entity\Block
2013-01-17 04:03:30 +00:00
* The loaded block object.
2009-10-16 23:48:38 +00:00
*/
2013-01-17 04:03:30 +00:00
function block_load($entity_id) {
return entity_load('block', $entity_id);
2009-06-08 09:23:55 +00:00
}
2008-03-21 08:41:25 +00:00
/**
2013-03-05 02:25:53 +00:00
* Builds the content and label for a block.
2011-11-03 10:53:06 +00:00
*
* For cacheable blocks, this is called during #pre_render.
2008-03-21 08:41:25 +00:00
*
2011-09-29 03:29:59 +00:00
* @param $element
* A renderable array.
2011-11-03 10:53:06 +00:00
*
2008-03-21 08:41:25 +00:00
* @return
2011-09-29 03:29:59 +00:00
* A renderable array.
2008-03-21 08:41:25 +00:00
*/
2011-09-29 03:29:59 +00:00
function _block_get_renderable_block($element) {
$block = $element['#block'];
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// Don't bother to build blocks that aren't accessible.
if ($element['#access'] = $block->access()) {
2013-01-17 04:03:30 +00:00
$element += entity_view($block, 'block');
2007-08-19 08:08:45 +00:00
}
2011-09-29 03:29:59 +00:00
return $element;
2007-11-26 16:36:44 +00:00
}
2009-02-03 12:30:14 +00:00
2012-05-03 15:09:39 +00:00
/**
* Implements hook_rebuild().
*/
function block_rebuild() {
2012-10-09 20:32:40 +00:00
foreach (list_themes() as $name => $data) {
if ($data->status) {
_block_rehash($name);
}
2010-06-15 16:19:28 +00:00
}
2009-02-03 12:30:14 +00:00
}
2009-04-26 01:15:04 +00:00
2013-09-29 07:19:59 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function block_theme_suggestions_block(array $variables) {
$suggestions = array();
$suggestions[] = 'block__' . $variables['elements']['#configuration']['module'];
// Hyphens (-) and underscores (_) play a special role in theme suggestions.
// Theme suggestions should only contain underscores, because within
// drupal_find_theme_templates(), underscores are converted to hyphens to
// match template file names, and then converted back to underscores to match
// pre-processing and other function names. So if your theme suggestion
// contains a hyphen, it will end up as an underscore after this conversion,
// and your function names won't be recognized. So, we need to convert
// hyphens to underscores in block deltas for the theme suggestions.
// We can safely explode on : because we know the Block plugin type manager
// enforces that delimiter for all derivatives.
$parts = explode(':', $variables['elements']['#plugin_id']);
$suggestion = 'block';
while ($part = array_shift($parts)) {
$suggestions[] = $suggestion .= '__' . strtr($part, '-', '_');
}
if ($id = $variables['elements']['#block']->id()) {
$config_id = explode('.', $id);
$machine_name = array_pop($config_id);
$suggestions[] = 'block__' . $machine_name;
}
return $suggestions;
}
2009-04-26 01:15:04 +00:00
/**
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* Prepares variables for block templates.
*
* Default template: block.html.twig.
2009-04-26 01:15:04 +00:00
*
2011-11-03 10:53:06 +00:00
* Prepares the values passed to the theme_block function to be passed
2009-04-26 01:15:04 +00:00
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* block.html.twig is used.
2009-04-26 01:15:04 +00:00
*
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* Most themes use their own copy of block.html.twig. The default is located
* inside "core/modules/block/templates/block.html.twig". Look in there for the
* full list of available variables.
2009-04-26 01:15:04 +00:00
*
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the properties of the element.
* Properties used: #block, #configuration, #children, #plugin_id.
2009-04-26 01:15:04 +00:00
*/
function template_preprocess_block(&$variables) {
2009-06-07 02:29:07 +00:00
$block_counter = &drupal_static(__FUNCTION__, array());
2013-01-17 04:03:30 +00:00
2013-05-11 10:01:25 +00:00
$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'];
2009-05-21 21:12:25 +00:00
2013-05-15 18:59:43 +00:00
$variables['attributes']['class'][] = 'block';
2013-05-11 10:01:25 +00:00
$variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['module']);
2009-05-28 16:44:07 +00:00
2013-06-12 15:57:44 +00:00
// The block template provides a wrapping element for the content. Render the
// #attributes of the content on this wrapping element rather than passing
// them through to the content's #theme function/template. This allows the
// content to not require a function/template at all, or if it does use one,
// to not require it to output an extra wrapping element.
if (isset($variables['content']['#attributes'])) {
$variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']);
unset($variables['content']['#attributes']);
}
2011-09-24 20:15:40 +00:00
// Add default class for block content.
2012-08-03 15:31:18 +00:00
$variables['content_attributes']['class'][] = 'content';
2011-09-24 20:15:40 +00:00
2010-04-26 14:10:40 +00:00
// Create a valid HTML ID and make sure it is unique.
2013-01-17 04:03:30 +00:00
if ($id = $variables['elements']['#block']->id()) {
$config_id = explode('.', $id);
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
$machine_name = array_pop($config_id);
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
$variables['attributes']['id'] = drupal_html_id('block-' . $machine_name);
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2009-05-28 16:44:07 +00:00
}
2009-08-26 10:29:26 +00:00
2009-08-27 20:25:29 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_role_delete().
2009-08-27 20:25:29 +00:00
*
2011-11-03 10:53:06 +00:00
* Removes deleted role from blocks that use it.
2009-08-27 20:25:29 +00:00
*/
function block_user_role_delete($role) {
2013-08-13 08:37:51 +00:00
foreach (entity_load_multiple('block') as $block) {
2013-01-17 04:03:30 +00:00
$visibility = $block->get('visibility');
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
if (isset($visibility['roles']['roles'][$role->id()])) {
unset($visibility['roles']['roles'][$role->id()]);
2013-01-17 04:03:30 +00:00
$block->set('visibility', $visibility);
$block->save();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2009-08-27 20:25:29 +00:00
}
2009-10-09 08:02:25 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu_delete().
2009-10-09 08:02:25 +00:00
*/
function block_menu_delete($menu) {
2013-08-13 08:37:51 +00:00
foreach (entity_load_multiple('block') as $block) {
2013-09-10 06:32:13 +00:00
if ($block->get('plugin') == 'system_menu_block:' . $menu->id()) {
2013-01-17 04:03:30 +00:00
$block->delete();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2009-10-09 08:02:25 +00:00
}
#655416 by casey, yoroy, BTMash, Bojhan, Gábor Hojtsy, sun, ksenzee, oseldman, jrguitar21, et al: 'Demonstrate block preview' should not open in the overlay, and should reflect the regions in the front-end theme.
2010-07-31 12:54:59 +00:00
/**
* Implements hook_admin_paths().
*/
function block_admin_paths() {
$paths = array(
// Exclude the block demonstration page from admin (overlay) treatment.
// This allows us to present this page in its true form, full page.
'admin/structure/block/demo/*' => FALSE,
);
return $paths;
}
2012-04-19 17:45:46 +00:00
/**
* Implements hook_language_delete().
*
* Delete the potential block visibility settings of the deleted language.
*/
function block_language_delete($language) {
// Remove the block visibility settings for the deleted language.
2013-08-13 08:37:51 +00:00
foreach (entity_load_multiple('block') as $block) {
2013-01-17 04:03:30 +00:00
$visibility = $block->get('visibility');
2013-06-29 10:56:53 +00:00
if (isset($visibility['language']['langcodes'][$language->id])) {
unset($visibility['language']['langcodes'][$language->id]);
2013-01-17 04:03:30 +00:00
$block->set('visibility', $visibility);
$block->save();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2010-03-09 12:09:52 +00:00
}
2012-08-30 19:24:38 +00:00
/**
* Implements hook_library_info().
*/
function block_library_info() {
$libraries['drupal.block'] = array(
'title' => 'Block',
2013-09-16 03:58:06 +00:00
'version' => \Drupal::VERSION,
2012-08-30 19:24:38 +00:00
'js' => array(
drupal_get_path('module', 'block') . '/block.js' => array(),
),
'dependencies' => array(
array('system', 'jquery'),
array('system', 'drupal'),
),
);
2013-08-15 15:28:57 +00:00
$libraries['drupal.block.admin'] = array(
'title' => 'Block admin',
2013-09-16 03:58:06 +00:00
'version' => \Drupal::VERSION,
2013-08-15 15:28:57 +00:00
'js' => array(
drupal_get_path('module', 'block') . '/js/block.admin.js' => array(),
),
'css' => array(
drupal_get_path('module', 'block') . '/css/block.admin.css' => array(),
),
'dependencies' => array(
array('system', 'jquery'),
array('system', 'drupal'),
),
);
2012-08-30 19:24:38 +00:00
return $libraries;
}