Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
221
web/core/modules/block/block.api.php
Normal file
221
web/core/modules/block/block.api.php
Normal file
|
@ -0,0 +1,221 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the Block module.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
|
||||
/**
|
||||
* @defgroup block_api Block API
|
||||
* @{
|
||||
* Information about the classes and interfaces that make up the Block API.
|
||||
*
|
||||
* Blocks are a combination of a configuration entity and a plugin. The
|
||||
* configuration entity stores placement information (theme, region, weight) and
|
||||
* any other configuration that is specific to the block. The block plugin does
|
||||
* the work of rendering the block's content for display.
|
||||
*
|
||||
* To define a block in a module you need to:
|
||||
* - Define a Block plugin by creating a new class that implements the
|
||||
* \Drupal\Core\Block\BlockPluginInterface, in namespace Plugin\Block under your
|
||||
* module namespace. For more information about creating plugins, see the
|
||||
* @link plugin_api Plugin API topic. @endlink
|
||||
* - Usually you will want to extend the \Drupal\Core\Block\BlockBase class, which
|
||||
* provides a common configuration form and utility methods for getting and
|
||||
* setting configuration in the block configuration entity.
|
||||
* - Block plugins use the annotations defined by
|
||||
* \Drupal\Core\Block\Annotation\Block. See the
|
||||
* @link annotation Annotations topic @endlink for more information about
|
||||
* annotations.
|
||||
*
|
||||
* The Block API also makes use of Condition plugins, for conditional block
|
||||
* placement. Condition plugins have interface
|
||||
* \Drupal\Core\Condition\ConditionInterface, base class
|
||||
* \Drupal\Core\Condition\ConditionPluginBase, and go in plugin namespace
|
||||
* Plugin\Condition. Again, see the Plugin API and Annotations topics for
|
||||
* details of how to create a plugin class and annotate it.
|
||||
*
|
||||
* There are also several block-related hooks, which allow you to affect
|
||||
* the content and access permissions for blocks:
|
||||
* - hook_block_view_alter()
|
||||
* - hook_block_view_BASE_BLOCK_ID_alter()
|
||||
* - hook_block_access()
|
||||
*
|
||||
* Further information and examples:
|
||||
* - \Drupal\system\Plugin\Block\SystemPoweredByBlock provides a simple example
|
||||
* of defining a block.
|
||||
* - \Drupal\user\Plugin\Condition\UserRole is a straightforward example of a
|
||||
* block placement condition plugin.
|
||||
* - \Drupal\book\Plugin\Block\BookNavigationBlock is an example of a block with
|
||||
* a custom configuration form.
|
||||
* - For a more in-depth discussion of the Block API, see
|
||||
* https://www.drupal.org/developing/api/8/block_api.
|
||||
* - The Examples for Developers project also provides a Block example in
|
||||
* https://www.drupal.org/project/examples.
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Alter the result of \Drupal\Core\Block\BlockBase::build().
|
||||
*
|
||||
* This hook is called after the content has been assembled in a structured
|
||||
* array and may be used for doing processing which requires that the complete
|
||||
* block content structure has been built.
|
||||
*
|
||||
* If the module wishes to act on the rendered HTML of the block rather than
|
||||
* the structured content array, it may use this hook to add a #post_render
|
||||
* callback. Alternatively, it could also implement hook_preprocess_HOOK() for
|
||||
* block.html.twig. See drupal_render() documentation or the
|
||||
* @link themeable Default theme implementations topic @endlink for details.
|
||||
*
|
||||
* In addition to hook_block_view_alter(), which is called for all blocks, there
|
||||
* is hook_block_view_BASE_BLOCK_ID_alter(), which can be used to target a
|
||||
* specific block or set of similar blocks.
|
||||
*
|
||||
* @param array &$build
|
||||
* A renderable array of data, as returned from the build() implementation of
|
||||
* the plugin that defined the block:
|
||||
* - #title: The default localized title of the block.
|
||||
* @param \Drupal\Core\Block\BlockPluginInterface $block
|
||||
* The block plugin instance.
|
||||
*
|
||||
* @see hook_block_view_BASE_BLOCK_ID_alter()
|
||||
* @see entity_crud
|
||||
*
|
||||
* @ingroup block_api
|
||||
*/
|
||||
function hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
|
||||
// Remove the contextual links on all blocks that provide them.
|
||||
if (isset($build['#contextual_links'])) {
|
||||
unset($build['#contextual_links']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a block plugin specific block_view alteration.
|
||||
*
|
||||
* In this hook name, BASE_BLOCK_ID refers to the block implementation's plugin
|
||||
* id, regardless of whether the plugin supports derivatives. For example, for
|
||||
* the \Drupal\system\Plugin\Block\SystemPoweredByBlock block, this would be
|
||||
* 'system_powered_by_block' as per that class's annotation. And for the
|
||||
* \Drupal\system\Plugin\Block\SystemMenuBlock block, it would be
|
||||
* 'system_menu_block' as per that class's annotation, regardless of which menu
|
||||
* the derived block is for.
|
||||
*
|
||||
* @param array $build
|
||||
* A renderable array of data, as returned from the build() implementation of
|
||||
* the plugin that defined the block:
|
||||
* - #title: The default localized title of the block.
|
||||
* @param \Drupal\Core\Block\BlockPluginInterface $block
|
||||
* The block plugin instance.
|
||||
*
|
||||
* @see hook_block_view_alter()
|
||||
* @see entity_crud
|
||||
*
|
||||
* @ingroup block_api
|
||||
*/
|
||||
function hook_block_view_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
|
||||
// Change the title of the specific block.
|
||||
$build['#title'] = t('New title of the block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter the result of \Drupal\Core\Block\BlockBase::build().
|
||||
*
|
||||
* Unlike hook_block_view_alter(), this hook is called very early, before the
|
||||
* block is being assembled. Therefore, it is early enough to alter the
|
||||
* cacheability metadata (change #cache), or to explicitly placeholder the block
|
||||
* (set #create_placeholder).
|
||||
*
|
||||
* In addition to hook_block_build_alter(), which is called for all blocks,
|
||||
* there is hook_block_build_BASE_BLOCK_ID_alter(), which can be used to target
|
||||
* a specific block or set of similar blocks.
|
||||
*
|
||||
* @param array &$build
|
||||
* A renderable array of data, only containing #cache.
|
||||
* @param \Drupal\Core\Block\BlockPluginInterface $block
|
||||
* The block plugin instance.
|
||||
*
|
||||
* @see hook_block_build_BASE_BLOCK_ID_alter()
|
||||
* @see entity_crud
|
||||
*
|
||||
* @ingroup block_api
|
||||
*/
|
||||
function hook_block_build_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
|
||||
// Add the 'user' cache context to some blocks.
|
||||
if ($some_condition) {
|
||||
$build['#cache']['contexts'][] = 'user';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a block plugin specific block_build alteration.
|
||||
*
|
||||
* In this hook name, BASE_BLOCK_ID refers to the block implementation's plugin
|
||||
* id, regardless of whether the plugin supports derivatives. For example, for
|
||||
* the \Drupal\system\Plugin\Block\SystemPoweredByBlock block, this would be
|
||||
* 'system_powered_by_block' as per that class's annotation. And for the
|
||||
* \Drupal\system\Plugin\Block\SystemMenuBlock block, it would be
|
||||
* 'system_menu_block' as per that class's annotation, regardless of which menu
|
||||
* the derived block is for.
|
||||
*
|
||||
* @param array $build
|
||||
* A renderable array of data, only containing #cache.
|
||||
* @param \Drupal\Core\Block\BlockPluginInterface $block
|
||||
* The block plugin instance.
|
||||
*
|
||||
* @see hook_block_build_alter()
|
||||
* @see entity_crud
|
||||
*
|
||||
* @ingroup block_api
|
||||
*/
|
||||
function hook_block_build_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
|
||||
// Explicitly enable placeholdering of the specific block.
|
||||
$build['#create_placeholder'] = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Control access to a block instance.
|
||||
*
|
||||
* Modules may implement this hook if they want to have a say in whether or not
|
||||
* a given user has access to perform a given operation on a block instance.
|
||||
*
|
||||
* @param \Drupal\block\Entity\Block $block
|
||||
* The block instance.
|
||||
* @param string $operation
|
||||
* The operation to be performed; for instance, 'view', 'create', 'delete', or
|
||||
* 'update'.
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The user object to perform the access check operation on.
|
||||
*
|
||||
* @return \Drupal\Core\Access\AccessResultInterface
|
||||
* The access result. If all implementations of this hook return
|
||||
* AccessResultInterface objects whose value is !isAllowed() and
|
||||
* !isForbidden(), then default access rules from
|
||||
* \Drupal\block\BlockAccessControlHandler::checkAccess() are used.
|
||||
*
|
||||
* @see \Drupal\Core\Entity\EntityAccessControlHandler::access()
|
||||
* @see \Drupal\block\BlockAccessControlHandler::checkAccess()
|
||||
* @ingroup block_api
|
||||
*/
|
||||
function hook_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) {
|
||||
// Example code that would prevent displaying the 'Powered by Drupal' block in
|
||||
// a region different than the footer.
|
||||
if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') {
|
||||
return AccessResult::forbiddenIf($block->getRegion() != 'footer')->addCacheableDependency($block);
|
||||
}
|
||||
|
||||
// No opinion.
|
||||
return AccessResult::neutral();
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup hooks".
|
||||
*/
|
7
web/core/modules/block/block.info.yml
Normal file
7
web/core/modules/block/block.info.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
name: Block
|
||||
type: module
|
||||
description: 'Controls the visual building blocks a page is constructed with. Blocks are boxes of content rendered into an area, or region, of a web page.'
|
||||
package: Core
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
configure: block.admin_display
|
128
web/core/modules/block/block.install
Normal file
128
web/core/modules/block/block.install
Normal file
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains install and update functions for Block.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function block_install() {
|
||||
// Because the Block module upon installation unconditionally overrides all
|
||||
// HTML output by selecting a different page display variant, we must
|
||||
// invalidate all cached HTML output.
|
||||
Cache::invalidateTags(['rendered']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup updates-8.0.0-beta
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Update block visibility context mapping.
|
||||
*/
|
||||
function block_update_8001() {
|
||||
// This update function updates blocks for the change from
|
||||
// https://www.drupal.org/node/2354889.
|
||||
|
||||
// Core visibility context plugins are updated automatically; blocks with
|
||||
// unknown plugins are disabled and their previous visibility settings are
|
||||
// saved in key value storage; see change record
|
||||
// https://www.drupal.org/node/2527840 for more explanation.
|
||||
|
||||
// These are all the contexts that Drupal core provides.
|
||||
$context_service_id_map = [
|
||||
'node.node' => '@node.node_route_context:node',
|
||||
'user.current_user' => '@user.current_user_context:current_user',
|
||||
];
|
||||
|
||||
foreach (array_keys(\Drupal::languageManager()->getDefinedLanguageTypesInfo()) as $language_type_id) {
|
||||
$context_service_id_map['language.' . $language_type_id] = '@language.current_language_context:' . $language_type_id;
|
||||
}
|
||||
|
||||
// Contributed modules should leverage hook_update_dependencies() in order to
|
||||
// be executed after block_update_8001(). The blocks are then disabled if the
|
||||
// contexts are still missing via
|
||||
// block_post_update_disable_blocks_with_missing_contexts().
|
||||
$config_factory = \Drupal::configFactory();
|
||||
$backup_values = $update_backup = [];
|
||||
|
||||
foreach ($config_factory->listAll('block.block.') as $block_config_name) {
|
||||
$block = $config_factory->getEditable($block_config_name);
|
||||
if ($visibility = $block->get('visibility')) {
|
||||
foreach ($visibility as $condition_plugin_id => &$condition) {
|
||||
foreach ($condition['context_mapping'] as $key => $context) {
|
||||
if (!isset($context_service_id_map[$context])) {
|
||||
// Remove the visibility condition for unknown context mapping
|
||||
// entries, so the update process itself runs through and users can
|
||||
// fix their block placements manually OR alternatively contributed
|
||||
// modules can run their own update functions to update mappings
|
||||
// that they provide.
|
||||
$backup_values[$context][] = $condition_plugin_id;
|
||||
unset($visibility[$condition_plugin_id]);
|
||||
continue;
|
||||
}
|
||||
// Replace the context ID based on the defined mapping.
|
||||
$condition['context_mapping'][$key] = $context_service_id_map[$context];
|
||||
}
|
||||
}
|
||||
$block->set('visibility', $visibility);
|
||||
|
||||
if ($backup_values) {
|
||||
// We not only store the missing context mappings but also the previous
|
||||
// block status, in order to allow contributed and custom modules to do
|
||||
// their own updates.
|
||||
$update_backup[$block->get('id')] = [
|
||||
'missing_context_ids' => $backup_values,
|
||||
'status' => $block->get('status')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Mark the resulting configuration as trusted data. This avoids issues with
|
||||
// future schema changes.
|
||||
$block->save(TRUE);
|
||||
}
|
||||
|
||||
if ($update_backup) {
|
||||
\Drupal::keyValue('update_backup')->set('block_update_8001', $update_backup);
|
||||
}
|
||||
|
||||
return t('Block context IDs updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholder for the previous 8002 update.
|
||||
*/
|
||||
function block_update_8002() {
|
||||
\Drupal::state()->set('block_update_8002_placeholder', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove 'cache' setting.
|
||||
*/
|
||||
function block_update_8003() {
|
||||
$config_factory = \Drupal::configFactory();
|
||||
foreach ($config_factory->listAll('block.block.') as $block_config_name) {
|
||||
$block = $config_factory->getEditable($block_config_name);
|
||||
|
||||
// Remove the 'cache' setting.
|
||||
$settings = $block->get('settings');
|
||||
unset($settings['cache']);
|
||||
$block->set('settings', $settings);
|
||||
|
||||
// Mark the resulting configuration as trusted data. This avoids issues with
|
||||
// future schema changes.
|
||||
$block->save(TRUE);
|
||||
}
|
||||
|
||||
return t('Block settings updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup updates-8.0.0-beta".
|
||||
*/
|
19
web/core/modules/block/block.libraries.yml
Normal file
19
web/core/modules/block/block.libraries.yml
Normal file
|
@ -0,0 +1,19 @@
|
|||
drupal.block:
|
||||
version: VERSION
|
||||
js:
|
||||
js/block.js: {}
|
||||
dependencies:
|
||||
- core/jquery
|
||||
- core/drupal
|
||||
|
||||
drupal.block.admin:
|
||||
version: VERSION
|
||||
js:
|
||||
js/block.admin.js: {}
|
||||
css:
|
||||
theme:
|
||||
css/block.admin.css: {}
|
||||
dependencies:
|
||||
- core/jquery
|
||||
- core/drupal
|
||||
- core/drupal.dialog.ajax
|
4
web/core/modules/block/block.links.contextual.yml
Normal file
4
web/core/modules/block/block.links.contextual.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
block_configure:
|
||||
title: 'Configure block'
|
||||
route_name: 'entity.block.edit_form'
|
||||
group: 'block'
|
5
web/core/modules/block/block.links.menu.yml
Normal file
5
web/core/modules/block/block.links.menu.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
block.admin_display:
|
||||
title: 'Block layout'
|
||||
parent: system.admin_structure
|
||||
description: 'Configure what block content appears in your site''s sidebars and other regions.'
|
||||
route_name: block.admin_display
|
15
web/core/modules/block/block.links.task.yml
Normal file
15
web/core/modules/block/block.links.task.yml
Normal file
|
@ -0,0 +1,15 @@
|
|||
entity.block.edit_form:
|
||||
title: 'Configure block'
|
||||
route_name: entity.block.edit_form
|
||||
base_route: entity.block.edit_form
|
||||
|
||||
# Per theme block layout pages.
|
||||
block.admin_display:
|
||||
title: 'Block layout'
|
||||
route_name: block.admin_display
|
||||
base_route: block.admin_display
|
||||
block.admin_display_theme:
|
||||
title: 'Block layout'
|
||||
route_name: block.admin_display_theme
|
||||
parent_id: block.admin_display
|
||||
deriver: 'Drupal\block\Plugin\Derivative\ThemeLocalTask'
|
286
web/core/modules/block/block.module
Normal file
286
web/core/modules/block/block.module
Normal file
|
@ -0,0 +1,286 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Controls the visual building blocks a page is constructed with.
|
||||
*/
|
||||
|
||||
use Drupal\block\BlockInterface;
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\language\ConfigurableLanguageInterface;
|
||||
use Drupal\system\Entity\Menu;
|
||||
use Drupal\block\Entity\Block;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function block_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'help.page.block':
|
||||
$block_content = \Drupal::moduleHandler()->moduleExists('block_content') ? \Drupal::url('help.page', array('name' => 'block_content')) : '#';
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('The Block module allows you to place blocks in regions of your installed themes, and configure block settings. For more information, see the <a href=":blocks-documentation">online documentation for the Block module</a>.', array(':blocks-documentation' => 'https://www.drupal.org/documentation/modules/block/')) . '</p>';
|
||||
$output .= '<h3>' . t('Uses') . '</h3>';
|
||||
$output .= '<dl>';
|
||||
$output .= '<dt>' . t('Placing and moving blocks') . '</dt>';
|
||||
$output .= '<dd>' . t('You can place a new block in a region by selecting <em>Place block</em> on the <a href=":blocks">Block layout page</a>. Once a block is placed, it can be moved to a different region by drag-and-drop or by using the <em>Region</em> drop-down list, and then clicking <em>Save blocks</em>.', array(':blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
|
||||
$output .= '<dt>' . t('Toggling between different themes') . '</dt>';
|
||||
$output .= '<dd>' . t('Blocks are placed and configured specifically for each theme. The Block layout page opens with the default theme, but you can toggle to other installed themes.') . '</dd>';
|
||||
$output .= '<dt>' . t('Demonstrating block regions for a theme') . '</dt>';
|
||||
$output .= '<dd>' . t('You can see where the regions are for the current theme by clicking the <em>Demonstrate block regions</em> link on the <a href=":blocks">Block layout page</a>. Regions are specific to each theme.', array(':blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
|
||||
$output .= '<dt>' . t('Configuring block settings') . '</dt>';
|
||||
$output .= '<dd>' . t('To change the settings of an individual block click on the <em>Configure</em> link on the <a href=":blocks">Block layout page</a>. The available options vary depending on the module that provides the block. For all blocks you can change the block title and toggle whether to display it.', array(':blocks' => Drupal::url('block.admin_display'))) . '</dd>';
|
||||
$output .= '<dt>' . t('Controlling visibility') . '</dt>';
|
||||
$output .= '<dd>' . t('You can control the visibility of a block by restricting it to specific pages, content types, and/or roles by setting the appropriate options under <em>Visibility settings</em> of the block configuration.') . '</dd>';
|
||||
$output .= '<dt>' . t('Adding custom blocks') . '</dt>';
|
||||
$output .= '<dd>' . t('You can add custom blocks, if the <em>Custom Block</em> module is installed. For more information, see the <a href=":blockcontent-help">Custom Block help page</a>.', array(':blockcontent-help' => $block_content)) . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
if ($route_name == 'block.admin_display' || $route_name == 'block.admin_display_theme') {
|
||||
$demo_theme = $route_match->getParameter('theme') ?: \Drupal::config('system.theme')->get('default');
|
||||
$themes = \Drupal::service('theme_handler')->listInfo();
|
||||
$output = '<p>' . t('Block placement is specific to each theme on your site. Changes will not be saved until you click <em>Save blocks</em> at the bottom of the page.') . '</p>';
|
||||
$output .= '<p>' . \Drupal::l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), new Url('block.admin_demo', array('theme' => $demo_theme))) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function block_theme() {
|
||||
return array(
|
||||
'block' => array(
|
||||
'render element' => 'elements',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_page_top().
|
||||
*/
|
||||
function block_page_top(array &$page_top) {
|
||||
if (\Drupal::routeMatch()->getRouteName() === 'block.admin_demo') {
|
||||
$theme = \Drupal::theme()->getActiveTheme()->getName();
|
||||
$page_top['backlink'] = array(
|
||||
'#type' => 'link',
|
||||
'#title' => t('Exit block region demonstration'),
|
||||
'#options' => array('attributes' => array('class' => array('block-demo-backlink'))),
|
||||
'#weight' => -10,
|
||||
);
|
||||
if (\Drupal::config('system.theme')->get('default') == $theme) {
|
||||
$page_top['backlink']['#url'] = Url::fromRoute('block.admin_display');
|
||||
}
|
||||
else {
|
||||
$page_top['backlink']['#url'] = Url::fromRoute('block.admin_display_theme', ['theme' => $theme]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes blocks for installed themes.
|
||||
*
|
||||
* @param $theme_list
|
||||
* An array of theme names.
|
||||
*/
|
||||
function block_themes_installed($theme_list) {
|
||||
foreach ($theme_list as $theme) {
|
||||
// Don't initialize themes that are not displayed in the UI.
|
||||
if (\Drupal::service('theme_handler')->hasUi($theme)) {
|
||||
block_theme_initialize($theme);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns an initial, default set of blocks for a theme.
|
||||
*
|
||||
* This function is called the first time a new theme is installed. 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.
|
||||
*
|
||||
* @param $theme
|
||||
* The name of a theme.
|
||||
*/
|
||||
function block_theme_initialize($theme) {
|
||||
// Initialize theme's blocks if none already registered.
|
||||
$has_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(array('theme' => $theme));
|
||||
if (!$has_blocks) {
|
||||
$default_theme = \Drupal::config('system.theme')->get('default');
|
||||
// Apply only to new theme's visible regions.
|
||||
$regions = system_region_list($theme, REGIONS_VISIBLE);
|
||||
$default_theme_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(array('theme' => $default_theme));
|
||||
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
|
||||
if (strpos($default_theme_block_id, $default_theme . '_') === 0) {
|
||||
$id = str_replace($default_theme, $theme, $default_theme_block_id);
|
||||
}
|
||||
else {
|
||||
$id = $theme . '_' . $default_theme_block_id;
|
||||
}
|
||||
$block = $default_theme_block->createDuplicateBlock($id, $theme);
|
||||
// If the region isn't supported by the theme, assign the block to the
|
||||
// theme's default region.
|
||||
if (!isset($regions[$block->getRegion()])) {
|
||||
$block->setRegion(system_default_region($theme));
|
||||
}
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_rebuild().
|
||||
*/
|
||||
function block_rebuild() {
|
||||
foreach (\Drupal::service('theme_handler')->listInfo() as $theme => $data) {
|
||||
if ($data->status) {
|
||||
$regions = system_region_list($theme);
|
||||
/** @var \Drupal\block\BlockInterface[] $blocks */
|
||||
$blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]);
|
||||
foreach ($blocks as $block_id => $block) {
|
||||
// Disable blocks in invalid regions.
|
||||
$region = $block->getRegion();
|
||||
if ($region !== BlockInterface::BLOCK_REGION_NONE) {
|
||||
if (!empty($region) && !isset($regions[$region]) && $block->status()) {
|
||||
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block_id, '%region' => $region]), 'warning');
|
||||
$block->disable();
|
||||
}
|
||||
// Set region to none if not enabled.
|
||||
if (!$block->status()) {
|
||||
$block->setRegion(BlockInterface::BLOCK_REGION_NONE);
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme_suggestions_HOOK().
|
||||
*/
|
||||
function block_theme_suggestions_block(array $variables) {
|
||||
$suggestions = array();
|
||||
|
||||
$suggestions[] = 'block__' . $variables['elements']['#configuration']['provider'];
|
||||
// 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 (!empty($variables['elements']['#id'])) {
|
||||
$suggestions[] = 'block__' . $variables['elements']['#id'];
|
||||
}
|
||||
|
||||
return $suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares variables for block templates.
|
||||
*
|
||||
* Default template: block.html.twig.
|
||||
*
|
||||
* Prepares the values passed to the theme_block function to be passed
|
||||
* into a pluggable template engine. Uses block properties to generate a
|
||||
* series of template file suggestions. If none are found, the default
|
||||
* block.html.twig is used.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param array $variables
|
||||
* An associative array containing:
|
||||
* - elements: An associative array containing the properties of the element.
|
||||
* Properties used: #block, #configuration, #children, #plugin_id.
|
||||
*/
|
||||
function template_preprocess_block(&$variables) {
|
||||
$variables['configuration'] = $variables['elements']['#configuration'];
|
||||
$variables['plugin_id'] = $variables['elements']['#plugin_id'];
|
||||
$variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
|
||||
$variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
|
||||
$variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
|
||||
$variables['content'] = $variables['elements']['content'];
|
||||
// A block's label is configuration: it is static. Allow dynamic labels to be
|
||||
// set in the render array.
|
||||
if (isset($variables['elements']['content']['#title']) && !empty($variables['configuration']['label_display'])) {
|
||||
$variables['label'] = $variables['elements']['content']['#title'];
|
||||
}
|
||||
|
||||
// Create a valid HTML ID and make sure it is unique.
|
||||
if (!empty($variables['elements']['#id'])) {
|
||||
$variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']);
|
||||
}
|
||||
|
||||
// Proactively add aria-describedby if possible to improve accessibility.
|
||||
if ($variables['label'] && isset($variables['attributes']['role'])) {
|
||||
$variables['title_attributes']['id'] = Html::getUniqueId($variables['label']);
|
||||
$variables['attributes']['aria-describedby'] = $variables['title_attributes']['id'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_ENTITY_TYPE_delete() for user_role entities.
|
||||
*
|
||||
* Removes deleted role from blocks that use it.
|
||||
*/
|
||||
function block_user_role_delete($role) {
|
||||
foreach (Block::loadMultiple() as $block) {
|
||||
/** @var $block \Drupal\block\BlockInterface */
|
||||
$visibility = $block->getVisibility();
|
||||
if (isset($visibility['user_role']['roles'][$role->id()])) {
|
||||
unset($visibility['user_role']['roles'][$role->id()]);
|
||||
$block->setVisibilityConfig('user_role', $visibility['user_role']);
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_ENTITY_TYPE_delete() for menu entities.
|
||||
*/
|
||||
function block_menu_delete(Menu $menu) {
|
||||
if (!$menu->isSyncing()) {
|
||||
foreach (Block::loadMultiple() as $block) {
|
||||
if ($block->getPluginId() == 'system_menu_block:' . $menu->id()) {
|
||||
$block->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_ENTITY_TYPE_delete() for 'configurable_language'.
|
||||
*
|
||||
* Delete the potential block visibility settings of the deleted language.
|
||||
*/
|
||||
function block_configurable_language_delete(ConfigurableLanguageInterface $language) {
|
||||
// Remove the block visibility settings for the deleted language.
|
||||
foreach (Block::loadMultiple() as $block) {
|
||||
/** @var $block \Drupal\block\BlockInterface */
|
||||
$visibility = $block->getVisibility();
|
||||
if (isset($visibility['language']['langcodes'][$language->id()])) {
|
||||
unset($visibility['language']['langcodes'][$language->id()]);
|
||||
$block->setVisibilityConfig('language', $visibility['language']);
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
}
|
2
web/core/modules/block/block.permissions.yml
Normal file
2
web/core/modules/block/block.permissions.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
administer blocks:
|
||||
title: 'Administer blocks'
|
112
web/core/modules/block/block.post_update.php
Normal file
112
web/core/modules/block/block.post_update.php
Normal file
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Post update functions for Block.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup updates-8.0.0-beta
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Disable all blocks with missing context IDs in block_update_8001().
|
||||
*/
|
||||
function block_post_update_disable_blocks_with_missing_contexts() {
|
||||
// Don't execute the function if block_update_8002() got executed already,
|
||||
// which used to do the same. Note: Its okay to check here, because
|
||||
// update_do_one() does not update the installed schema version until the
|
||||
// batch is finished.
|
||||
$module_schema = drupal_get_installed_schema_version('block');
|
||||
|
||||
// The state entry 'block_update_8002_placeholder' is used in order to
|
||||
// indicate that the placeholder block_update_8002() function has been
|
||||
// executed, so this function needs to be executed as well. If the non
|
||||
// placeholder version of block_update_8002() got executed already, the state
|
||||
// won't be set and we skip this update.
|
||||
if ($module_schema >= 8002 && !\Drupal::state()->get('block_update_8002_placeholder', FALSE)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Cleanup the state entry as its no longer needed.
|
||||
\Drupal::state()->delete('block_update_8002');
|
||||
|
||||
$block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
|
||||
|
||||
$block_ids = array_keys($block_update_8001);
|
||||
$block_storage = \Drupal::entityManager()->getStorage('block');
|
||||
$blocks = $block_storage->loadMultiple($block_ids);
|
||||
/** @var $blocks \Drupal\block\BlockInterface[] */
|
||||
foreach ($blocks as $block) {
|
||||
// This block has had conditions removed due to an inability to resolve
|
||||
// contexts in block_update_8001() so disable it.
|
||||
|
||||
// Disable currently enabled blocks.
|
||||
if ($block_update_8001[$block->id()]['status']) {
|
||||
$block->setStatus(FALSE);
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Provides a list of plugin labels, keyed by plugin ID.
|
||||
$condition_plugin_id_label_map = array_column(\Drupal::service('plugin.manager.condition')->getDefinitions(), 'label', 'id');
|
||||
|
||||
// Override with the UI labels we are aware of. Sadly they are not machine
|
||||
// accessible, see
|
||||
// \Drupal\node\Plugin\Condition\NodeType::buildConfigurationForm().
|
||||
$condition_plugin_id_label_map['node_type'] = t('Content types');
|
||||
$condition_plugin_id_label_map['request_path'] = t('Pages');
|
||||
$condition_plugin_id_label_map['user_role'] = t('Roles');
|
||||
|
||||
if (count($block_ids) > 0) {
|
||||
$message = t('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:');
|
||||
$message .= '<ul>';
|
||||
foreach ($blocks as $disabled_block_id => $disabled_block) {
|
||||
$message .= '<li>' . t('@label (Visibility: @plugin_ids)', array(
|
||||
'@label' => $disabled_block->get('settings')['label'],
|
||||
'@plugin_ids' => implode(', ', array_intersect_key($condition_plugin_id_label_map, array_flip(array_keys($block_update_8001[$disabled_block_id]['missing_context_ids']))))
|
||||
)) . '</li>';
|
||||
}
|
||||
$message .= '</ul>';
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup updates-8.0.0-beta".
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup updates-8.2.x
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fix invalid 'negate' values in block visibility conditions.
|
||||
*/
|
||||
function block_post_update_fix_negate_in_conditions() {
|
||||
$block_storage = \Drupal::entityTypeManager()->getStorage('block');
|
||||
/** @var \Drupal\block\BlockInterface[] $blocks */
|
||||
$blocks = $block_storage->loadMultiple();
|
||||
foreach ($blocks as $block) {
|
||||
$block_needs_saving = FALSE;
|
||||
// Check each visibility condition for an invalid negate value, and fix it.
|
||||
foreach ($block->getVisibilityConditions() as $condition_id => $condition) {
|
||||
$configuration = $condition->getConfiguration();
|
||||
if (array_key_exists('negate', $configuration) && !is_bool($configuration['negate'])) {
|
||||
$configuration['negate'] = (bool) $configuration['negate'];
|
||||
$condition->setConfiguration($configuration);
|
||||
$block_needs_saving = TRUE;
|
||||
}
|
||||
}
|
||||
if ($block_needs_saving) {
|
||||
$block->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup updates-8.2.x".
|
||||
*/
|
68
web/core/modules/block/block.routing.yml
Normal file
68
web/core/modules/block/block.routing.yml
Normal file
|
@ -0,0 +1,68 @@
|
|||
block.admin_demo:
|
||||
path: '/admin/structure/block/demo/{theme}'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\BlockController::demo'
|
||||
_title_callback: 'theme_handler:getName'
|
||||
requirements:
|
||||
_access_theme: 'TRUE'
|
||||
_permission: 'administer blocks'
|
||||
options:
|
||||
_admin_route: FALSE
|
||||
|
||||
entity.block.delete_form:
|
||||
path: '/admin/structure/block/manage/{block}/delete'
|
||||
defaults:
|
||||
_entity_form: 'block.delete'
|
||||
_title: 'Delete block'
|
||||
requirements:
|
||||
_permission: 'administer blocks'
|
||||
|
||||
entity.block.edit_form:
|
||||
path: '/admin/structure/block/manage/{block}'
|
||||
defaults:
|
||||
_entity_form: 'block.default'
|
||||
_title: 'Configure block'
|
||||
requirements:
|
||||
_entity_access: 'block.update'
|
||||
|
||||
block.admin_display:
|
||||
path: '/admin/structure/block'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\BlockListController::listing'
|
||||
_title: 'Block layout'
|
||||
requirements:
|
||||
_permission: 'administer blocks'
|
||||
|
||||
block.admin_display_theme:
|
||||
path: 'admin/structure/block/list/{theme}'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\BlockListController::listing'
|
||||
_title: 'Block layout'
|
||||
requirements:
|
||||
_access_theme: 'TRUE'
|
||||
_permission: 'administer blocks'
|
||||
|
||||
block.admin_library:
|
||||
path: 'admin/structure/block/library/{theme}'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\BlockLibraryController::listBlocks'
|
||||
_title: 'Place block'
|
||||
requirements:
|
||||
_access_theme: 'TRUE'
|
||||
_permission: 'administer blocks'
|
||||
|
||||
block.admin_add:
|
||||
path: '/admin/structure/block/add/{plugin_id}/{theme}'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\BlockAddController::blockAddConfigureForm'
|
||||
theme: null
|
||||
_title: 'Configure block'
|
||||
requirements:
|
||||
_permission: 'administer blocks'
|
||||
|
||||
block.category_autocomplete:
|
||||
path: '/block-category/autocomplete'
|
||||
defaults:
|
||||
_controller: '\Drupal\block\Controller\CategoryAutocompleteController::autocomplete'
|
||||
requirements:
|
||||
_permission: 'administer blocks'
|
12
web/core/modules/block/block.services.yml
Normal file
12
web/core/modules/block/block.services.yml
Normal file
|
@ -0,0 +1,12 @@
|
|||
services:
|
||||
theme.negotiator.block.admin_demo:
|
||||
class: Drupal\block\Theme\AdminDemoNegotiator
|
||||
tags:
|
||||
- { name: theme_negotiator, priority: 1000 }
|
||||
block.page_display_variant_subscriber:
|
||||
class: Drupal\block\EventSubscriber\BlockPageDisplayVariantSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
block.repository:
|
||||
class: Drupal\block\BlockRepository
|
||||
arguments: ['@entity.manager', '@theme.manager', '@context.handler']
|
35
web/core/modules/block/config/schema/block.schema.yml
Normal file
35
web/core/modules/block/config/schema/block.schema.yml
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Schema for the configuration files of the Block module.
|
||||
|
||||
block.block.*:
|
||||
type: config_entity
|
||||
label: 'Block'
|
||||
mapping:
|
||||
id:
|
||||
type: string
|
||||
label: 'ID'
|
||||
theme:
|
||||
type: string
|
||||
label: 'Theme'
|
||||
region:
|
||||
type: string
|
||||
label: 'Region'
|
||||
weight:
|
||||
type: integer
|
||||
label: 'Weight'
|
||||
provider:
|
||||
type: string
|
||||
label: 'Provider'
|
||||
plugin:
|
||||
type: string
|
||||
label: 'Plugin'
|
||||
settings:
|
||||
type: block.settings.[%parent.plugin]
|
||||
visibility:
|
||||
type: sequence
|
||||
label: 'Visibility Conditions'
|
||||
sequence:
|
||||
type: condition.plugin.[id]
|
||||
label: 'Visibility Condition'
|
||||
|
||||
block.settings.*:
|
||||
type: block_settings
|
42
web/core/modules/block/css/block.admin.css
Normal file
42
web/core/modules/block/css/block.admin.css
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* Block listing page */
|
||||
.region-title__action {
|
||||
display: inline-block;
|
||||
margin-left: 1em; /* LTR */
|
||||
}
|
||||
[dir="rtl"] .region-title__action {
|
||||
margin-left: 0;
|
||||
margin-right: 1em;
|
||||
}
|
||||
|
||||
/* Block demo mode */
|
||||
.block-region {
|
||||
background-color: #ff6;
|
||||
margin-top: 4px;
|
||||
margin-bottom: 4px;
|
||||
padding: 3px;
|
||||
}
|
||||
a.block-demo-backlink,
|
||||
a.block-demo-backlink:link,
|
||||
a.block-demo-backlink:visited {
|
||||
background-color: #b4d7f0;
|
||||
border-radius: 0 0 10px 10px;
|
||||
color: #000;
|
||||
font-family: "Lucida Grande", Verdana, sans-serif;
|
||||
font-size: small;
|
||||
line-height: 20px;
|
||||
left: 20px; /*LTR*/
|
||||
padding: 5px 10px;
|
||||
position: fixed;
|
||||
z-index: 499;
|
||||
}
|
||||
a.block-demo-backlink:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* Configure block form - Block description */
|
||||
.block-form .form-item-settings-admin-label label {
|
||||
display: inline;
|
||||
}
|
||||
.block-form .form-item-settings-admin-label label:after {
|
||||
content: ':';
|
||||
}
|
97
web/core/modules/block/js/block.admin.js
Normal file
97
web/core/modules/block/js/block.admin.js
Normal file
|
@ -0,0 +1,97 @@
|
|||
/**
|
||||
* @file
|
||||
* Block admin behaviors.
|
||||
*/
|
||||
|
||||
(function ($, Drupal) {
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Filters the block list by a text input search string.
|
||||
*
|
||||
* The text input will have the selector `input.block-filter-text`.
|
||||
*
|
||||
* The target element to do searching in will be in the selector
|
||||
* `input.block-filter-text[data-element]`
|
||||
*
|
||||
* The text source where the text should be found will have the selector
|
||||
* `.block-filter-text-source`
|
||||
*
|
||||
* @type {Drupal~behavior}
|
||||
*
|
||||
* @prop {Drupal~behaviorAttach} attach
|
||||
* Attaches the behavior for the block filtering.
|
||||
*/
|
||||
Drupal.behaviors.blockFilterByText = {
|
||||
attach: function (context, settings) {
|
||||
var $input = $('input.block-filter-text').once('block-filter-text');
|
||||
var $table = $($input.attr('data-element'));
|
||||
var $filter_rows;
|
||||
|
||||
/**
|
||||
* Filters the block list.
|
||||
*
|
||||
* @param {jQuery.Event} e
|
||||
* The jQuery event for the keyup event that triggered the filter.
|
||||
*/
|
||||
function filterBlockList(e) {
|
||||
var query = $(e.target).val().toLowerCase();
|
||||
|
||||
/**
|
||||
* Shows or hides the block entry based on the query.
|
||||
*
|
||||
* @param {number} index
|
||||
* The index in the loop, as provided by `jQuery.each`
|
||||
* @param {HTMLElement} label
|
||||
* The label of the block.
|
||||
*/
|
||||
function toggleBlockEntry(index, label) {
|
||||
var $label = $(label);
|
||||
var $row = $label.parent().parent();
|
||||
var textMatch = $label.text().toLowerCase().indexOf(query) !== -1;
|
||||
$row.toggle(textMatch);
|
||||
}
|
||||
|
||||
// Filter if the length of the query is at least 2 characters.
|
||||
if (query.length >= 2) {
|
||||
$filter_rows.each(toggleBlockEntry);
|
||||
}
|
||||
else {
|
||||
$filter_rows.each(function (index) {
|
||||
$(this).parent().parent().show();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if ($table.length) {
|
||||
$filter_rows = $table.find('div.block-filter-text-source');
|
||||
$input.on('keyup', filterBlockList);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Highlights the block that was just placed into the block listing.
|
||||
*
|
||||
* @type {Drupal~behavior}
|
||||
*
|
||||
* @prop {Drupal~behaviorAttach} attach
|
||||
* Attaches the behavior for the block placement highlighting.
|
||||
*/
|
||||
Drupal.behaviors.blockHighlightPlacement = {
|
||||
attach: function (context, settings) {
|
||||
if (settings.blockPlacement) {
|
||||
$(context).find('[data-drupal-selector="edit-blocks"]').once('block-highlight').each(function () {
|
||||
var $container = $(this);
|
||||
// Just scrolling the document.body will not work in Firefox. The html
|
||||
// element is needed as well.
|
||||
$('html, body').animate({
|
||||
scrollTop: $('.js-block-placed').offset().top - $container.offset().top + $container.scrollTop()
|
||||
}, 500);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}(jQuery, Drupal));
|
228
web/core/modules/block/js/block.js
Normal file
228
web/core/modules/block/js/block.js
Normal file
|
@ -0,0 +1,228 @@
|
|||
/**
|
||||
* @file
|
||||
* Block behaviors.
|
||||
*/
|
||||
|
||||
(function ($, window, Drupal) {
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Provide the summary information for the block settings vertical tabs.
|
||||
*
|
||||
* @type {Drupal~behavior}
|
||||
*
|
||||
* @prop {Drupal~behaviorAttach} attach
|
||||
* Attaches the behavior for the block settings summaries.
|
||||
*/
|
||||
Drupal.behaviors.blockSettingsSummary = {
|
||||
attach: function () {
|
||||
// The drupalSetSummary method required for this behavior is not available
|
||||
// on the Blocks administration page, so we need to make sure this
|
||||
// behavior is processed only if drupalSetSummary is defined.
|
||||
if (typeof $.fn.drupalSetSummary === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a summary for checkboxes in the provided context.
|
||||
*
|
||||
* @param {HTMLDocument|HTMLElement} context
|
||||
* A context where one would find checkboxes to summarize.
|
||||
*
|
||||
* @return {string}
|
||||
* A string with the summary.
|
||||
*/
|
||||
function checkboxesSummary(context) {
|
||||
var vals = [];
|
||||
var $checkboxes = $(context).find('input[type="checkbox"]:checked + label');
|
||||
var il = $checkboxes.length;
|
||||
for (var i = 0; i < il; i++) {
|
||||
vals.push($($checkboxes[i]).html());
|
||||
}
|
||||
if (!vals.length) {
|
||||
vals.push(Drupal.t('Not restricted'));
|
||||
}
|
||||
return vals.join(', ');
|
||||
}
|
||||
|
||||
$('[data-drupal-selector="edit-visibility-node-type"], [data-drupal-selector="edit-visibility-language"], [data-drupal-selector="edit-visibility-user-role"]').drupalSetSummary(checkboxesSummary);
|
||||
|
||||
$('[data-drupal-selector="edit-visibility-request-path"]').drupalSetSummary(function (context) {
|
||||
var $pages = $(context).find('textarea[name="visibility[request_path][pages]"]');
|
||||
if (!$pages.val()) {
|
||||
return Drupal.t('Not restricted');
|
||||
}
|
||||
else {
|
||||
return Drupal.t('Restricted to certain pages');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Move a block in the blocks table between regions via select list.
|
||||
*
|
||||
* This behavior is dependent on the tableDrag behavior, since it uses the
|
||||
* objects initialized in that behavior to update the row.
|
||||
*
|
||||
* @type {Drupal~behavior}
|
||||
*
|
||||
* @prop {Drupal~behaviorAttach} attach
|
||||
* Attaches the tableDrag behaviour for blocks in block administration.
|
||||
*/
|
||||
Drupal.behaviors.blockDrag = {
|
||||
attach: function (context, settings) {
|
||||
// tableDrag is required and we should be on the blocks admin page.
|
||||
if (typeof Drupal.tableDrag === 'undefined' || typeof Drupal.tableDrag.blocks === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to check empty regions and toggle classes based on this.
|
||||
*
|
||||
* @param {jQuery} table
|
||||
* The jQuery object representing the table to inspect.
|
||||
* @param {jQuery} rowObject
|
||||
* The jQuery object representing the table row.
|
||||
*/
|
||||
function checkEmptyRegions(table, rowObject) {
|
||||
table.find('tr.region-message').each(function () {
|
||||
var $this = $(this);
|
||||
// If the dragged row is in this region, but above the message row,
|
||||
// swap it down one space.
|
||||
if ($this.prev('tr').get(0) === rowObject.element) {
|
||||
// Prevent a recursion problem when using the keyboard to move rows
|
||||
// up.
|
||||
if ((rowObject.method !== 'keyboard' || rowObject.direction === 'down')) {
|
||||
rowObject.swap('after', this);
|
||||
}
|
||||
}
|
||||
// This region has become empty.
|
||||
if ($this.next('tr').is(':not(.draggable)') || $this.next('tr').length === 0) {
|
||||
$this.removeClass('region-populated').addClass('region-empty');
|
||||
}
|
||||
// This region has become populated.
|
||||
else if ($this.is('.region-empty')) {
|
||||
$this.removeClass('region-empty').addClass('region-populated');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to update the last placed row with the correct classes.
|
||||
*
|
||||
* @param {jQuery} table
|
||||
* The jQuery object representing the table to inspect.
|
||||
* @param {jQuery} rowObject
|
||||
* The jQuery object representing the table row.
|
||||
*/
|
||||
function updateLastPlaced(table, rowObject) {
|
||||
// Remove the color-success class from new block if applicable.
|
||||
table.find('.color-success').removeClass('color-success');
|
||||
|
||||
var $rowObject = $(rowObject);
|
||||
if (!$rowObject.is('.drag-previous')) {
|
||||
table.find('.drag-previous').removeClass('drag-previous');
|
||||
$rowObject.addClass('drag-previous');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update block weights in the given region.
|
||||
*
|
||||
* @param {jQuery} table
|
||||
* Table with draggable items.
|
||||
* @param {string} region
|
||||
* Machine name of region containing blocks to update.
|
||||
*/
|
||||
function updateBlockWeights(table, region) {
|
||||
// Calculate minimum weight.
|
||||
var weight = -Math.round(table.find('.draggable').length / 2);
|
||||
// Update the block weights.
|
||||
table.find('.region-' + region + '-message').nextUntil('.region-title')
|
||||
.find('select.block-weight').val(function () {
|
||||
// Increment the weight before assigning it to prevent using the
|
||||
// absolute minimum available weight. This way we always have an
|
||||
// unused upper and lower bound, which makes manually setting the
|
||||
// weights easier for users who prefer to do it that way.
|
||||
return ++weight;
|
||||
});
|
||||
}
|
||||
|
||||
var table = $('#blocks');
|
||||
// Get the blocks tableDrag object.
|
||||
var tableDrag = Drupal.tableDrag.blocks;
|
||||
// Add a handler for when a row is swapped, update empty regions.
|
||||
tableDrag.row.prototype.onSwap = function (swappedRow) {
|
||||
checkEmptyRegions(table, this);
|
||||
updateLastPlaced(table, this);
|
||||
};
|
||||
|
||||
// Add a handler so when a row is dropped, update fields dropped into
|
||||
// new regions.
|
||||
tableDrag.onDrop = function () {
|
||||
var dragObject = this;
|
||||
var $rowElement = $(dragObject.rowObject.element);
|
||||
// Use "region-message" row instead of "region" row because
|
||||
// "region-{region_name}-message" is less prone to regexp match errors.
|
||||
var regionRow = $rowElement.prevAll('tr.region-message').get(0);
|
||||
var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
|
||||
var regionField = $rowElement.find('select.block-region-select');
|
||||
// Check whether the newly picked region is available for this block.
|
||||
if (regionField.find('option[value=' + regionName + ']').length === 0) {
|
||||
// If not, alert the user and keep the block in its old region
|
||||
// setting.
|
||||
window.alert(Drupal.t('The block cannot be placed in this region.'));
|
||||
// Simulate that there was a selected element change, so the row is
|
||||
// put back to from where the user tried to drag it.
|
||||
regionField.trigger('change');
|
||||
}
|
||||
|
||||
// Update region and weight fields if the region has been changed.
|
||||
if (!regionField.is('.block-region-' + regionName)) {
|
||||
var weightField = $rowElement.find('select.block-weight');
|
||||
var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*block-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
|
||||
regionField.removeClass('block-region-' + oldRegionName).addClass('block-region-' + regionName);
|
||||
weightField.removeClass('block-weight-' + oldRegionName).addClass('block-weight-' + regionName);
|
||||
regionField.val(regionName);
|
||||
}
|
||||
|
||||
updateBlockWeights(table, regionName);
|
||||
};
|
||||
|
||||
// Add the behavior to each region select list.
|
||||
$(context).find('select.block-region-select').once('block-region-select')
|
||||
.on('change', function (event) {
|
||||
// Make our new row and select field.
|
||||
var row = $(this).closest('tr');
|
||||
var select = $(this);
|
||||
// Find the correct region and insert the row as the last in the
|
||||
// region.
|
||||
tableDrag.rowObject = new tableDrag.row(row[0]);
|
||||
var region_message = table.find('.region-' + select[0].value + '-message');
|
||||
var region_items = region_message.nextUntil('.region-message, .region-title');
|
||||
if (region_items.length) {
|
||||
region_items.last().after(row);
|
||||
}
|
||||
// We found that region_message is the last row.
|
||||
else {
|
||||
region_message.after(row);
|
||||
}
|
||||
updateBlockWeights(table, select[0].value);
|
||||
// Modify empty regions with added or removed fields.
|
||||
checkEmptyRegions(table, tableDrag.rowObject);
|
||||
// Update last placed block indication.
|
||||
updateLastPlaced(table, row);
|
||||
// Show unsaved changes warning.
|
||||
if (!tableDrag.changed) {
|
||||
$(Drupal.theme('tableDragChangedWarning')).insertBefore(tableDrag.table).hide().fadeIn('slow');
|
||||
tableDrag.changed = true;
|
||||
}
|
||||
// Remove focus from selectbox.
|
||||
select.trigger('blur');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
})(jQuery, window, Drupal);
|
102
web/core/modules/block/migration_templates/d6_block.yml
Normal file
102
web/core/modules/block/migration_templates/d6_block.yml
Normal file
|
@ -0,0 +1,102 @@
|
|||
id: d6_block
|
||||
label: Blocks
|
||||
migration_tags:
|
||||
- Drupal 6
|
||||
source:
|
||||
plugin: block
|
||||
process:
|
||||
# Block status is not a thing in Drupal 8, so this is how we skip over
|
||||
# disabled blocks.
|
||||
status:
|
||||
plugin: skip_on_empty
|
||||
method: row
|
||||
source: status
|
||||
id:
|
||||
# We need something unique, so aggregator, aggregator_1 etc will do.
|
||||
plugin: dedupe_entity
|
||||
entity_type: block
|
||||
field: id
|
||||
postfix: _
|
||||
length: 32
|
||||
source: module
|
||||
plugin:
|
||||
-
|
||||
plugin: static_map
|
||||
bypass: true
|
||||
source:
|
||||
- module
|
||||
- delta
|
||||
map:
|
||||
book:
|
||||
0: book_navigation
|
||||
comment:
|
||||
0: views_block:comments_recent-block_1
|
||||
forum:
|
||||
0: forum_active_block
|
||||
1: forum_new_block
|
||||
locale:
|
||||
0: language_block
|
||||
node:
|
||||
0: node_syndicate_block
|
||||
search:
|
||||
0: search_form_block
|
||||
statistics:
|
||||
0: statistics_popular_block
|
||||
system:
|
||||
0: system_powered_by_block
|
||||
user:
|
||||
0: user_login_block
|
||||
1: system_menu_block:tools
|
||||
2: views_block:who_s_new-block_1
|
||||
3: views_block:who_s_online-who_s_online_block
|
||||
-
|
||||
plugin: block_plugin_id
|
||||
-
|
||||
plugin: skip_on_empty
|
||||
method: row
|
||||
theme:
|
||||
plugin: block_theme
|
||||
source:
|
||||
- theme
|
||||
- default_theme
|
||||
- admin_theme
|
||||
region:
|
||||
plugin: block_region
|
||||
source:
|
||||
- theme
|
||||
- '@theme'
|
||||
- region
|
||||
map:
|
||||
garland:
|
||||
bartik:
|
||||
# Garland 6.x --> Bartik 8.x
|
||||
header: header
|
||||
footer: footer_fifth
|
||||
left: sidebar_first
|
||||
right: sidebar_second
|
||||
# If mapping fails, put the block in the content region.
|
||||
default_value: content
|
||||
weight: weight
|
||||
settings:
|
||||
plugin: block_settings
|
||||
source:
|
||||
- '@plugin'
|
||||
- delta
|
||||
- settings
|
||||
- title
|
||||
visibility:
|
||||
plugin: block_visibility
|
||||
source:
|
||||
- visibility
|
||||
- pages
|
||||
- roles
|
||||
# If the block uses PHP visibility, don't migrate it unless the PHP module
|
||||
# is enabled.
|
||||
skip_php: true
|
||||
destination:
|
||||
plugin: entity:block
|
||||
migration_dependencies:
|
||||
required:
|
||||
- menu
|
||||
- d6_custom_block
|
||||
- d6_user_role
|
110
web/core/modules/block/migration_templates/d7_block.yml
Normal file
110
web/core/modules/block/migration_templates/d7_block.yml
Normal file
|
@ -0,0 +1,110 @@
|
|||
id: d7_block
|
||||
label: Blocks
|
||||
migration_tags:
|
||||
- Drupal 7
|
||||
source:
|
||||
plugin: block
|
||||
process:
|
||||
# Block status is not a thing in Drupal 8, so this is how we skip over
|
||||
# disabled blocks.
|
||||
status:
|
||||
plugin: skip_on_empty
|
||||
method: row
|
||||
source: status
|
||||
id:
|
||||
-
|
||||
plugin: concat
|
||||
source:
|
||||
- theme
|
||||
- module
|
||||
- delta
|
||||
delimiter: _
|
||||
-
|
||||
plugin: machine_name
|
||||
field: id
|
||||
plugin:
|
||||
-
|
||||
plugin: static_map
|
||||
bypass: true
|
||||
source:
|
||||
- module
|
||||
- delta
|
||||
map:
|
||||
book:
|
||||
navigation: book_navigation
|
||||
comment:
|
||||
recent: views_block:comments_recent-block_1
|
||||
forum:
|
||||
active: forum_active_block
|
||||
new: forum_new_block
|
||||
# locale:
|
||||
# 0: language_block
|
||||
node:
|
||||
syndicate: node_syndicate_block
|
||||
search:
|
||||
form: search_form_block
|
||||
statistics:
|
||||
popular: statistics_popular_block
|
||||
system:
|
||||
main: system_main_block
|
||||
'powered-by': system_powered_by_block
|
||||
user:
|
||||
login: user_login_block
|
||||
# 1: system_menu_block:tools
|
||||
new: views_block:who_s_new-block_1
|
||||
online: views_block:who_s_online-who_s_online_block
|
||||
-
|
||||
plugin: block_plugin_id
|
||||
-
|
||||
plugin: skip_on_empty
|
||||
method: row
|
||||
theme:
|
||||
plugin: block_theme
|
||||
source:
|
||||
- theme
|
||||
- default_theme
|
||||
- admin_theme
|
||||
region:
|
||||
plugin: block_region
|
||||
source:
|
||||
- theme
|
||||
- '@theme'
|
||||
- region
|
||||
map:
|
||||
bartik:
|
||||
bartik:
|
||||
# Bartik 7.x --> Bartik 8.x
|
||||
featured: featured_top
|
||||
triptych_first: featured_bottom_first
|
||||
triptych_middle: featured_bottom_second
|
||||
triptych_last: featured_bottom_third
|
||||
footer_firstcolumn: footer_first
|
||||
footer_secondcolumn: footer_second
|
||||
footer_thirdcolumn: footer_third
|
||||
footer_fourthcolumn: footer_fourth
|
||||
footer: footer_fifth
|
||||
# If mapping fails, put the block in the content region.
|
||||
default_value: content
|
||||
weight: weight
|
||||
settings:
|
||||
plugin: block_settings
|
||||
source:
|
||||
- '@plugin'
|
||||
- delta
|
||||
- settings
|
||||
- title
|
||||
visibility:
|
||||
plugin: block_visibility
|
||||
source:
|
||||
- visibility
|
||||
- pages
|
||||
- roles
|
||||
# If the block uses PHP visibility, don't migrate it unless the PHP module
|
||||
# is enabled.
|
||||
skip_php: true
|
||||
destination:
|
||||
plugin: entity:block
|
||||
migration_dependencies:
|
||||
optional:
|
||||
- d7_custom_block
|
||||
- d7_user_role
|
171
web/core/modules/block/src/BlockAccessControlHandler.php
Normal file
171
web/core/modules/block/src/BlockAccessControlHandler.php
Normal file
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\ContextException;
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Drupal\Core\Condition\ConditionAccessResolverTrait;
|
||||
use Drupal\Core\Entity\EntityAccessControlHandler;
|
||||
use Drupal\Core\Entity\EntityHandlerInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Executable\ExecutableManagerInterface;
|
||||
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
|
||||
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
|
||||
use Drupal\Core\Plugin\ContextAwarePluginInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Defines the access control handler for the block entity type.
|
||||
*
|
||||
* @see \Drupal\block\Entity\Block
|
||||
*/
|
||||
class BlockAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
|
||||
|
||||
use ConditionAccessResolverTrait;
|
||||
|
||||
/**
|
||||
* The condition plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Executable\ExecutableManagerInterface
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* The plugin context handler.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
|
||||
*/
|
||||
protected $contextHandler;
|
||||
|
||||
/**
|
||||
* The context manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
|
||||
*/
|
||||
protected $contextRepository;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
||||
return new static(
|
||||
$entity_type,
|
||||
$container->get('plugin.manager.condition'),
|
||||
$container->get('context.handler'),
|
||||
$container->get('context.repository')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the block access control handler instance
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type definition.
|
||||
* @param \Drupal\Core\Executable\ExecutableManagerInterface $manager
|
||||
* The ConditionManager for checking visibility of blocks.
|
||||
* @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
|
||||
* The ContextHandler for applying contexts to conditions properly.
|
||||
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
|
||||
* The lazy context repository service.
|
||||
*/
|
||||
public function __construct(EntityTypeInterface $entity_type, ExecutableManagerInterface $manager, ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository ) {
|
||||
parent::__construct($entity_type);
|
||||
$this->manager = $manager;
|
||||
$this->contextHandler = $context_handler;
|
||||
$this->contextRepository = $context_repository;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
|
||||
/** @var \Drupal\block\BlockInterface $entity */
|
||||
if ($operation != 'view') {
|
||||
return parent::checkAccess($entity, $operation, $account);
|
||||
}
|
||||
|
||||
// Don't grant access to disabled blocks.
|
||||
if (!$entity->status()) {
|
||||
return AccessResult::forbidden()->addCacheableDependency($entity);
|
||||
}
|
||||
else {
|
||||
$conditions = [];
|
||||
$missing_context = FALSE;
|
||||
foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
|
||||
if ($condition instanceof ContextAwarePluginInterface) {
|
||||
try {
|
||||
$contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
|
||||
$this->contextHandler->applyContextMapping($condition, $contexts);
|
||||
}
|
||||
catch (ContextException $e) {
|
||||
$missing_context = TRUE;
|
||||
}
|
||||
}
|
||||
$conditions[$condition_id] = $condition;
|
||||
}
|
||||
|
||||
if ($missing_context) {
|
||||
// If any context is missing then we might be missing cacheable
|
||||
// metadata, and don't know based on what conditions the block is
|
||||
// accessible or not. For example, blocks that have a node type
|
||||
// condition will have a missing context on any non-node route like the
|
||||
// frontpage.
|
||||
// @todo Avoid setting max-age 0 for some or all cases, for example by
|
||||
// treating available contexts without value differently in
|
||||
// https://www.drupal.org/node/2521956.
|
||||
$access = AccessResult::forbidden()->setCacheMaxAge(0);
|
||||
}
|
||||
elseif ($this->resolveConditions($conditions, 'and') !== FALSE) {
|
||||
// Delegate to the plugin.
|
||||
$block_plugin = $entity->getPlugin();
|
||||
try {
|
||||
if ($block_plugin instanceof ContextAwarePluginInterface) {
|
||||
$contexts = $this->contextRepository->getRuntimeContexts(array_values($block_plugin->getContextMapping()));
|
||||
$this->contextHandler->applyContextMapping($block_plugin, $contexts);
|
||||
}
|
||||
$access = $block_plugin->access($account, TRUE);
|
||||
}
|
||||
catch (ContextException $e) {
|
||||
// Setting access to forbidden if any context is missing for the same
|
||||
// reasons as with conditions (described in the comment above).
|
||||
// @todo Avoid setting max-age 0 for some or all cases, for example by
|
||||
// treating available contexts without value differently in
|
||||
// https://www.drupal.org/node/2521956.
|
||||
$access = AccessResult::forbidden()->setCacheMaxAge(0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$access = AccessResult::forbidden();
|
||||
}
|
||||
|
||||
$this->mergeCacheabilityFromConditions($access, $conditions);
|
||||
|
||||
// Ensure that access is evaluated again when the block changes.
|
||||
return $access->addCacheableDependency($entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges cacheable metadata from conditions onto the access result object.
|
||||
*
|
||||
* @param \Drupal\Core\Access\AccessResult $access
|
||||
* The access result object.
|
||||
* @param \Drupal\Core\Condition\ConditionInterface[] $conditions
|
||||
* List of visibility conditions.
|
||||
*/
|
||||
protected function mergeCacheabilityFromConditions(AccessResult $access, array $conditions) {
|
||||
foreach ($conditions as $condition) {
|
||||
if ($condition instanceof CacheableDependencyInterface) {
|
||||
$access->addCacheTags($condition->getCacheTags());
|
||||
$access->addCacheContexts($condition->getCacheContexts());
|
||||
$access->setCacheMaxAge(Cache::mergeMaxAges($access->getCacheMaxAge(), $condition->getCacheMaxAge()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
447
web/core/modules/block/src/BlockForm.php
Normal file
447
web/core/modules/block/src/BlockForm.php
Normal file
|
@ -0,0 +1,447 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Plugin\PluginFormFactoryInterface;
|
||||
use Drupal\Core\Block\BlockPluginInterface;
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Executable\ExecutableManagerInterface;
|
||||
use Drupal\Core\Extension\ThemeHandlerInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\SubformState;
|
||||
use Drupal\Core\Language\LanguageManagerInterface;
|
||||
use Drupal\Core\Plugin\ContextAwarePluginInterface;
|
||||
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
|
||||
use Drupal\Core\Plugin\PluginWithFormsInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides form for block instance forms.
|
||||
*/
|
||||
class BlockForm extends EntityForm {
|
||||
|
||||
/**
|
||||
* The block entity.
|
||||
*
|
||||
* @var \Drupal\block\BlockInterface
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* The block storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* The condition plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Condition\ConditionManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* The event dispatcher service.
|
||||
*
|
||||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* The language manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface
|
||||
*/
|
||||
protected $language;
|
||||
|
||||
/**
|
||||
* The theme handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ThemeHandler
|
||||
*/
|
||||
protected $themeHandler;
|
||||
|
||||
/**
|
||||
* The context repository service.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
|
||||
*/
|
||||
protected $contextRepository;
|
||||
|
||||
/**
|
||||
* The plugin form manager.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\PluginFormFactoryInterface
|
||||
*/
|
||||
protected $pluginFormFactory;
|
||||
|
||||
/**
|
||||
* Constructs a BlockForm object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \Drupal\Core\Executable\ExecutableManagerInterface $manager
|
||||
* The ConditionManager for building the visibility UI.
|
||||
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
|
||||
* The lazy context repository service.
|
||||
* @param \Drupal\Core\Language\LanguageManagerInterface $language
|
||||
* The language manager.
|
||||
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
||||
* The theme handler.
|
||||
* @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
|
||||
* The plugin form manager.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler, PluginFormFactoryInterface $plugin_form_manager) {
|
||||
$this->storage = $entity_manager->getStorage('block');
|
||||
$this->manager = $manager;
|
||||
$this->contextRepository = $context_repository;
|
||||
$this->language = $language;
|
||||
$this->themeHandler = $theme_handler;
|
||||
$this->pluginFormFactory = $plugin_form_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager'),
|
||||
$container->get('plugin.manager.condition'),
|
||||
$container->get('context.repository'),
|
||||
$container->get('language_manager'),
|
||||
$container->get('theme_handler'),
|
||||
$container->get('plugin_form.factory')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$entity = $this->entity;
|
||||
|
||||
// Store theme settings in $form_state for use below.
|
||||
if (!$theme = $entity->getTheme()) {
|
||||
$theme = $this->config('system.theme')->get('default');
|
||||
}
|
||||
$form_state->set('block_theme', $theme);
|
||||
|
||||
// Store the gathered contexts in the form state for other objects to use
|
||||
// during form building.
|
||||
$form_state->setTemporaryValue('gathered_contexts', $this->contextRepository->getAvailableContexts());
|
||||
|
||||
$form['#tree'] = TRUE;
|
||||
$form['settings'] = [];
|
||||
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
|
||||
$form['settings'] = $this->getPluginForm($entity->getPlugin())->buildConfigurationForm($form['settings'], $subform_state);
|
||||
$form['visibility'] = $this->buildVisibilityInterface([], $form_state);
|
||||
|
||||
// If creating a new block, calculate a safe default machine name.
|
||||
$form['id'] = array(
|
||||
'#type' => 'machine_name',
|
||||
'#maxlength' => 64,
|
||||
'#description' => $this->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
|
||||
'#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity),
|
||||
'#machine_name' => array(
|
||||
'exists' => '\Drupal\block\Entity\Block::load',
|
||||
'replace_pattern' => '[^a-z0-9_.]+',
|
||||
'source' => array('settings', 'label'),
|
||||
),
|
||||
'#required' => TRUE,
|
||||
'#disabled' => !$entity->isNew(),
|
||||
);
|
||||
|
||||
// Theme settings.
|
||||
if ($entity->getTheme()) {
|
||||
$form['theme'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $theme,
|
||||
);
|
||||
}
|
||||
else {
|
||||
$theme_options = array();
|
||||
foreach ($this->themeHandler->listInfo() as $theme_name => $theme_info) {
|
||||
if (!empty($theme_info->status)) {
|
||||
$theme_options[$theme_name] = $theme_info->info['name'];
|
||||
}
|
||||
}
|
||||
$form['theme'] = array(
|
||||
'#type' => 'select',
|
||||
'#options' => $theme_options,
|
||||
'#title' => t('Theme'),
|
||||
'#default_value' => $theme,
|
||||
'#ajax' => array(
|
||||
'callback' => '::themeSwitch',
|
||||
'wrapper' => 'edit-block-region-wrapper',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Hidden weight setting.
|
||||
$weight = $entity->isNew() ? $this->getRequest()->query->get('weight', 0) : $entity->getWeight();
|
||||
$form['weight'] = array(
|
||||
'#type' => 'hidden',
|
||||
'#default_value' => $weight,
|
||||
);
|
||||
|
||||
// Region settings.
|
||||
$entity_region = $entity->getRegion();
|
||||
$region = $entity->isNew() ? $this->getRequest()->query->get('region', $entity_region) : $entity_region;
|
||||
$form['region'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Region'),
|
||||
'#description' => $this->t('Select the region where this block should be displayed.'),
|
||||
'#default_value' => $region,
|
||||
'#empty_value' => BlockInterface::BLOCK_REGION_NONE,
|
||||
'#options' => system_region_list($theme, REGIONS_VISIBLE),
|
||||
'#prefix' => '<div id="edit-block-region-wrapper">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
$form['#attached']['library'][] = 'block/drupal.block.admin';
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles switching the available regions based on the selected theme.
|
||||
*/
|
||||
public function themeSwitch($form, FormStateInterface $form_state) {
|
||||
$form['region']['#options'] = system_region_list($form_state->getValue('theme'), REGIONS_VISIBLE);
|
||||
return $form['region'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for building the visibility UI form.
|
||||
*
|
||||
* @param array $form
|
||||
* An associative array containing the structure of the form.
|
||||
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
||||
* The current state of the form.
|
||||
*
|
||||
* @return array
|
||||
* The form array with the visibility UI added in.
|
||||
*/
|
||||
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
|
||||
$form['visibility_tabs'] = [
|
||||
'#type' => 'vertical_tabs',
|
||||
'#title' => $this->t('Visibility'),
|
||||
'#parents' => ['visibility_tabs'],
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'block/drupal.block',
|
||||
],
|
||||
],
|
||||
];
|
||||
// @todo Allow list of conditions to be configured in
|
||||
// https://www.drupal.org/node/2284687.
|
||||
$visibility = $this->entity->getVisibility();
|
||||
foreach ($this->manager->getDefinitionsForContexts($form_state->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) {
|
||||
// Don't display the current theme condition.
|
||||
if ($condition_id == 'current_theme') {
|
||||
continue;
|
||||
}
|
||||
// Don't display the language condition until we have multiple languages.
|
||||
if ($condition_id == 'language' && !$this->language->isMultilingual()) {
|
||||
continue;
|
||||
}
|
||||
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
|
||||
$condition = $this->manager->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []);
|
||||
$form_state->set(['conditions', $condition_id], $condition);
|
||||
$condition_form = $condition->buildConfigurationForm([], $form_state);
|
||||
$condition_form['#type'] = 'details';
|
||||
$condition_form['#title'] = $condition->getPluginDefinition()['label'];
|
||||
$condition_form['#group'] = 'visibility_tabs';
|
||||
$form[$condition_id] = $condition_form;
|
||||
}
|
||||
|
||||
if (isset($form['node_type'])) {
|
||||
$form['node_type']['#title'] = $this->t('Content types');
|
||||
$form['node_type']['bundles']['#title'] = $this->t('Content types');
|
||||
$form['node_type']['negate']['#type'] = 'value';
|
||||
$form['node_type']['negate']['#title_display'] = 'invisible';
|
||||
$form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
|
||||
}
|
||||
if (isset($form['user_role'])) {
|
||||
$form['user_role']['#title'] = $this->t('Roles');
|
||||
unset($form['user_role']['roles']['#description']);
|
||||
$form['user_role']['negate']['#type'] = 'value';
|
||||
$form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value'];
|
||||
}
|
||||
if (isset($form['request_path'])) {
|
||||
$form['request_path']['#title'] = $this->t('Pages');
|
||||
$form['request_path']['negate']['#type'] = 'radios';
|
||||
$form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
|
||||
$form['request_path']['negate']['#title_display'] = 'invisible';
|
||||
$form['request_path']['negate']['#options'] = [
|
||||
$this->t('Show for the listed pages'),
|
||||
$this->t('Hide for the listed pages'),
|
||||
];
|
||||
}
|
||||
if (isset($form['language'])) {
|
||||
$form['language']['negate']['#type'] = 'value';
|
||||
$form['language']['negate']['#value'] = $form['language']['negate']['#default_value'];
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function actions(array $form, FormStateInterface $form_state) {
|
||||
$actions = parent::actions($form, $form_state);
|
||||
$actions['submit']['#value'] = $this->t('Save block');
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::validateForm($form, $form_state);
|
||||
|
||||
$form_state->setValue('weight', (int) $form_state->getValue('weight'));
|
||||
// The Block Entity form puts all block plugin form elements in the
|
||||
// settings form element, so just pass that to the block for validation.
|
||||
$this->getPluginForm($this->entity->getPlugin())->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
|
||||
$this->validateVisibility($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to independently validate the visibility UI.
|
||||
*
|
||||
* @param array $form
|
||||
* A nested array form elements comprising the form.
|
||||
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
||||
* The current state of the form.
|
||||
*/
|
||||
protected function validateVisibility(array $form, FormStateInterface $form_state) {
|
||||
// Validate visibility condition settings.
|
||||
foreach ($form_state->getValue('visibility') as $condition_id => $values) {
|
||||
// All condition plugins use 'negate' as a Boolean in their schema.
|
||||
// However, certain form elements may return it as 0/1. Cast here to
|
||||
// ensure the data is in the expected type.
|
||||
if (array_key_exists('negate', $values)) {
|
||||
$form_state->setValue(['visibility', $condition_id, 'negate'], (bool) $values['negate']);
|
||||
}
|
||||
|
||||
// Allow the condition to validate the form.
|
||||
$condition = $form_state->get(['conditions', $condition_id]);
|
||||
$condition->validateConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitForm($form, $form_state);
|
||||
|
||||
$entity = $this->entity;
|
||||
// The Block Entity form puts all block plugin form elements in the
|
||||
// settings form element, so just pass that to the block for submission.
|
||||
$sub_form_state = SubformState::createForSubform($form['settings'], $form, $form_state);
|
||||
// Call the plugin submit handler.
|
||||
$block = $entity->getPlugin();
|
||||
$this->getPluginForm($block)->submitConfigurationForm($form, $sub_form_state);
|
||||
// If this block is context-aware, set the context mapping.
|
||||
if ($block instanceof ContextAwarePluginInterface && $block->getContextDefinitions()) {
|
||||
$context_mapping = $sub_form_state->getValue('context_mapping', []);
|
||||
$block->setContextMapping($context_mapping);
|
||||
}
|
||||
|
||||
$this->submitVisibility($form, $form_state);
|
||||
|
||||
// Save the settings of the plugin.
|
||||
$entity->save();
|
||||
|
||||
drupal_set_message($this->t('The block configuration has been saved.'));
|
||||
$form_state->setRedirect(
|
||||
'block.admin_display_theme',
|
||||
array(
|
||||
'theme' => $form_state->getValue('theme'),
|
||||
),
|
||||
array('query' => array('block-placement' => Html::getClass($this->entity->id())))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to independently submit the visibility UI.
|
||||
*
|
||||
* @param array $form
|
||||
* A nested array form elements comprising the form.
|
||||
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
||||
* The current state of the form.
|
||||
*/
|
||||
protected function submitVisibility(array $form, FormStateInterface $form_state) {
|
||||
foreach ($form_state->getValue('visibility') as $condition_id => $values) {
|
||||
// Allow the condition to submit the form.
|
||||
$condition = $form_state->get(['conditions', $condition_id]);
|
||||
$condition->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state));
|
||||
|
||||
// Setting conditions' context mappings is the plugins' responsibility.
|
||||
// This code exists for backwards compatibility, because
|
||||
// \Drupal\Core\Condition\ConditionPluginBase::submitConfigurationForm()
|
||||
// did not set its own mappings until Drupal 8.2
|
||||
// @todo Remove the code that sets context mappings in Drupal 9.0.0.
|
||||
if ($condition instanceof ContextAwarePluginInterface) {
|
||||
$context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : [];
|
||||
$condition->setContextMapping($context_mapping);
|
||||
}
|
||||
|
||||
$condition_configuration = $condition->getConfiguration();
|
||||
// Update the visibility conditions on the block.
|
||||
$this->entity->getVisibilityConditions()->addInstanceId($condition_id, $condition_configuration);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique machine name for a block.
|
||||
*
|
||||
* @param \Drupal\block\BlockInterface $block
|
||||
* The block entity.
|
||||
*
|
||||
* @return string
|
||||
* Returns the unique name.
|
||||
*/
|
||||
public function getUniqueMachineName(BlockInterface $block) {
|
||||
$suggestion = $block->getPlugin()->getMachineNameSuggestion();
|
||||
|
||||
// Get all the blocks which starts with the suggested machine name.
|
||||
$query = $this->storage->getQuery();
|
||||
$query->condition('id', $suggestion, 'CONTAINS');
|
||||
$block_ids = $query->execute();
|
||||
|
||||
$block_ids = array_map(function ($block_id) {
|
||||
$parts = explode('.', $block_id);
|
||||
return end($parts);
|
||||
}, $block_ids);
|
||||
|
||||
// Iterate through potential IDs until we get a new one. E.g.
|
||||
// 'plugin', 'plugin_2', 'plugin_3', etc.
|
||||
$count = 1;
|
||||
$machine_default = $suggestion;
|
||||
while (in_array($machine_default, $block_ids)) {
|
||||
$machine_default = $suggestion . '_' . ++$count;
|
||||
}
|
||||
return $machine_default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the plugin form for a given block and operation.
|
||||
*
|
||||
* @param \Drupal\Core\Block\BlockPluginInterface $block
|
||||
* The block plugin.
|
||||
*
|
||||
* @return \Drupal\Core\Plugin\PluginFormInterface
|
||||
* The plugin form for the block.
|
||||
*/
|
||||
protected function getPluginForm(BlockPluginInterface $block) {
|
||||
if ($block instanceof PluginWithFormsInterface) {
|
||||
return $this->pluginFormFactory->createInstance($block, 'configure');
|
||||
}
|
||||
return $block;
|
||||
}
|
||||
|
||||
}
|
135
web/core/modules/block/src/BlockInterface.php
Normal file
135
web/core/modules/block/src/BlockInterface.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a block entity.
|
||||
*/
|
||||
interface BlockInterface extends ConfigEntityInterface {
|
||||
|
||||
/**
|
||||
* Indicates the block label (title) should be displayed to end users.
|
||||
*/
|
||||
const BLOCK_LABEL_VISIBLE = 'visible';
|
||||
|
||||
/**
|
||||
* Denotes that a block is not enabled in any region and should not be shown.
|
||||
*/
|
||||
const BLOCK_REGION_NONE = -1;
|
||||
|
||||
/**
|
||||
* Returns the plugin instance.
|
||||
*
|
||||
* @return \Drupal\Core\Block\BlockPluginInterface
|
||||
* The plugin instance for this block.
|
||||
*/
|
||||
public function getPlugin();
|
||||
|
||||
/**
|
||||
* Returns the plugin ID.
|
||||
*
|
||||
* @return string
|
||||
* The plugin ID for this block.
|
||||
*/
|
||||
public function getPluginId();
|
||||
|
||||
/**
|
||||
* Returns the region this block is placed in.
|
||||
*
|
||||
* @return string
|
||||
* The region this block is placed in.
|
||||
*/
|
||||
public function getRegion();
|
||||
|
||||
/**
|
||||
* Returns the theme ID.
|
||||
*
|
||||
* @return string
|
||||
* The theme ID for this block instance.
|
||||
*/
|
||||
public function getTheme();
|
||||
|
||||
/**
|
||||
* Returns an array of visibility condition configurations.
|
||||
*
|
||||
* @return array
|
||||
* An array of visibility condition configuration keyed by the condition ID.
|
||||
*/
|
||||
public function getVisibility();
|
||||
|
||||
/**
|
||||
* Gets conditions for this block.
|
||||
*
|
||||
* @return \Drupal\Core\Condition\ConditionInterface[]|\Drupal\Core\Condition\ConditionPluginCollection
|
||||
* An array or collection of configured condition plugins.
|
||||
*/
|
||||
public function getVisibilityConditions();
|
||||
|
||||
/**
|
||||
* Gets a visibility condition plugin instance.
|
||||
*
|
||||
* @param string $instance_id
|
||||
* The condition plugin instance ID.
|
||||
*
|
||||
* @return \Drupal\Core\Condition\ConditionInterface
|
||||
* A condition plugin.
|
||||
*/
|
||||
public function getVisibilityCondition($instance_id);
|
||||
|
||||
/**
|
||||
* Sets the visibility condition configuration.
|
||||
*
|
||||
* @param string $instance_id
|
||||
* The condition instance ID.
|
||||
* @param array $configuration
|
||||
* The condition configuration.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setVisibilityConfig($instance_id, array $configuration);
|
||||
|
||||
/**
|
||||
* Returns the weight of this block (used for sorting).
|
||||
*
|
||||
* @return int
|
||||
* The block weight.
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Sets the region this block is placed in.
|
||||
*
|
||||
* @param string $region
|
||||
* The region to place this block in.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRegion($region);
|
||||
|
||||
/**
|
||||
* Sets the block weight.
|
||||
*
|
||||
* @param int $weight
|
||||
* The desired weight.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setWeight($weight);
|
||||
|
||||
/**
|
||||
* Creates a duplicate of the block entity.
|
||||
*
|
||||
* @param string $new_id
|
||||
* (optional) The new ID on the duplicate block.
|
||||
* @param string $new_theme
|
||||
* (optional) The theme on the duplicate block.
|
||||
*
|
||||
* @return static
|
||||
* A clone of $this with all identifiers unset, so saving it inserts a new
|
||||
* entity into the storage system.
|
||||
*/
|
||||
public function createDuplicateBlock($new_id = NULL, $new_theme = NULL);
|
||||
|
||||
}
|
385
web/core/modules/block/src/BlockListBuilder.php
Normal file
385
web/core/modules/block/src/BlockListBuilder.php
Normal file
|
@ -0,0 +1,385 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Form\FormBuilderInterface;
|
||||
use Drupal\Core\Form\FormInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Theme\ThemeManagerInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Defines a class to build a listing of block entities.
|
||||
*
|
||||
* @see \Drupal\block\Entity\Block
|
||||
*/
|
||||
class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface {
|
||||
|
||||
/**
|
||||
* The theme containing the blocks.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $theme;
|
||||
|
||||
/**
|
||||
* The current request.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* The theme manager.
|
||||
*
|
||||
* @var \Drupal\Core\Theme\ThemeManagerInterface
|
||||
*/
|
||||
protected $themeManager;
|
||||
|
||||
/**
|
||||
* The form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormBuilderInterface
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $limit = FALSE;
|
||||
|
||||
/**
|
||||
* Constructs a new BlockListBuilder object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type definition.
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
|
||||
* The entity storage class.
|
||||
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
|
||||
* The theme manager.
|
||||
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
|
||||
* The form builder.
|
||||
*/
|
||||
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ThemeManagerInterface $theme_manager, FormBuilderInterface $form_builder) {
|
||||
parent::__construct($entity_type, $storage);
|
||||
|
||||
$this->themeManager = $theme_manager;
|
||||
$this->formBuilder = $form_builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
||||
return new static(
|
||||
$entity_type,
|
||||
$container->get('entity.manager')->getStorage($entity_type->id()),
|
||||
$container->get('theme.manager'),
|
||||
$container->get('form_builder')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param string|null $theme
|
||||
* (optional) The theme to display the blocks for. If NULL, the current
|
||||
* theme will be used.
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
*
|
||||
* @return array
|
||||
* The block list as a renderable array.
|
||||
*/
|
||||
public function render($theme = NULL, Request $request = NULL) {
|
||||
$this->request = $request;
|
||||
$this->theme = $theme;
|
||||
|
||||
return $this->formBuilder->getForm($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'block_admin_display_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['#attached']['library'][] = 'core/drupal.tableheader';
|
||||
$form['#attached']['library'][] = 'block/drupal.block';
|
||||
$form['#attached']['library'][] = 'block/drupal.block.admin';
|
||||
$form['#attributes']['class'][] = 'clearfix';
|
||||
|
||||
// Build the form tree.
|
||||
$form['blocks'] = $this->buildBlocksForm();
|
||||
|
||||
$form['actions'] = array(
|
||||
'#tree' => FALSE,
|
||||
'#type' => 'actions',
|
||||
);
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save blocks'),
|
||||
'#button_type' => 'primary',
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the main "Blocks" portion of the form.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildBlocksForm() {
|
||||
// Build blocks first for each region.
|
||||
$blocks = [];
|
||||
$entities = $this->load();
|
||||
/** @var \Drupal\block\BlockInterface[] $entities */
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
$definition = $entity->getPlugin()->getPluginDefinition();
|
||||
$blocks[$entity->getRegion()][$entity_id] = array(
|
||||
'label' => $entity->label(),
|
||||
'entity_id' => $entity_id,
|
||||
'weight' => $entity->getWeight(),
|
||||
'entity' => $entity,
|
||||
'category' => $definition['category'],
|
||||
);
|
||||
}
|
||||
|
||||
$form = array(
|
||||
'#type' => 'table',
|
||||
'#header' => array(
|
||||
$this->t('Block'),
|
||||
$this->t('Category'),
|
||||
$this->t('Region'),
|
||||
$this->t('Weight'),
|
||||
$this->t('Operations'),
|
||||
),
|
||||
'#attributes' => array(
|
||||
'id' => 'blocks',
|
||||
),
|
||||
);
|
||||
|
||||
// Weights range from -delta to +delta, so delta should be at least half
|
||||
// of the amount of blocks present. This makes sure all blocks in the same
|
||||
// region get an unique weight.
|
||||
$weight_delta = round(count($entities) / 2);
|
||||
|
||||
$placement = FALSE;
|
||||
if ($this->request->query->has('block-placement')) {
|
||||
$placement = $this->request->query->get('block-placement');
|
||||
$form['#attached']['drupalSettings']['blockPlacement'] = $placement;
|
||||
}
|
||||
|
||||
// Loop over each region and build blocks.
|
||||
$regions = $this->systemRegionList($this->getThemeName(), REGIONS_VISIBLE);
|
||||
$block_regions_with_disabled = $regions + array(BlockInterface::BLOCK_REGION_NONE => $this->t('Disabled', array(), array('context' => 'Plural')));
|
||||
foreach ($block_regions_with_disabled as $region => $title) {
|
||||
$form['#tabledrag'][] = array(
|
||||
'action' => 'match',
|
||||
'relationship' => 'sibling',
|
||||
'group' => 'block-region-select',
|
||||
'subgroup' => 'block-region-' . $region,
|
||||
'hidden' => FALSE,
|
||||
);
|
||||
$form['#tabledrag'][] = array(
|
||||
'action' => 'order',
|
||||
'relationship' => 'sibling',
|
||||
'group' => 'block-weight',
|
||||
'subgroup' => 'block-weight-' . $region,
|
||||
);
|
||||
|
||||
$form['region-' . $region] = array(
|
||||
'#attributes' => array(
|
||||
'class' => array('region-title', 'region-title-' . $region),
|
||||
'no_striping' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['region-' . $region]['title'] = array(
|
||||
'#theme_wrappers' => array(
|
||||
'container' => array(
|
||||
'#attributes' => array('class' => 'region-title__action'),
|
||||
)
|
||||
),
|
||||
'#prefix' => $region != BlockInterface::BLOCK_REGION_NONE ? $title : $block_regions_with_disabled[$region],
|
||||
'#type' => 'link',
|
||||
'#title' => $this->t('Place block <span class="visually-hidden">in the %region region</span>', ['%region' => $block_regions_with_disabled[$region]]),
|
||||
'#url' => Url::fromRoute('block.admin_library', ['theme' => $this->getThemeName()], ['query' => ['region' => $region]]),
|
||||
'#wrapper_attributes' => array(
|
||||
'colspan' => 5,
|
||||
),
|
||||
'#attributes' => [
|
||||
'class' => ['use-ajax', 'button', 'button--small'],
|
||||
'data-dialog-type' => 'modal',
|
||||
'data-dialog-options' => Json::encode([
|
||||
'width' => 700,
|
||||
]),
|
||||
],
|
||||
);
|
||||
|
||||
$form['region-' . $region . '-message'] = array(
|
||||
'#attributes' => array(
|
||||
'class' => array(
|
||||
'region-message',
|
||||
'region-' . $region . '-message',
|
||||
empty($blocks[$region]) ? 'region-empty' : 'region-populated',
|
||||
),
|
||||
),
|
||||
);
|
||||
$form['region-' . $region . '-message']['message'] = array(
|
||||
'#markup' => '<em>' . $this->t('No blocks in this region') . '</em>',
|
||||
'#wrapper_attributes' => array(
|
||||
'colspan' => 5,
|
||||
),
|
||||
);
|
||||
|
||||
if (isset($blocks[$region])) {
|
||||
foreach ($blocks[$region] as $info) {
|
||||
$entity_id = $info['entity_id'];
|
||||
|
||||
$form[$entity_id] = array(
|
||||
'#attributes' => array(
|
||||
'class' => array('draggable'),
|
||||
),
|
||||
);
|
||||
if ($placement && $placement == Html::getClass($entity_id)) {
|
||||
$form[$entity_id]['#attributes']['class'][] = 'color-success';
|
||||
$form[$entity_id]['#attributes']['class'][] = 'js-block-placed';
|
||||
}
|
||||
$form[$entity_id]['info'] = array(
|
||||
'#plain_text' => $info['label'],
|
||||
'#wrapper_attributes' => array(
|
||||
'class' => array('block'),
|
||||
),
|
||||
);
|
||||
$form[$entity_id]['type'] = array(
|
||||
'#markup' => $info['category'],
|
||||
);
|
||||
$form[$entity_id]['region-theme']['region'] = array(
|
||||
'#type' => 'select',
|
||||
'#default_value' => $region,
|
||||
'#empty_value' => BlockInterface::BLOCK_REGION_NONE,
|
||||
'#title' => $this->t('Region for @block block', array('@block' => $info['label'])),
|
||||
'#title_display' => 'invisible',
|
||||
'#options' => $regions,
|
||||
'#attributes' => array(
|
||||
'class' => array('block-region-select', 'block-region-' . $region),
|
||||
),
|
||||
'#parents' => array('blocks', $entity_id, 'region'),
|
||||
);
|
||||
$form[$entity_id]['region-theme']['theme'] = array(
|
||||
'#type' => 'hidden',
|
||||
'#value' => $this->getThemeName(),
|
||||
'#parents' => array('blocks', $entity_id, 'theme'),
|
||||
);
|
||||
$form[$entity_id]['weight'] = array(
|
||||
'#type' => 'weight',
|
||||
'#default_value' => $info['weight'],
|
||||
'#delta' => $weight_delta,
|
||||
'#title' => $this->t('Weight for @block block', array('@block' => $info['label'])),
|
||||
'#title_display' => 'invisible',
|
||||
'#attributes' => array(
|
||||
'class' => array('block-weight', 'block-weight-' . $region),
|
||||
),
|
||||
);
|
||||
$form[$entity_id]['operations'] = $this->buildOperations($info['entity']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Do not allow disabling the main system content block when it is present.
|
||||
if (isset($form['system_main']['region'])) {
|
||||
$form['system_main']['region']['#required'] = TRUE;
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the theme used for this block listing.
|
||||
*
|
||||
* @return string
|
||||
* The name of the theme.
|
||||
*/
|
||||
protected function getThemeName() {
|
||||
// If no theme was specified, use the current theme.
|
||||
if (!$this->theme) {
|
||||
$this->theme = $this->themeManager->getActiveTheme()->getName();
|
||||
}
|
||||
return $this->theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEntityIds() {
|
||||
return $this->getStorage()->getQuery()
|
||||
->condition('theme', $this->getThemeName())
|
||||
->sort($this->entityType->getKey('id'))
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultOperations(EntityInterface $entity) {
|
||||
$operations = parent::getDefaultOperations($entity);
|
||||
|
||||
if (isset($operations['edit'])) {
|
||||
$operations['edit']['title'] = $this->t('Configure');
|
||||
}
|
||||
|
||||
return $operations;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
// No validation.
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$entities = $this->storage->loadMultiple(array_keys($form_state->getValue('blocks')));
|
||||
/** @var \Drupal\block\BlockInterface[] $entities */
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
$entity_values = $form_state->getValue(array('blocks', $entity_id));
|
||||
$entity->setWeight($entity_values['weight']);
|
||||
$entity->setRegion($entity_values['region']);
|
||||
if ($entity->getRegion() == BlockInterface::BLOCK_REGION_NONE) {
|
||||
$entity->disable();
|
||||
}
|
||||
else {
|
||||
$entity->enable();
|
||||
}
|
||||
$entity->save();
|
||||
}
|
||||
drupal_set_message(t('The block settings have been updated.'));
|
||||
|
||||
// Remove any previously set block placement.
|
||||
$this->request->query->remove('block-placement');
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps system_region_list().
|
||||
*/
|
||||
protected function systemRegionList($theme, $show = REGIONS_ALL) {
|
||||
return system_region_list($theme, $show);
|
||||
}
|
||||
|
||||
}
|
70
web/core/modules/block/src/BlockPluginCollection.php
Normal file
70
web/core/modules/block/src/BlockPluginCollection.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\PluginException;
|
||||
use Drupal\Component\Plugin\PluginManagerInterface;
|
||||
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
|
||||
|
||||
/**
|
||||
* Provides a collection of block plugins.
|
||||
*/
|
||||
class BlockPluginCollection extends DefaultSingleLazyPluginCollection {
|
||||
|
||||
/**
|
||||
* The block ID this plugin collection belongs to.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $blockId;
|
||||
|
||||
/**
|
||||
* Constructs a new BlockPluginCollection.
|
||||
*
|
||||
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
|
||||
* The manager to be used for instantiating plugins.
|
||||
* @param string $instance_id
|
||||
* The ID of the plugin instance.
|
||||
* @param array $configuration
|
||||
* An array of configuration.
|
||||
* @param string $block_id
|
||||
* The unique ID of the block entity using this plugin.
|
||||
*/
|
||||
public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $block_id) {
|
||||
parent::__construct($manager, $instance_id, $configuration);
|
||||
|
||||
$this->blockId = $block_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return \Drupal\Core\Block\BlockPluginInterface
|
||||
*/
|
||||
public function &get($instance_id) {
|
||||
return parent::get($instance_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function initializePlugin($instance_id) {
|
||||
if (!$instance_id) {
|
||||
throw new PluginException("The block '{$this->blockId}' did not specify a plugin.");
|
||||
}
|
||||
|
||||
try {
|
||||
parent::initializePlugin($instance_id);
|
||||
}
|
||||
catch (PluginException $e) {
|
||||
$module = $this->configuration['provider'];
|
||||
// Ignore blocks belonging to uninstalled modules, but re-throw valid
|
||||
// exceptions when the module is installed and the plugin is
|
||||
// misconfigured.
|
||||
if (!$module || \Drupal::moduleHandler()->moduleExists($module)) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
81
web/core/modules/block/src/BlockRepository.php
Normal file
81
web/core/modules/block/src/BlockRepository.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
|
||||
use Drupal\Core\Theme\ThemeManagerInterface;
|
||||
|
||||
/**
|
||||
* Provides a repository for Block config entities.
|
||||
*/
|
||||
class BlockRepository implements BlockRepositoryInterface {
|
||||
|
||||
/**
|
||||
* The block storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $blockStorage;
|
||||
|
||||
/**
|
||||
* The theme manager.
|
||||
*
|
||||
* @var \Drupal\Core\Theme\ThemeManagerInterface
|
||||
*/
|
||||
protected $themeManager;
|
||||
|
||||
/**
|
||||
* Constructs a new BlockRepository.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
|
||||
* The theme manager.
|
||||
* @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
|
||||
* The plugin context handler.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, ThemeManagerInterface $theme_manager, ContextHandlerInterface $context_handler) {
|
||||
$this->blockStorage = $entity_manager->getStorage('block');
|
||||
$this->themeManager = $theme_manager;
|
||||
$this->contextHandler = $context_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []) {
|
||||
$active_theme = $this->themeManager->getActiveTheme();
|
||||
// Build an array of the region names in the right order.
|
||||
$empty = array_fill_keys($active_theme->getRegions(), array());
|
||||
|
||||
$full = array();
|
||||
foreach ($this->blockStorage->loadByProperties(array('theme' => $active_theme->getName())) as $block_id => $block) {
|
||||
/** @var \Drupal\block\BlockInterface $block */
|
||||
$access = $block->access('view', NULL, TRUE);
|
||||
$region = $block->getRegion();
|
||||
if (!isset($cacheable_metadata[$region])) {
|
||||
$cacheable_metadata[$region] = CacheableMetadata::createFromObject($access);
|
||||
}
|
||||
else {
|
||||
$cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access));
|
||||
}
|
||||
|
||||
// Set the contexts on the block before checking access.
|
||||
if ($access->isAllowed()) {
|
||||
$full[$region][$block_id] = $block;
|
||||
}
|
||||
}
|
||||
|
||||
// Merge it with the actual values to maintain the region ordering.
|
||||
$assignments = array_intersect_key(array_merge($empty, $full), $empty);
|
||||
foreach ($assignments as &$assignment) {
|
||||
// Suppress errors because PHPUnit will indirectly modify the contents,
|
||||
// triggering https://bugs.php.net/bug.php?id=50688.
|
||||
@uasort($assignment, 'Drupal\block\Entity\Block::sort');
|
||||
}
|
||||
return $assignments;
|
||||
}
|
||||
|
||||
}
|
20
web/core/modules/block/src/BlockRepositoryInterface.php
Normal file
20
web/core/modules/block/src/BlockRepositoryInterface.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
interface BlockRepositoryInterface {
|
||||
|
||||
/**
|
||||
* Returns an array of regions and their block entities.
|
||||
*
|
||||
* @param \Drupal\Core\Cache\CacheableMetadata[] $cacheable_metadata
|
||||
* (optional) List of CacheableMetadata objects, keyed by region. This is
|
||||
* by reference and is used to pass this information back to the caller.
|
||||
*
|
||||
* @return array
|
||||
* The array is first keyed by region machine name, with the values
|
||||
* containing an array keyed by block ID, with block entities as the values.
|
||||
*/
|
||||
public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []);
|
||||
|
||||
}
|
251
web/core/modules/block/src/BlockViewBuilder.php
Normal file
251
web/core/modules/block/src/BlockViewBuilder.php
Normal file
|
@ -0,0 +1,251 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Core\Block\MainContentBlockPluginInterface;
|
||||
use Drupal\Core\Block\TitleBlockPluginInterface;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Entity\EntityViewBuilder;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Language\LanguageManagerInterface;
|
||||
use Drupal\Core\Plugin\ContextAwarePluginInterface;
|
||||
use Drupal\Core\Render\Element;
|
||||
use Drupal\block\Entity\Block;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a Block view builder.
|
||||
*/
|
||||
class BlockViewBuilder extends EntityViewBuilder {
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* Constructs a new BlockViewBuilder.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type definition.
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager service.
|
||||
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
||||
* The language manager.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
*/
|
||||
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct($entity_type, $entity_manager, $language_manager);
|
||||
$this->moduleHandler = $module_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
||||
return new static(
|
||||
$entity_type,
|
||||
$container->get('entity.manager'),
|
||||
$container->get('language_manager'),
|
||||
$container->get('module_handler')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
|
||||
$build = $this->viewMultiple(array($entity), $view_mode, $langcode);
|
||||
return reset($build);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
|
||||
/** @var \Drupal\block\BlockInterface[] $entities */
|
||||
$build = array();
|
||||
foreach ($entities as $entity) {
|
||||
$entity_id = $entity->id();
|
||||
$plugin = $entity->getPlugin();
|
||||
|
||||
$cache_tags = Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags());
|
||||
$cache_tags = Cache::mergeTags($cache_tags, $plugin->getCacheTags());
|
||||
|
||||
// Create the render array for the block as a whole.
|
||||
// @see template_preprocess_block().
|
||||
$build[$entity_id] = array(
|
||||
'#cache' => [
|
||||
'keys' => ['entity_view', 'block', $entity->id()],
|
||||
'contexts' => Cache::mergeContexts(
|
||||
$entity->getCacheContexts(),
|
||||
$plugin->getCacheContexts()
|
||||
),
|
||||
'tags' => $cache_tags,
|
||||
'max-age' => $plugin->getCacheMaxAge(),
|
||||
],
|
||||
'#weight' => $entity->getWeight(),
|
||||
);
|
||||
|
||||
// Allow altering of cacheability metadata or setting #create_placeholder.
|
||||
$this->moduleHandler->alter(['block_build', "block_build_" . $plugin->getBaseId()], $build[$entity_id], $plugin);
|
||||
|
||||
if ($plugin instanceof MainContentBlockPluginInterface || $plugin instanceof TitleBlockPluginInterface) {
|
||||
// Immediately build a #pre_render-able block, since this block cannot
|
||||
// be built lazily.
|
||||
$build[$entity_id] += static::buildPreRenderableBlock($entity, $this->moduleHandler());
|
||||
}
|
||||
else {
|
||||
// Assign a #lazy_builder callback, which will generate a #pre_render-
|
||||
// able block lazily (when necessary).
|
||||
$build[$entity_id] += [
|
||||
'#lazy_builder' => [static::class . '::lazyBuilder', [$entity_id, $view_mode, $langcode]],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a #pre_render-able block render array.
|
||||
*
|
||||
* @param \Drupal\block\BlockInterface $entity
|
||||
* A block config entity.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler service.
|
||||
*
|
||||
* @return array
|
||||
* A render array with a #pre_render callback to render the block.
|
||||
*/
|
||||
protected static function buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler) {
|
||||
$plugin = $entity->getPlugin();
|
||||
$plugin_id = $plugin->getPluginId();
|
||||
$base_id = $plugin->getBaseId();
|
||||
$derivative_id = $plugin->getDerivativeId();
|
||||
$configuration = $plugin->getConfiguration();
|
||||
|
||||
// Inject runtime contexts.
|
||||
if ($plugin instanceof ContextAwarePluginInterface) {
|
||||
$contexts = \Drupal::service('context.repository')->getRuntimeContexts($plugin->getContextMapping());
|
||||
\Drupal::service('context.handler')->applyContextMapping($plugin, $contexts);
|
||||
}
|
||||
|
||||
// Create the render array for the block as a whole.
|
||||
// @see template_preprocess_block().
|
||||
$build = [
|
||||
'#theme' => 'block',
|
||||
'#attributes' => [],
|
||||
// All blocks get a "Configure block" contextual link.
|
||||
'#contextual_links' => [
|
||||
'block' => [
|
||||
'route_parameters' => ['block' => $entity->id()],
|
||||
],
|
||||
],
|
||||
'#weight' => $entity->getWeight(),
|
||||
'#configuration' => $configuration,
|
||||
'#plugin_id' => $plugin_id,
|
||||
'#base_plugin_id' => $base_id,
|
||||
'#derivative_plugin_id' => $derivative_id,
|
||||
'#id' => $entity->id(),
|
||||
'#pre_render' => [
|
||||
static::class . '::preRender',
|
||||
],
|
||||
// Add the entity so that it can be used in the #pre_render method.
|
||||
'#block' => $entity,
|
||||
];
|
||||
|
||||
// If an alter hook wants to modify the block contents, it can append
|
||||
// another #pre_render hook.
|
||||
$module_handler->alter(['block_view', "block_view_$base_id"], $build, $plugin);
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* #lazy_builder callback; builds a #pre_render-able block.
|
||||
*
|
||||
* @param $entity_id
|
||||
* A block config entity ID.
|
||||
* @param $view_mode
|
||||
* The view mode the block is being viewed in.
|
||||
*
|
||||
* @return array
|
||||
* A render array with a #pre_render callback to render the block.
|
||||
*/
|
||||
public static function lazyBuilder($entity_id, $view_mode) {
|
||||
return static::buildPreRenderableBlock(Block::load($entity_id), \Drupal::service('module_handler'));
|
||||
}
|
||||
|
||||
/**
|
||||
* #pre_render callback for building a block.
|
||||
*
|
||||
* Renders the content using the provided block plugin, and then:
|
||||
* - if there is no content, aborts rendering, and makes sure the block won't
|
||||
* be rendered.
|
||||
* - if there is content, moves the contextual links from the block content to
|
||||
* the block itself.
|
||||
*/
|
||||
public static function preRender($build) {
|
||||
$content = $build['#block']->getPlugin()->build();
|
||||
// Remove the block entity from the render array, to ensure that blocks
|
||||
// can be rendered without the block config entity.
|
||||
unset($build['#block']);
|
||||
if ($content !== NULL && !Element::isEmpty($content)) {
|
||||
// Place the $content returned by the block plugin into a 'content' child
|
||||
// element, as a way to allow the plugin to have complete control of its
|
||||
// properties and rendering (for instance, its own #theme) without
|
||||
// conflicting with the properties used above, or alternate ones used by
|
||||
// alternate block rendering approaches in contrib (for instance, Panels).
|
||||
// However, the use of a child element is an implementation detail of this
|
||||
// particular block rendering approach. Semantically, the content returned
|
||||
// by the plugin "is the" block, and in particular, #attributes and
|
||||
// #contextual_links is information about the *entire* block. Therefore,
|
||||
// we must move these properties from $content and merge them into the
|
||||
// top-level element.
|
||||
foreach (array('#attributes', '#contextual_links') as $property) {
|
||||
if (isset($content[$property])) {
|
||||
$build[$property] += $content[$property];
|
||||
unset($content[$property]);
|
||||
}
|
||||
}
|
||||
$build['content'] = $content;
|
||||
}
|
||||
// Either the block's content is completely empty, or it consists only of
|
||||
// cacheability metadata.
|
||||
else {
|
||||
// Abort rendering: render as the empty string and ensure this block is
|
||||
// render cached, so we can avoid the work of having to repeatedly
|
||||
// determine whether the block is empty. For instance, modifying or adding
|
||||
// entities could cause the block to no longer be empty.
|
||||
$build = array(
|
||||
'#markup' => '',
|
||||
'#cache' => $build['#cache'],
|
||||
);
|
||||
// If $content is not empty, then it contains cacheability metadata, and
|
||||
// we must merge it with the existing cacheability metadata. This allows
|
||||
// blocks to be empty, yet still bubble cacheability metadata, to indicate
|
||||
// why they are empty.
|
||||
if (!empty($content)) {
|
||||
CacheableMetadata::createFromRenderArray($build)
|
||||
->merge(CacheableMetadata::createFromRenderArray($content))
|
||||
->applyTo($build);
|
||||
}
|
||||
}
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
30
web/core/modules/block/src/Controller/BlockAddController.php
Normal file
30
web/core/modules/block/src/Controller/BlockAddController.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
|
||||
/**
|
||||
* Controller for building the block instance add form.
|
||||
*/
|
||||
class BlockAddController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Build the block instance add form.
|
||||
*
|
||||
* @param string $plugin_id
|
||||
* The plugin ID for the block instance.
|
||||
* @param string $theme
|
||||
* The name of the theme for the block instance.
|
||||
*
|
||||
* @return array
|
||||
* The block instance edit form.
|
||||
*/
|
||||
public function blockAddConfigureForm($plugin_id, $theme) {
|
||||
// Create a block entity.
|
||||
$entity = $this->entityManager()->getStorage('block')->create(array('plugin' => $plugin_id, 'theme' => $theme));
|
||||
|
||||
return $this->entityFormBuilder()->getForm($entity);
|
||||
}
|
||||
|
||||
}
|
100
web/core/modules/block/src/Controller/BlockController.php
Normal file
100
web/core/modules/block/src/Controller/BlockController.php
Normal file
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Controller;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\Extension\ThemeHandlerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Controller routines for admin block routes.
|
||||
*/
|
||||
class BlockController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The theme handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ThemeHandlerInterface
|
||||
*/
|
||||
protected $themeHandler;
|
||||
|
||||
/**
|
||||
* Constructs a new BlockController instance.
|
||||
*
|
||||
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
||||
* The theme handler.
|
||||
*/
|
||||
public function __construct(ThemeHandlerInterface $theme_handler) {
|
||||
$this->themeHandler = $theme_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('theme_handler')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a block theme demo page.
|
||||
*
|
||||
* @param string $theme
|
||||
* The name of the theme.
|
||||
*
|
||||
* @return array
|
||||
* A #type 'page' render array containing the block region demo.
|
||||
*/
|
||||
public function demo($theme) {
|
||||
if (!$this->themeHandler->hasUi($theme)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$page = [
|
||||
'#title' => Html::escape($this->themeHandler->getName($theme)),
|
||||
'#type' => 'page',
|
||||
'#attached' => array(
|
||||
'drupalSettings' => [
|
||||
// The block demonstration page is not marked as an administrative
|
||||
// page by \Drupal::service('router.admin_context')->isAdminRoute()
|
||||
// function in order to use the frontend theme. Since JavaScript
|
||||
// relies on a proper separation of admin pages, it needs to know this
|
||||
// is an actual administrative page.
|
||||
'path' => ['currentPathIsAdmin' => TRUE],
|
||||
],
|
||||
'library' => array(
|
||||
'block/drupal.block.admin',
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
// Show descriptions in each visible page region, nothing else.
|
||||
$visible_regions = $this->getVisibleRegionNames($theme);
|
||||
foreach (array_keys($visible_regions) as $region) {
|
||||
$page[$region]['block_description'] = array(
|
||||
'#type' => 'inline_template',
|
||||
'#template' => '<div class="block-region demo-block">{{ region_name }}</div>',
|
||||
'#context' => array('region_name' => $visible_regions[$region]),
|
||||
);
|
||||
}
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the human-readable list of regions keyed by machine name.
|
||||
*
|
||||
* @param string $theme
|
||||
* The name of the theme.
|
||||
*
|
||||
* @return array
|
||||
* An array of human-readable region names keyed by machine name.
|
||||
*/
|
||||
protected function getVisibleRegionNames($theme) {
|
||||
return system_region_list($theme, REGIONS_VISIBLE);
|
||||
}
|
||||
|
||||
}
|
191
web/core/modules/block/src/Controller/BlockLibraryController.php
Normal file
191
web/core/modules/block/src/Controller/BlockLibraryController.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Controller;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Core\Block\BlockManagerInterface;
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
||||
use Drupal\Core\Menu\LocalActionManagerInterface;
|
||||
use Drupal\Core\Plugin\Context\LazyContextRepository;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Provides a list of block plugins to be added to the layout.
|
||||
*/
|
||||
class BlockLibraryController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The block manager.
|
||||
*
|
||||
* @var \Drupal\Core\Block\BlockManagerInterface
|
||||
*/
|
||||
protected $blockManager;
|
||||
|
||||
/**
|
||||
* The context repository.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\Context\LazyContextRepository
|
||||
*/
|
||||
protected $contextRepository;
|
||||
|
||||
/**
|
||||
* The route match.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\RouteMatchInterface
|
||||
*/
|
||||
protected $routeMatch;
|
||||
|
||||
/**
|
||||
* The local action manager.
|
||||
*
|
||||
* @var \Drupal\Core\Menu\LocalActionManagerInterface
|
||||
*/
|
||||
protected $localActionManager;
|
||||
|
||||
/**
|
||||
* Constructs a BlockLibraryController object.
|
||||
*
|
||||
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
|
||||
* The block manager.
|
||||
* @param \Drupal\Core\Plugin\Context\LazyContextRepository $context_repository
|
||||
* The context repository.
|
||||
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
||||
* The current route match.
|
||||
* @param \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager
|
||||
* The local action manager.
|
||||
*/
|
||||
public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, RouteMatchInterface $route_match, LocalActionManagerInterface $local_action_manager) {
|
||||
$this->blockManager = $block_manager;
|
||||
$this->routeMatch = $route_match;
|
||||
$this->localActionManager = $local_action_manager;
|
||||
$this->contextRepository = $context_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.block'),
|
||||
$container->get('context.repository'),
|
||||
$container->get('current_route_match'),
|
||||
$container->get('plugin.manager.menu.local_action')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a list of blocks that can be added to a theme's layout.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
* @param string $theme
|
||||
* Theme key of the block list.
|
||||
*
|
||||
* @return array
|
||||
* A render array as expected by the renderer.
|
||||
*/
|
||||
public function listBlocks(Request $request, $theme) {
|
||||
// Since modals do not render any other part of the page, we need to render
|
||||
// them manually as part of this listing.
|
||||
if ($request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT) === 'drupal_modal') {
|
||||
$build['local_actions'] = $this->buildLocalActions();
|
||||
}
|
||||
|
||||
$headers = [
|
||||
['data' => $this->t('Block')],
|
||||
['data' => $this->t('Category')],
|
||||
['data' => $this->t('Operations')],
|
||||
];
|
||||
|
||||
// Only add blocks which work without any available context.
|
||||
$definitions = $this->blockManager->getDefinitionsForContexts($this->contextRepository->getAvailableContexts());
|
||||
// Order by category, and then by admin label.
|
||||
$definitions = $this->blockManager->getSortedDefinitions($definitions);
|
||||
|
||||
$region = $request->query->get('region');
|
||||
$weight = $request->query->get('weight');
|
||||
$rows = [];
|
||||
foreach ($definitions as $plugin_id => $plugin_definition) {
|
||||
$row = [];
|
||||
$row['title']['data'] = [
|
||||
'#type' => 'inline_template',
|
||||
'#template' => '<div class="block-filter-text-source">{{ label }}</div>',
|
||||
'#context' => [
|
||||
'label' => $plugin_definition['admin_label'],
|
||||
],
|
||||
];
|
||||
$row['category']['data'] = $plugin_definition['category'];
|
||||
$links['add'] = [
|
||||
'title' => $this->t('Place block'),
|
||||
'url' => Url::fromRoute('block.admin_add', ['plugin_id' => $plugin_id, 'theme' => $theme]),
|
||||
'attributes' => [
|
||||
'class' => ['use-ajax'],
|
||||
'data-dialog-type' => 'modal',
|
||||
'data-dialog-options' => Json::encode([
|
||||
'width' => 700,
|
||||
]),
|
||||
],
|
||||
];
|
||||
if ($region) {
|
||||
$links['add']['query']['region'] = $region;
|
||||
}
|
||||
if (isset($weight)) {
|
||||
$links['add']['query']['weight'] = $weight;
|
||||
}
|
||||
$row['operations']['data'] = [
|
||||
'#type' => 'operations',
|
||||
'#links' => $links,
|
||||
];
|
||||
$rows[] = $row;
|
||||
}
|
||||
|
||||
$build['#attached']['library'][] = 'block/drupal.block.admin';
|
||||
|
||||
$build['filter'] = [
|
||||
'#type' => 'search',
|
||||
'#title' => $this->t('Filter'),
|
||||
'#title_display' => 'invisible',
|
||||
'#size' => 30,
|
||||
'#placeholder' => $this->t('Filter by block name'),
|
||||
'#attributes' => [
|
||||
'class' => ['block-filter-text'],
|
||||
'data-element' => '.block-add-table',
|
||||
'title' => $this->t('Enter a part of the block name to filter by.'),
|
||||
],
|
||||
];
|
||||
|
||||
$build['blocks'] = [
|
||||
'#type' => 'table',
|
||||
'#header' => $headers,
|
||||
'#rows' => $rows,
|
||||
'#empty' => $this->t('No blocks available.'),
|
||||
'#attributes' => [
|
||||
'class' => ['block-add-table'],
|
||||
],
|
||||
];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the local actions for this listing.
|
||||
*
|
||||
* @return array
|
||||
* An array of local actions for this listing.
|
||||
*/
|
||||
protected function buildLocalActions() {
|
||||
$build = $this->localActionManager->getActionsForRoute($this->routeMatch->getRouteName());
|
||||
// Without this workaround, the action links will be rendered as <li> with
|
||||
// no wrapping <ul> element.
|
||||
if (!empty($build)) {
|
||||
$build['#prefix'] = '<ul class="action-links">';
|
||||
$build['#suffix'] = '</ul>';
|
||||
}
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Controller;
|
||||
|
||||
use Drupal\Core\Entity\Controller\EntityListController;
|
||||
use Drupal\Core\Extension\ThemeHandlerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Defines a controller to list blocks.
|
||||
*/
|
||||
class BlockListController extends EntityListController {
|
||||
|
||||
/**
|
||||
* The theme handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ThemeHandlerInterface
|
||||
*/
|
||||
protected $themeHandler;
|
||||
|
||||
/**
|
||||
* Constructs the BlockListController.
|
||||
*
|
||||
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
||||
* The theme handler.
|
||||
*/
|
||||
public function __construct(ThemeHandlerInterface $theme_handler) {
|
||||
$this->themeHandler = $theme_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('theme_handler')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the block administration page.
|
||||
*
|
||||
* @param string|null $theme
|
||||
* Theme key of block list.
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
*
|
||||
* @return array
|
||||
* A render array as expected by drupal_render().
|
||||
*/
|
||||
public function listing($theme = NULL, Request $request = NULL) {
|
||||
$theme = $theme ?: $this->config('system.theme')->get('default');
|
||||
if (!$this->themeHandler->hasUi($theme)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
return $this->entityManager()->getListBuilder('block')->render($theme, $request);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Controller;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Block\BlockManagerInterface;
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Returns autocomplete responses for block categories.
|
||||
*/
|
||||
class CategoryAutocompleteController implements ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* The block manager.
|
||||
*
|
||||
* @var \Drupal\Core\Block\BlockManagerInterface
|
||||
*/
|
||||
protected $blockManager;
|
||||
|
||||
/**
|
||||
* Constructs a new CategoryAutocompleteController.
|
||||
*
|
||||
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
|
||||
* The block manager.
|
||||
*/
|
||||
public function __construct(BlockManagerInterface $block_manager) {
|
||||
$this->blockManager = $block_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.block')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves suggestions for block category autocompletion.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
||||
* A JSON response containing autocomplete suggestions.
|
||||
*/
|
||||
public function autocomplete(Request $request) {
|
||||
$typed_category = $request->query->get('q');
|
||||
$matches = array();
|
||||
foreach ($this->blockManager->getCategories() as $category) {
|
||||
if (stripos($category, $typed_category) === 0) {
|
||||
$matches[] = array('value' => $category, 'label' => Html::escape($category));
|
||||
}
|
||||
}
|
||||
return new JsonResponse($matches);
|
||||
}
|
||||
|
||||
}
|
330
web/core/modules/block/src/Entity/Block.php
Normal file
330
web/core/modules/block/src/Entity/Block.php
Normal file
|
@ -0,0 +1,330 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Entity;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Condition\ConditionPluginCollection;
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\block\BlockPluginCollection;
|
||||
use Drupal\block\BlockInterface;
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
|
||||
/**
|
||||
* Defines a Block configuration entity class.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "block",
|
||||
* label = @Translation("Block"),
|
||||
* handlers = {
|
||||
* "access" = "Drupal\block\BlockAccessControlHandler",
|
||||
* "view_builder" = "Drupal\block\BlockViewBuilder",
|
||||
* "list_builder" = "Drupal\block\BlockListBuilder",
|
||||
* "form" = {
|
||||
* "default" = "Drupal\block\BlockForm",
|
||||
* "delete" = "Drupal\block\Form\BlockDeleteForm"
|
||||
* }
|
||||
* },
|
||||
* admin_permission = "administer blocks",
|
||||
* entity_keys = {
|
||||
* "id" = "id"
|
||||
* },
|
||||
* links = {
|
||||
* "delete-form" = "/admin/structure/block/manage/{block}/delete",
|
||||
* "edit-form" = "/admin/structure/block/manage/{block}"
|
||||
* },
|
||||
* config_export = {
|
||||
* "id",
|
||||
* "theme",
|
||||
* "region",
|
||||
* "weight",
|
||||
* "provider",
|
||||
* "plugin",
|
||||
* "settings",
|
||||
* "visibility",
|
||||
* },
|
||||
* lookup_keys = {
|
||||
* "theme"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
|
||||
|
||||
/**
|
||||
* The ID of the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The plugin instance settings.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $settings = array();
|
||||
|
||||
/**
|
||||
* The region this block is placed in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $region = self::BLOCK_REGION_NONE;
|
||||
|
||||
/**
|
||||
* The block weight.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $weight;
|
||||
|
||||
/**
|
||||
* The plugin instance ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $plugin;
|
||||
|
||||
/**
|
||||
* The visibility settings for this block.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $visibility = [];
|
||||
|
||||
/**
|
||||
* The plugin collection that holds the block plugin for this entity.
|
||||
*
|
||||
* @var \Drupal\block\BlockPluginCollection
|
||||
*/
|
||||
protected $pluginCollection;
|
||||
|
||||
/**
|
||||
* The available contexts for this block and its visibility conditions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $contexts = [];
|
||||
|
||||
/**
|
||||
* The visibility collection.
|
||||
*
|
||||
* @var \Drupal\Core\Condition\ConditionPluginCollection
|
||||
*/
|
||||
protected $visibilityCollection;
|
||||
|
||||
/**
|
||||
* The condition plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Executable\ExecutableManagerInterface
|
||||
*/
|
||||
protected $conditionPluginManager;
|
||||
|
||||
/**
|
||||
* The theme that includes the block plugin for this entity.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $theme;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPlugin() {
|
||||
return $this->getPluginCollection()->get($this->plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates the creation of the block's LazyPluginCollection.
|
||||
*
|
||||
* @return \Drupal\Component\Plugin\LazyPluginCollection
|
||||
* The block's plugin collection.
|
||||
*/
|
||||
protected function getPluginCollection() {
|
||||
if (!$this->pluginCollection) {
|
||||
$this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
|
||||
}
|
||||
return $this->pluginCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginCollections() {
|
||||
return [
|
||||
'settings' => $this->getPluginCollection(),
|
||||
'visibility' => $this->getVisibilityConditions(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginId() {
|
||||
return $this->plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRegion() {
|
||||
return $this->region;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTheme() {
|
||||
return $this->theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function label() {
|
||||
$settings = $this->get('settings');
|
||||
if ($settings['label']) {
|
||||
return $settings['label'];
|
||||
}
|
||||
else {
|
||||
$definition = $this->getPlugin()->getPluginDefinition();
|
||||
return $definition['admin_label'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts active blocks by weight; sorts inactive blocks by name.
|
||||
*/
|
||||
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||
// Separate enabled from disabled.
|
||||
$status = (int) $b->status() - (int) $a->status();
|
||||
if ($status !== 0) {
|
||||
return $status;
|
||||
}
|
||||
// Sort by weight, unless disabled.
|
||||
if ($a->getRegion() != static::BLOCK_REGION_NONE) {
|
||||
$weight = $a->getWeight() - $b->getWeight();
|
||||
if ($weight) {
|
||||
return $weight;
|
||||
}
|
||||
}
|
||||
// Sort by label.
|
||||
return strcmp($a->label(), $b->label());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
parent::calculateDependencies();
|
||||
$this->addDependency('theme', $this->theme);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
// Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
|
||||
// handles the regular cases. The Block entity has one special case: a
|
||||
// newly created block may *also* appear on any page in the current theme,
|
||||
// so we must invalidate the associated block's cache tag (which includes
|
||||
// the theme cache tag).
|
||||
if (!$update) {
|
||||
Cache::invalidateTags($this->getCacheTagsToInvalidate());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getVisibility() {
|
||||
return $this->getVisibilityConditions()->getConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setVisibilityConfig($instance_id, array $configuration) {
|
||||
$conditions = $this->getVisibilityConditions();
|
||||
if (!$conditions->has($instance_id)) {
|
||||
$configuration['id'] = $instance_id;
|
||||
$conditions->addInstanceId($instance_id, $configuration);
|
||||
}
|
||||
else {
|
||||
$conditions->setInstanceConfiguration($instance_id, $configuration);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getVisibilityConditions() {
|
||||
if (!isset($this->visibilityCollection)) {
|
||||
$this->visibilityCollection = new ConditionPluginCollection($this->conditionPluginManager(), $this->get('visibility'));
|
||||
}
|
||||
return $this->visibilityCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getVisibilityCondition($instance_id) {
|
||||
return $this->getVisibilityConditions()->get($instance_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the condition plugin manager.
|
||||
*
|
||||
* @return \Drupal\Core\Executable\ExecutableManagerInterface
|
||||
* The condition plugin manager.
|
||||
*/
|
||||
protected function conditionPluginManager() {
|
||||
if (!isset($this->conditionPluginManager)) {
|
||||
$this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
|
||||
}
|
||||
return $this->conditionPluginManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setRegion($region) {
|
||||
$this->region = $region;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setWeight($weight) {
|
||||
$this->weight = $weight;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
|
||||
$duplicate = parent::createDuplicate();
|
||||
if (!empty($new_id)) {
|
||||
$duplicate->id = $new_id;
|
||||
}
|
||||
if (!empty($new_theme)) {
|
||||
$duplicate->theme = $new_theme;
|
||||
}
|
||||
return $duplicate;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
|
||||
use Drupal\Core\Render\RenderEvents;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Selects the block page display variant.
|
||||
*
|
||||
* @see \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
|
||||
*/
|
||||
class BlockPageDisplayVariantSubscriber implements EventSubscriberInterface {
|
||||
|
||||
/**
|
||||
* Selects the block page display variant.
|
||||
*
|
||||
* @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event) {
|
||||
$event->setPluginId('block_page');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
static function getSubscribedEvents() {
|
||||
$events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = array('onSelectPageDisplayVariant');
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
20
web/core/modules/block/src/Form/BlockDeleteForm.php
Normal file
20
web/core/modules/block/src/Form/BlockDeleteForm.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Form;
|
||||
|
||||
use Drupal\Core\Entity\EntityDeleteForm;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Provides a deletion confirmation form for the block instance deletion form.
|
||||
*/
|
||||
class BlockDeleteForm extends EntityDeleteForm {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
return new Url('block.admin_display');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\Derivative;
|
||||
|
||||
use Drupal\Component\Plugin\Derivative\DeriverBase;
|
||||
use Drupal\Core\Extension\ThemeHandlerInterface;
|
||||
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides dynamic tabs based on active themes.
|
||||
*/
|
||||
class ThemeLocalTask extends DeriverBase implements ContainerDeriverInterface {
|
||||
|
||||
/**
|
||||
* The theme handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ThemeHandlerInterface
|
||||
*/
|
||||
protected $themeHandler;
|
||||
|
||||
/**
|
||||
* Constructs a new ThemeLocalTask.
|
||||
*
|
||||
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
||||
* The theme handler.
|
||||
*/
|
||||
public function __construct(ThemeHandlerInterface $theme_handler) {
|
||||
$this->themeHandler = $theme_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, $base_plugin_id) {
|
||||
return new static(
|
||||
$container->get('theme_handler')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinitions($base_plugin_definition) {
|
||||
$default_theme = $this->themeHandler->getDefault();
|
||||
|
||||
foreach ($this->themeHandler->listInfo() as $theme_name => $theme) {
|
||||
if ($this->themeHandler->hasUi($theme_name)) {
|
||||
$this->derivatives[$theme_name] = $base_plugin_definition;
|
||||
$this->derivatives[$theme_name]['title'] = $theme->info['name'];
|
||||
$this->derivatives[$theme_name]['route_parameters'] = array('theme' => $theme_name);
|
||||
}
|
||||
// Default task!
|
||||
if ($default_theme == $theme_name) {
|
||||
$this->derivatives[$theme_name]['route_name'] = $base_plugin_definition['parent_id'];
|
||||
// Emulate default logic because without the base plugin id we can't
|
||||
// change the base_route.
|
||||
$this->derivatives[$theme_name]['weight'] = -10;
|
||||
|
||||
unset($this->derivatives[$theme_name]['route_parameters']);
|
||||
}
|
||||
}
|
||||
return $this->derivatives;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,203 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\DisplayVariant;
|
||||
|
||||
use Drupal\block\BlockRepositoryInterface;
|
||||
use Drupal\Core\Block\MainContentBlockPluginInterface;
|
||||
use Drupal\Core\Block\TitleBlockPluginInterface;
|
||||
use Drupal\Core\Block\MessagesBlockPluginInterface;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Display\PageVariantInterface;
|
||||
use Drupal\Core\Entity\EntityViewBuilderInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Display\VariantBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a page display variant that decorates the main content with blocks.
|
||||
*
|
||||
* To ensure essential information is displayed, each essential part of a page
|
||||
* has a corresponding block plugin interface, so that BlockPageVariant can
|
||||
* automatically provide a fallback in case no block for each of these
|
||||
* interfaces is placed.
|
||||
*
|
||||
* @see \Drupal\Core\Block\MainContentBlockPluginInterface
|
||||
* @see \Drupal\Core\Block\MessagesBlockPluginInterface
|
||||
*
|
||||
* @PageDisplayVariant(
|
||||
* id = "block_page",
|
||||
* admin_label = @Translation("Page with blocks")
|
||||
* )
|
||||
*/
|
||||
class BlockPageVariant extends VariantBase implements PageVariantInterface, ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The block repository.
|
||||
*
|
||||
* @var \Drupal\block\BlockRepositoryInterface
|
||||
*/
|
||||
protected $blockRepository;
|
||||
|
||||
/**
|
||||
* The block view builder.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
|
||||
*/
|
||||
protected $blockViewBuilder;
|
||||
|
||||
/**
|
||||
* The Block entity type list cache tags.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $blockListCacheTags;
|
||||
|
||||
/**
|
||||
* The render array representing the main page content.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $mainContent = [];
|
||||
|
||||
/**
|
||||
* The page title: a string (plain title) or a render array (formatted title).
|
||||
*
|
||||
* @var string|array
|
||||
*/
|
||||
protected $title = '';
|
||||
|
||||
/**
|
||||
* Constructs a new BlockPageVariant.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin ID for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\block\BlockRepositoryInterface $block_repository
|
||||
* The block repository.
|
||||
* @param \Drupal\Core\Entity\EntityViewBuilderInterface $block_view_builder
|
||||
* The block view builder.
|
||||
* @param string[] $block_list_cache_tags
|
||||
* The Block entity type list cache tags.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, array $block_list_cache_tags) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->blockRepository = $block_repository;
|
||||
$this->blockViewBuilder = $block_view_builder;
|
||||
$this->blockListCacheTags = $block_list_cache_tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('block.repository'),
|
||||
$container->get('entity.manager')->getViewBuilder('block'),
|
||||
$container->get('entity.manager')->getDefinition('block')->getListCacheTags()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMainContent(array $main_content) {
|
||||
$this->mainContent = $main_content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setTitle($title) {
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
// Track whether blocks showing the main content and messages are displayed.
|
||||
$main_content_block_displayed = FALSE;
|
||||
$messages_block_displayed = FALSE;
|
||||
|
||||
$build = [
|
||||
'#cache' => [
|
||||
'tags' => $this->blockListCacheTags,
|
||||
],
|
||||
];
|
||||
// Load all region content assigned via blocks.
|
||||
$cacheable_metadata_list = [];
|
||||
foreach ($this->blockRepository->getVisibleBlocksPerRegion($cacheable_metadata_list) as $region => $blocks) {
|
||||
/** @var $blocks \Drupal\block\BlockInterface[] */
|
||||
foreach ($blocks as $key => $block) {
|
||||
$block_plugin = $block->getPlugin();
|
||||
if ($block_plugin instanceof MainContentBlockPluginInterface) {
|
||||
$block_plugin->setMainContent($this->mainContent);
|
||||
$main_content_block_displayed = TRUE;
|
||||
}
|
||||
elseif ($block_plugin instanceof TitleBlockPluginInterface) {
|
||||
$block_plugin->setTitle($this->title);
|
||||
}
|
||||
elseif ($block_plugin instanceof MessagesBlockPluginInterface) {
|
||||
$messages_block_displayed = TRUE;
|
||||
}
|
||||
$build[$region][$key] = $this->blockViewBuilder->view($block);
|
||||
|
||||
// The main content block cannot be cached: it is a placeholder for the
|
||||
// render array returned by the controller. It should be rendered as-is,
|
||||
// with other placed blocks "decorating" it. Analogous reasoning for the
|
||||
// title block.
|
||||
if ($block_plugin instanceof MainContentBlockPluginInterface || $block_plugin instanceof TitleBlockPluginInterface) {
|
||||
unset($build[$region][$key]['#cache']['keys']);
|
||||
}
|
||||
}
|
||||
if (!empty($build[$region])) {
|
||||
// \Drupal\block\BlockRepositoryInterface::getVisibleBlocksPerRegion()
|
||||
// returns the blocks in sorted order.
|
||||
$build[$region]['#sorted'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// If no block that shows the main content is displayed, still show the main
|
||||
// content. Otherwise the end user will see all displayed blocks, but not
|
||||
// the main content they came for.
|
||||
if (!$main_content_block_displayed) {
|
||||
$build['content']['system_main'] = $this->mainContent;
|
||||
}
|
||||
|
||||
// If no block displays status messages, still render them.
|
||||
if (!$messages_block_displayed) {
|
||||
$build['content']['messages'] = [
|
||||
'#weight' => -1000,
|
||||
'#type' => 'status_messages',
|
||||
];
|
||||
}
|
||||
|
||||
// If any render arrays are manually placed, render arrays and blocks must
|
||||
// be sorted.
|
||||
if (!$main_content_block_displayed || !$messages_block_displayed) {
|
||||
unset($build['content']['#sorted']);
|
||||
}
|
||||
|
||||
// The access results' cacheability is currently added to the top level of the
|
||||
// render array. This is done to prevent issues with empty regions being
|
||||
// displayed.
|
||||
// This would need to be changed to allow caching of block regions, as each
|
||||
// region must then have the relevant cacheable metadata.
|
||||
$merged_cacheable_metadata = CacheableMetadata::createFromRenderArray($build);
|
||||
foreach ($cacheable_metadata_list as $cacheable_metadata) {
|
||||
$merged_cacheable_metadata = $merged_cacheable_metadata->merge($cacheable_metadata);
|
||||
}
|
||||
$merged_cacheable_metadata->applyTo($build);
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\destination;
|
||||
|
||||
use Drupal\migrate\Plugin\migrate\destination\EntityConfigBase;
|
||||
use Drupal\migrate\Row;
|
||||
|
||||
/**
|
||||
* @MigrateDestination(
|
||||
* id = "entity:block"
|
||||
* )
|
||||
*/
|
||||
class EntityBlock extends EntityConfigBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEntityId(Row $row) {
|
||||
// Try to find the block by its plugin ID and theme.
|
||||
$properties = array(
|
||||
'plugin' => $row->getDestinationProperty('plugin'),
|
||||
'theme' => $row->getDestinationProperty('theme'),
|
||||
);
|
||||
$blocks = array_keys($this->storage->loadByProperties($properties));
|
||||
return reset($blocks);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\process;
|
||||
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\Plugin\MigrateProcessInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "block_plugin_id"
|
||||
* )
|
||||
*/
|
||||
class BlockPluginId extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The migration process plugin, configured for lookups in d6_custom_block
|
||||
* and d7_custom_block.
|
||||
*
|
||||
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
|
||||
*/
|
||||
protected $migrationPlugin;
|
||||
|
||||
/**
|
||||
* The block_content entity storage handler.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $blockContentStorage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $storage, MigrateProcessInterface $migration_plugin) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->blockContentStorage = $storage;
|
||||
$this->migrationPlugin = $migration_plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||
$entity_manager = $container->get('entity.manager');
|
||||
$migration_configuration = array(
|
||||
'migration' => array(
|
||||
'd6_custom_block',
|
||||
'd7_custom_block',
|
||||
),
|
||||
);
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$entity_manager->getDefinition('block_content') ? $entity_manager->getStorage('block_content') : NULL,
|
||||
$container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Set the block plugin id.
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
if (is_array($value)) {
|
||||
list($module, $delta) = $value;
|
||||
switch ($module) {
|
||||
case 'aggregator':
|
||||
list($type, $id) = explode('-', $delta);
|
||||
if ($type == 'feed') {
|
||||
return 'aggregator_feed_block';
|
||||
}
|
||||
break;
|
||||
case 'menu':
|
||||
return "system_menu_block:$delta";
|
||||
case 'block':
|
||||
if ($this->blockContentStorage) {
|
||||
$block_id = $this->migrationPlugin
|
||||
->transform($delta, $migrate_executable, $row, $destination_property);
|
||||
if ($block_id) {
|
||||
return 'block_content:' . $this->blockContentStorage->load($block_id)->uuid();
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\process;
|
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\Plugin\migrate\process\StaticMap;
|
||||
use Drupal\migrate\Row;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "block_region"
|
||||
* )
|
||||
*/
|
||||
class BlockRegion extends StaticMap implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* List of regions, keyed by theme.
|
||||
*
|
||||
* @var array[]
|
||||
*/
|
||||
protected $regions;
|
||||
|
||||
/**
|
||||
* Constructs a BlockRegion plugin instance.
|
||||
*
|
||||
* @param array $configuration
|
||||
* The plugin configuration.
|
||||
* @param string $plugin_id
|
||||
* The plugin ID.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin definition.
|
||||
* @param array $regions
|
||||
* Array of region maps, keyed by theme.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $regions) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->regions = $regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
$regions = array();
|
||||
foreach ($container->get('theme_handler')->listInfo() as $key => $theme) {
|
||||
$regions[$key] = $theme->info['regions'];
|
||||
}
|
||||
return new static($configuration, $plugin_id, $plugin_definition, $regions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
// Set the destination region, based on the source region and theme as well
|
||||
// as the current destination default theme.
|
||||
list($source_theme, $destination_theme, $region) = $value;
|
||||
|
||||
// Theme is the same on both source and destination, so ensure that the
|
||||
// region exists in the destination theme.
|
||||
if (strtolower($source_theme) == strtolower($destination_theme)) {
|
||||
if (isset($this->regions[$destination_theme][$region])) {
|
||||
return $region;
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to static mapping.
|
||||
return parent::transform($value, $migrate_executable, $row, $destination_property);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\process;
|
||||
|
||||
use Drupal\block\BlockInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "block_settings"
|
||||
* )
|
||||
*/
|
||||
class BlockSettings extends ProcessPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Set the block configuration.
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
list($plugin, $delta, $old_settings, $title) = $value;
|
||||
$settings = array();
|
||||
$settings['label'] = $title;
|
||||
if ($title) {
|
||||
$settings['label_display'] = BlockInterface::BLOCK_LABEL_VISIBLE;
|
||||
}
|
||||
else {
|
||||
$settings['label_display'] = '0';
|
||||
}
|
||||
switch ($plugin) {
|
||||
case 'aggregator_feed_block':
|
||||
list(, $id) = explode('-', $delta);
|
||||
$settings['block_count'] = $old_settings['aggregator']['item_count'];
|
||||
$settings['feed'] = $id;
|
||||
break;
|
||||
case 'book_navigation':
|
||||
$settings['block_mode'] = $old_settings['book']['block_mode'];
|
||||
break;
|
||||
case 'forum_active_block':
|
||||
case 'forum_new_block':
|
||||
$settings['block_count'] = $old_settings['forum']['block_num'];
|
||||
break;
|
||||
case 'statistics_popular_block':
|
||||
$settings['top_day_num'] = $old_settings['statistics']['statistics_block_top_day_num'];
|
||||
$settings['top_all_num'] = $old_settings['statistics']['statistics_block_top_all_num'];
|
||||
$settings['top_last_num'] = $old_settings['statistics']['statistics_block_top_last_num'];
|
||||
break;
|
||||
case 'views_block:who_s_new-block_1':
|
||||
$settings['items_per_page'] = $old_settings['user']['block_whois_new_count'];
|
||||
break;
|
||||
case 'views_block:who_s_online-who_s_online_block':
|
||||
$settings['items_per_page'] = $old_settings['user']['max_list_count'];
|
||||
break;
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\process;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\Core\Config\Config;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "block_theme"
|
||||
* )
|
||||
*/
|
||||
class BlockTheme extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* Contains the configuration object factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* Contains the system.theme configuration object.
|
||||
*
|
||||
* @var \Drupal\Core\Config\Config
|
||||
*/
|
||||
protected $themeConfig;
|
||||
|
||||
/**
|
||||
* Constructs a BlockTheme object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin ID for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
|
||||
* The migration entity.
|
||||
* @param \Drupal\Core\Config\Config $theme_config
|
||||
* The system.theme configuration factory object.
|
||||
* @param array $themes
|
||||
* The list of themes available on the destination.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Config $theme_config, array $themes) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
|
||||
$this->themeConfig = $theme_config;
|
||||
$this->themes = $themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$migration,
|
||||
$container->get('config.factory')->get('system.theme'),
|
||||
$container->get('theme_handler')->listInfo()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
list($theme, $default_theme, $admin_theme) = $value;
|
||||
|
||||
// If the source theme exists on the destination, we're good.
|
||||
if (isset($this->themes[$theme])) {
|
||||
return $theme;
|
||||
}
|
||||
|
||||
// If the source block is assigned to a region in the source default theme,
|
||||
// then assign it to the destination default theme.
|
||||
if (strtolower($theme) == strtolower($default_theme)) {
|
||||
return $this->themeConfig->get('default');
|
||||
}
|
||||
|
||||
// If the source block is assigned to a region in the source admin theme,
|
||||
// then assign it to the destination admin theme.
|
||||
if (strtolower($theme) == strtolower($admin_theme)) {
|
||||
return $this->themeConfig->get('admin');
|
||||
}
|
||||
|
||||
// We couldn't map it to a D8 theme so just return the incoming theme.
|
||||
return $theme;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\process;
|
||||
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\MigrateSkipRowException;
|
||||
use Drupal\migrate\Plugin\MigrateProcessInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "block_visibility"
|
||||
* )
|
||||
*/
|
||||
class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* The migration process plugin, configured for lookups in the d6_user_role
|
||||
* and d7_user_role migrations.
|
||||
*
|
||||
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
|
||||
*/
|
||||
protected $migrationPlugin;
|
||||
|
||||
/**
|
||||
* Whether or not to skip blocks that use PHP for visibility. Only applies
|
||||
* if the PHP module is not enabled.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $skipPHP = FALSE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateProcessInterface $migration_plugin) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->moduleHandler = $module_handler;
|
||||
$this->migrationPlugin = $migration_plugin;
|
||||
|
||||
if (isset($configuration['skip_php'])) {
|
||||
$this->skipPHP = $configuration['skip_php'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||
$migration_configuration = array(
|
||||
'migration' => array(
|
||||
'd6_user_role',
|
||||
'd7_user_role',
|
||||
),
|
||||
);
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('module_handler'),
|
||||
$container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
list($old_visibility, $pages, $roles) = $value;
|
||||
|
||||
$visibility = array();
|
||||
|
||||
// If the block is assigned to specific roles, add the user_role condition.
|
||||
if ($roles) {
|
||||
$visibility['user_role'] = array(
|
||||
'id' => 'user_role',
|
||||
'roles' => array(),
|
||||
'context_mapping' => array(
|
||||
'user' => '@user.current_user_context:current_user',
|
||||
),
|
||||
'negate' => FALSE,
|
||||
);
|
||||
|
||||
foreach ($roles as $key => $role_id) {
|
||||
$roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
|
||||
}
|
||||
$visibility['user_role']['roles'] = array_combine($roles, $roles);
|
||||
}
|
||||
|
||||
if ($pages) {
|
||||
// 2 == BLOCK_VISIBILITY_PHP in Drupal 6 and 7.
|
||||
if ($old_visibility == 2) {
|
||||
// If the PHP module is present, migrate the visibility code unaltered.
|
||||
if ($this->moduleHandler->moduleExists('php')) {
|
||||
$visibility['php'] = array(
|
||||
'id' => 'php',
|
||||
// PHP code visibility could not be negated in Drupal 6 or 7.
|
||||
'negate' => FALSE,
|
||||
'php' => $pages,
|
||||
);
|
||||
}
|
||||
// Skip the row if we're configured to. If not, we don't need to do
|
||||
// anything else -- the block will simply have no PHP or request_path
|
||||
// visibility configuration.
|
||||
elseif ($this->skipPHP) {
|
||||
throw new MigrateSkipRowException();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$paths = preg_split("(\r\n?|\n)", $pages);
|
||||
foreach ($paths as $key => $path) {
|
||||
$paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
|
||||
}
|
||||
$visibility['request_path'] = array(
|
||||
'id' => 'request_path',
|
||||
'negate' => !$old_visibility,
|
||||
'pages' => implode("\n", $paths),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $visibility;
|
||||
}
|
||||
|
||||
}
|
177
web/core/modules/block/src/Plugin/migrate/source/Block.php
Normal file
177
web/core/modules/block/src/Plugin/migrate/source/Block.php
Normal file
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Plugin\migrate\source;
|
||||
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
|
||||
|
||||
/**
|
||||
* Drupal block source from database.
|
||||
*
|
||||
* @MigrateSource(
|
||||
* id = "block",
|
||||
* source_provider = "block"
|
||||
* )
|
||||
*/
|
||||
class Block extends DrupalSqlBase {
|
||||
|
||||
/**
|
||||
* The default theme name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultTheme;
|
||||
|
||||
/**
|
||||
* The admin theme name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $adminTheme;
|
||||
|
||||
/**
|
||||
* Table containing block configuration.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $blockTable;
|
||||
|
||||
/**
|
||||
* Table mapping blocks to user roles.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $blockRoleTable;
|
||||
|
||||
/**
|
||||
* Table listing user roles.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $userRoleTable;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function query() {
|
||||
if ($this->getModuleSchemaVersion('system') >= 7000) {
|
||||
$this->blockTable = 'block';
|
||||
$this->blockRoleTable = 'block_role';
|
||||
}
|
||||
else {
|
||||
$this->blockTable = 'blocks';
|
||||
$this->blockRoleTable = 'blocks_roles';
|
||||
}
|
||||
// Drupal 6 & 7 both use the same name for the user roles table.
|
||||
$this->userRoleTable = 'role';
|
||||
|
||||
return $this->select($this->blockTable, 'b')->fields('b');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function initializeIterator() {
|
||||
$this->defaultTheme = $this->variableGet('theme_default', 'Garland');
|
||||
$this->adminTheme = $this->variableGet('admin_theme', NULL);
|
||||
return parent::initializeIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fields() {
|
||||
return array(
|
||||
'bid' => $this->t('The block numeric identifier.'),
|
||||
'module' => $this->t('The module providing the block.'),
|
||||
'delta' => $this->t('The block\'s delta.'),
|
||||
'theme' => $this->t('Which theme the block is placed in.'),
|
||||
'status' => $this->t('Whether or not the block is enabled.'),
|
||||
'weight' => $this->t('Weight of the block for ordering within regions.'),
|
||||
'region' => $this->t('Region the block is placed in.'),
|
||||
'visibility' => $this->t('Visibility expression.'),
|
||||
'pages' => $this->t('Pages list.'),
|
||||
'title' => $this->t('Block title.'),
|
||||
'cache' => $this->t('Cache rule.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIds() {
|
||||
$ids['module']['type'] = 'string';
|
||||
$ids['delta']['type'] = 'string';
|
||||
$ids['theme']['type'] = 'string';
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareRow(Row $row) {
|
||||
$row->setSourceProperty('default_theme', $this->defaultTheme);
|
||||
$row->setSourceProperty('admin_theme', $this->adminTheme);
|
||||
|
||||
$module = $row->getSourceProperty('module');
|
||||
$delta = $row->getSourceProperty('delta');
|
||||
|
||||
$query = $this->select($this->blockRoleTable, 'br')
|
||||
->fields('br', array('rid'))
|
||||
->condition('module', $module)
|
||||
->condition('delta', $delta);
|
||||
$query->join($this->userRoleTable, 'ur', 'br.rid = ur.rid');
|
||||
$roles = $query->execute()
|
||||
->fetchCol();
|
||||
$row->setSourceProperty('roles', $roles);
|
||||
|
||||
$settings = array();
|
||||
switch ($module) {
|
||||
case 'aggregator':
|
||||
list($type, $id) = explode('-', $delta);
|
||||
if ($type == 'feed') {
|
||||
$item_count = $this->select('aggregator_feed', 'af')
|
||||
->fields('af', ['block'])
|
||||
->condition('fid', $id)
|
||||
->execute()
|
||||
->fetchField();
|
||||
}
|
||||
else {
|
||||
$item_count = $this->select('aggregator_category', 'ac')
|
||||
->fields('ac', ['block'])
|
||||
->condition('cid', $id)
|
||||
->execute()
|
||||
->fetchField();
|
||||
}
|
||||
$settings['aggregator']['item_count'] = $item_count;
|
||||
break;
|
||||
case 'book':
|
||||
$settings['book']['block_mode'] = $this->variableGet('book_block_mode', 'all pages');
|
||||
break;
|
||||
case 'forum':
|
||||
$settings['forum']['block_num'] = $this->variableGet('forum_block_num_' . $delta, 5);
|
||||
break;
|
||||
case 'statistics':
|
||||
foreach (array('statistics_block_top_day_num', 'statistics_block_top_all_num', 'statistics_block_top_last_num') as $name) {
|
||||
$settings['statistics'][$name] = $this->variableGet($name, 0);
|
||||
}
|
||||
break;
|
||||
case 'user':
|
||||
switch ($delta) {
|
||||
case 2:
|
||||
case 'new':
|
||||
$settings['user']['block_whois_new_count'] = $this->variableGet('user_block_whois_new_count', 5);
|
||||
break;
|
||||
case 3:
|
||||
case 'online':
|
||||
$settings['user']['block_seconds_online'] = $this->variableGet('user_block_seconds_online', 900);
|
||||
$settings['user']['max_list_count'] = $this->variableGet('user_block_max_list_count', 10);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$row->setSourceProperty('settings', $settings);
|
||||
return parent::prepareRow($row);
|
||||
}
|
||||
|
||||
}
|
77
web/core/modules/block/src/Tests/BlockAdminThemeTest.php
Normal file
77
web/core/modules/block/src/Tests/BlockAdminThemeTest.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the block system with admin themes.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockAdminThemeTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'contextual');
|
||||
|
||||
/**
|
||||
* Check for the accessibility of the admin theme on the block admin page.
|
||||
*/
|
||||
function testAdminTheme() {
|
||||
// Create administrative user.
|
||||
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes'));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Ensure that access to block admin page is denied when theme is not
|
||||
// installed.
|
||||
$this->drupalGet('admin/structure/block/list/bartik');
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Install admin theme and confirm that tab is accessible.
|
||||
\Drupal::service('theme_handler')->install(array('bartik'));
|
||||
$edit['admin_theme'] = 'bartik';
|
||||
$this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
|
||||
$this->drupalGet('admin/structure/block/list/bartik');
|
||||
$this->assertResponse(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure contextual links are disabled in Seven theme.
|
||||
*/
|
||||
function testSevenAdminTheme() {
|
||||
// Create administrative user.
|
||||
$admin_user = $this->drupalCreateUser([
|
||||
'access administration pages',
|
||||
'administer themes',
|
||||
'access contextual links',
|
||||
'view the administration theme',
|
||||
]);
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Install admin theme and confirm that tab is accessible.
|
||||
\Drupal::service('theme_handler')->install(['seven']);
|
||||
$edit['admin_theme'] = 'seven';
|
||||
$this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
|
||||
|
||||
// Define our block settings.
|
||||
$settings = [
|
||||
'theme' => 'seven',
|
||||
'region' => 'header',
|
||||
];
|
||||
|
||||
// Place a block.
|
||||
$block = $this->drupalPlaceBlock('local_tasks_block', $settings);
|
||||
|
||||
// Open admin page.
|
||||
$this->drupalGet('admin');
|
||||
|
||||
// Check if contextual link classes are unavailable.
|
||||
$this->assertNoRaw('<div data-contextual-id="block:block=' . $block->id() . ':langcode=en"></div>');
|
||||
$this->assertNoRaw('contextual-region');
|
||||
}
|
||||
|
||||
}
|
215
web/core/modules/block/src/Tests/BlockCacheTest.php
Normal file
215
web/core/modules/block/src/Tests/BlockCacheTest.php
Normal file
|
@ -0,0 +1,215 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests block caching.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockCacheTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'block_test', 'test_page_test');
|
||||
|
||||
/**
|
||||
* A user with permission to create and edit books and to administer blocks.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* An authenticated user to test block caching.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $normalUser;
|
||||
|
||||
/**
|
||||
* Another authenticated user to test block caching.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $normalUserAlt;
|
||||
|
||||
/**
|
||||
* The block used by this test.
|
||||
*
|
||||
* @var \Drupal\block\BlockInterface
|
||||
*/
|
||||
protected $block;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create an admin user, log in and enable test blocks.
|
||||
$this->adminUser = $this->drupalCreateUser(array('administer blocks', 'access administration pages'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Create additional users to test caching modes.
|
||||
$this->normalUser = $this->drupalCreateUser();
|
||||
$this->normalUserAlt = $this->drupalCreateUser();
|
||||
// Sync the roles, since drupalCreateUser() creates separate roles for
|
||||
// the same permission sets.
|
||||
$this->normalUserAlt->roles = $this->normalUser->getRoles();
|
||||
$this->normalUserAlt->save();
|
||||
|
||||
// Enable our test block.
|
||||
$this->block = $this->drupalPlaceBlock('test_cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test "user.roles" cache context.
|
||||
*/
|
||||
function testCachePerRole() {
|
||||
\Drupal::state()->set('block_test.cache_contexts', ['user.roles']);
|
||||
|
||||
// Enable our test block. Set some content for it to display.
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
$this->drupalLogin($this->normalUser);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Block content displays.');
|
||||
|
||||
// Change the content, but the cached copy should still be served.
|
||||
$old_content = $current_content;
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($old_content, 'Block is served from the cache.');
|
||||
|
||||
// Clear the cache and verify that the stale data is no longer there.
|
||||
Cache::invalidateTags(array('block_view'));
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText($old_content, 'Block cache clear removes stale cache data.');
|
||||
$this->assertText($current_content, 'Fresh block content is displayed after clearing the cache.');
|
||||
|
||||
// Test whether the cached data is served for the correct users.
|
||||
$old_content = $current_content;
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
$this->drupalLogout();
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText($old_content, 'Anonymous user does not see content cached per-role for normal user.');
|
||||
|
||||
$this->drupalLogin($this->normalUserAlt);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($old_content, 'User with the same roles sees per-role cached content.');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText($old_content, 'Admin user does not see content cached per-role for normal user.');
|
||||
|
||||
$this->drupalLogin($this->normalUser);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($old_content, 'Block is served from the per-role cache.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a cacheable block without any additional cache context.
|
||||
*/
|
||||
function testCachePermissions() {
|
||||
// user.permissions is a required context, so a user with different
|
||||
// permissions will see a different version of the block.
|
||||
\Drupal::state()->set('block_test.cache_contexts', []);
|
||||
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Block content displays.');
|
||||
|
||||
$old_content = $current_content;
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertText($old_content, 'Block content served from cache.');
|
||||
|
||||
$this->drupalLogout();
|
||||
$this->drupalGet('user');
|
||||
$this->assertText($current_content, 'Block content not served from cache.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test non-cacheable block.
|
||||
*/
|
||||
function testNoCache() {
|
||||
\Drupal::state()->set('block_test.cache_max_age', 0);
|
||||
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
// If max_age = 0 has no effect, the next request would be cached.
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Block content displays.');
|
||||
|
||||
// A cached copy should not be served.
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Maximum age of zero prevents blocks from being cached.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test "user" cache context.
|
||||
*/
|
||||
function testCachePerUser() {
|
||||
\Drupal::state()->set('block_test.cache_contexts', ['user']);
|
||||
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
$this->drupalLogin($this->normalUser);
|
||||
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Block content displays.');
|
||||
|
||||
$old_content = $current_content;
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
$this->drupalGet('');
|
||||
$this->assertText($old_content, 'Block is served from per-user cache.');
|
||||
|
||||
$this->drupalLogin($this->normalUserAlt);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($current_content, 'Per-user block cache is not served for other users.');
|
||||
|
||||
$this->drupalLogin($this->normalUser);
|
||||
$this->drupalGet('');
|
||||
$this->assertText($old_content, 'Per-user block cache is persistent.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test "url" cache context.
|
||||
*/
|
||||
function testCachePerPage() {
|
||||
\Drupal::state()->set('block_test.cache_contexts', ['url']);
|
||||
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertText($current_content, 'Block content displays on the test page.');
|
||||
|
||||
$old_content = $current_content;
|
||||
$current_content = $this->randomMachineName();
|
||||
\Drupal::state()->set('block_test.content', $current_content);
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertResponse(200);
|
||||
$this->assertNoText($old_content, 'Block content cached for the test page does not show up for the user page.');
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
$this->assertText($old_content, 'Block content cached for the test page.');
|
||||
}
|
||||
|
||||
}
|
71
web/core/modules/block/src/Tests/BlockFormInBlockTest.php
Normal file
71
web/core/modules/block/src/Tests/BlockFormInBlockTest.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests form in block caching.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockFormInBlockTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['block', 'block_test', 'test_page_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Enable our test block.
|
||||
$this->drupalPlaceBlock('test_form_in_block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to see if form in block's redirect isn't cached.
|
||||
*/
|
||||
function testCachePerPage() {
|
||||
$form_values = ['email' => 'test@example.com'];
|
||||
|
||||
// Go to "test-page" and test if the block is enabled.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
$this->assertText('Your .com email address.', 'form found');
|
||||
|
||||
// Make sure that we're currently still on /test-page after submitting the
|
||||
// form.
|
||||
$this->drupalPostForm(NULL, $form_values, t('Submit'));
|
||||
$this->assertUrl('test-page');
|
||||
$this->assertText(t('Your email address is @email', ['@email' => 'test@example.com']));
|
||||
|
||||
// Go to a different page and see if the block is enabled there as well.
|
||||
$this->drupalGet('test-render-title');
|
||||
$this->assertResponse(200);
|
||||
$this->assertText('Your .com email address.', 'form found');
|
||||
|
||||
// Make sure that submitting the form didn't redirect us to the first page
|
||||
// we submitted the form from after submitting the form from
|
||||
// /test-render-title.
|
||||
$this->drupalPostForm(NULL, $form_values, t('Submit'));
|
||||
$this->assertUrl('test-render-title');
|
||||
$this->assertText(t('Your email address is @email', ['@email' => 'test@example.com']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the actual placeholders
|
||||
*/
|
||||
public function testPlaceholders() {
|
||||
$this->drupalGet('test-multiple-forms');
|
||||
|
||||
$placeholder = 'form_action_' . hash('crc32b', 'Drupal\Core\Form\FormBuilder::prepareForm');
|
||||
$this->assertText('Form action: ' . $placeholder, 'placeholder found.');
|
||||
}
|
||||
|
||||
}
|
73
web/core/modules/block/src/Tests/BlockHiddenRegionTest.php
Normal file
73
web/core/modules/block/src/Tests/BlockHiddenRegionTest.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests that a newly installed theme does not inherit blocks to its hidden
|
||||
* regions.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockHiddenRegionTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* An administrative user to configure the test environment.
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'block_test', 'search');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create administrative user.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
'administer themes',
|
||||
'search content',
|
||||
)
|
||||
);
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalPlaceBlock('search_form_block');
|
||||
$this->drupalPlaceBlock('local_tasks_block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that hidden regions do not inherit blocks when a theme is installed.
|
||||
*/
|
||||
public function testBlockNotInHiddenRegion() {
|
||||
|
||||
// Ensure that the search form block is displayed.
|
||||
$this->drupalGet('');
|
||||
$this->assertText('Search', 'Block was displayed on the front page.');
|
||||
|
||||
// Install "block_test_theme" and set it as the default theme.
|
||||
$theme = 'block_test_theme';
|
||||
// We need to install a non-hidden theme so that there is more than one
|
||||
// local task.
|
||||
\Drupal::service('theme_handler')->install(array($theme, 'stark'));
|
||||
$this->config('system.theme')
|
||||
->set('default', $theme)
|
||||
->save();
|
||||
// Installing a theme will cause the kernel terminate event to rebuild the
|
||||
// router. Simulate that here.
|
||||
\Drupal::service('router.builder')->rebuildIfNeeded();
|
||||
|
||||
// Ensure that "block_test_theme" is set as the default theme.
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->assertText('Block test theme(' . t('active tab') . ')', 'Default local task on blocks admin page is the block test theme.');
|
||||
|
||||
// Ensure that the search form block is displayed.
|
||||
$this->drupalGet('');
|
||||
$this->assertText('Search', 'Block was displayed on the front page.');
|
||||
}
|
||||
|
||||
}
|
51
web/core/modules/block/src/Tests/BlockHookOperationTest.php
Normal file
51
web/core/modules/block/src/Tests/BlockHookOperationTest.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests for Block module regarding hook_entity_operations_alter().
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockHookOperationTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'entity_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$permissions = array(
|
||||
'administer blocks',
|
||||
);
|
||||
|
||||
// Create and log in user.
|
||||
$admin_user = $this->drupalCreateUser($permissions);
|
||||
$this->drupalLogin($admin_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block list to see if the test_operation link is added.
|
||||
*/
|
||||
public function testBlockOperationAlter() {
|
||||
// Add a test block, any block will do.
|
||||
// Set the machine name so the test_operation link can be built later.
|
||||
$block_id = Unicode::strtolower($this->randomMachineName(16));
|
||||
$this->drupalPlaceBlock('system_powered_by_block', array('id' => $block_id));
|
||||
|
||||
// Get the Block listing.
|
||||
$this->drupalGet('admin/structure/block');
|
||||
|
||||
$test_operation_link = 'admin/structure/block/manage/' . $block_id . '/test_operation';
|
||||
// Test if the test_operation link is on the page.
|
||||
$this->assertLinkByHref($test_operation_link);
|
||||
}
|
||||
|
||||
}
|
50
web/core/modules/block/src/Tests/BlockHtmlTest.php
Normal file
50
web/core/modules/block/src/Tests/BlockHtmlTest.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests block HTML ID validity.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockHtmlTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'block_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
// Enable the test_html block, to test HTML ID and attributes.
|
||||
\Drupal::state()->set('block_test.attributes', array('data-custom-attribute' => 'foo'));
|
||||
\Drupal::state()->set('block_test.content', $this->randomMachineName());
|
||||
$this->drupalPlaceBlock('test_html', array('id' => 'test_html_block'));
|
||||
|
||||
// Enable a menu block, to test more complicated HTML.
|
||||
$this->drupalPlaceBlock('system_menu_block:admin');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for valid HTML for a block.
|
||||
*/
|
||||
function testHtml() {
|
||||
$this->drupalGet('');
|
||||
|
||||
// Ensure that a block's ID is converted to an HTML valid ID, and that
|
||||
// block-specific attributes are added to the same DOM element.
|
||||
$this->assertFieldByXPath('//div[@id="block-test-html-block" and @data-custom-attribute="foo"]', NULL, 'HTML ID and attributes for test block are valid and on the same DOM element.');
|
||||
|
||||
// Ensure expected markup for a menu block.
|
||||
$elements = $this->xpath('//nav[contains(@class, :nav-class)]/ul[contains(@class, :ul-class)]/li', array(':nav-class' => 'block-menu', ':ul-class' => 'menu'));
|
||||
$this->assertTrue(!empty($elements), 'The proper block markup was found.');
|
||||
}
|
||||
|
||||
}
|
33
web/core/modules/block/src/Tests/BlockInstallTest.php
Normal file
33
web/core/modules/block/src/Tests/BlockInstallTest.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests block module's installation.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockInstallTest extends WebTestBase {
|
||||
|
||||
public function testCacheTagInvalidationUponInstallation() {
|
||||
// Warm the page cache.
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText('Powered by Drupal');
|
||||
$this->assertNoCacheTag('config:block_list');
|
||||
|
||||
// Install the block module, and place the "Powered by Drupal" block.
|
||||
$this->container->get('module_installer')->install(['block', 'shortcut']);
|
||||
$this->rebuildContainer();
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
$this->drupalPlaceBlock('system_powered_by_block');
|
||||
|
||||
// Check the same page, block.module's hook_install() should have
|
||||
// invalidated the 'rendered' cache tag to make blocks show up.
|
||||
$this->drupalGet('');
|
||||
$this->assertCacheTag('config:block_list');
|
||||
$this->assertText('Powered by Drupal');
|
||||
}
|
||||
|
||||
}
|
63
web/core/modules/block/src/Tests/BlockInvalidRegionTest.php
Normal file
63
web/core/modules/block/src/Tests/BlockInvalidRegionTest.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\block\Entity\Block;
|
||||
|
||||
/**
|
||||
* Tests that an active block assigned to a non-existing region triggers the
|
||||
* warning message and is disabled.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockInvalidRegionTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'block_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Create an admin user.
|
||||
$admin_user = $this->drupalCreateUser(array(
|
||||
'administer site configuration',
|
||||
'access administration pages',
|
||||
'administer blocks',
|
||||
));
|
||||
$this->drupalLogin($admin_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that blocks assigned to invalid regions work correctly.
|
||||
*/
|
||||
function testBlockInInvalidRegion() {
|
||||
// Enable a test block and place it in an invalid region.
|
||||
$block = $this->drupalPlaceBlock('test_html');
|
||||
$block->setRegion('invalid_region');
|
||||
$block->save();
|
||||
|
||||
$warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block->id(), '%region' => 'invalid_region'));
|
||||
|
||||
// Clearing the cache should disable the test block placed in the invalid region.
|
||||
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
|
||||
$this->assertRaw($warning_message, 'Enabled block was in the invalid region and has been disabled.');
|
||||
|
||||
// Clear the cache to check if the warning message is not triggered.
|
||||
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
|
||||
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
|
||||
|
||||
// Place disabled test block in the invalid region of the default theme.
|
||||
$block = Block::load($block->id());
|
||||
$block->setRegion('invalid_region');
|
||||
$block->save();
|
||||
|
||||
// Clear the cache to check if the warning message is not triggered.
|
||||
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
|
||||
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
|
||||
}
|
||||
|
||||
}
|
77
web/core/modules/block/src/Tests/BlockLanguageCacheTest.php
Normal file
77
web/core/modules/block/src/Tests/BlockLanguageCacheTest.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests display of menu blocks with multiple languages.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockLanguageCacheTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'language', 'menu_ui');
|
||||
|
||||
/**
|
||||
* List of langcodes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $langcodes = array();
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create test languages.
|
||||
$this->langcodes = array(ConfigurableLanguage::load('en'));
|
||||
for ($i = 1; $i < 3; ++$i) {
|
||||
$language = ConfigurableLanguage::create(array(
|
||||
'id' => 'l' . $i,
|
||||
'label' => $this->randomString(),
|
||||
));
|
||||
$language->save();
|
||||
$this->langcodes[$i] = $language;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a block in a language, check blocks page in all languages.
|
||||
*/
|
||||
public function testBlockLinks() {
|
||||
// Create admin user to be able to access block admin.
|
||||
$admin_user = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
'access administration pages',
|
||||
'administer menu',
|
||||
));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Create the block cache for all languages.
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$this->drupalGet('admin/structure/block', array('language' => $langcode));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
}
|
||||
|
||||
// Create a menu in the default language.
|
||||
$edit['label'] = $this->randomMachineName();
|
||||
$edit['id'] = Unicode::strtolower($edit['label']);
|
||||
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
|
||||
$this->assertText(t('Menu @label has been added.', array('@label' => $edit['label'])));
|
||||
|
||||
// Check that the block is listed for all languages.
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$this->drupalGet('admin/structure/block', array('language' => $langcode));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$this->assertText($edit['label']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
185
web/core/modules/block/src/Tests/BlockLanguageTest.php
Normal file
185
web/core/modules/block/src/Tests/BlockLanguageTest.php
Normal file
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\block\Entity\Block;
|
||||
|
||||
/**
|
||||
* Tests if a block can be configured to be only visible on a particular
|
||||
* language.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockLanguageTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* An administrative user to configure the test environment.
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('language', 'block', 'content_translation');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a new user, allow him to manage the blocks and the languages.
|
||||
$this->adminUser = $this->drupalCreateUser(array('administer blocks', 'administer languages'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Add predefined language.
|
||||
$edit = array(
|
||||
'predefined_langcode' => 'fr',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
||||
$this->assertText('French', 'Language added successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the visibility settings for the blocks based on language.
|
||||
*/
|
||||
public function testLanguageBlockVisibility() {
|
||||
// Check if the visibility setting is available.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
|
||||
|
||||
$this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
|
||||
$this->assertNoField('visibility[language][context_mapping][language]', 'Language type field is not visible.');
|
||||
|
||||
// Enable a standard block and set the visibility setting for one language.
|
||||
$edit = array(
|
||||
'visibility[language][langcodes][en]' => TRUE,
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'region' => 'sidebar_first',
|
||||
);
|
||||
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
|
||||
|
||||
// Change the default language.
|
||||
$edit = array(
|
||||
'site_default_language' => 'fr',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
|
||||
|
||||
// Check that a page has a block.
|
||||
$this->drupalGet('en');
|
||||
$this->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
|
||||
|
||||
// Check that a page doesn't has a block for the current language anymore.
|
||||
$this->drupalGet('fr');
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the visibility settings are removed if the language is deleted.
|
||||
*/
|
||||
public function testLanguageBlockVisibilityLanguageDelete() {
|
||||
// Enable a standard block and set the visibility setting for one language.
|
||||
$edit = array(
|
||||
'visibility' => array(
|
||||
'language' => array(
|
||||
'langcodes' => array(
|
||||
'fr' => 'fr',
|
||||
),
|
||||
'context_mapping' => ['language' => '@language.current_language_context:language_interface'],
|
||||
),
|
||||
),
|
||||
);
|
||||
$block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
|
||||
|
||||
// Check that we have the language in config after saving the setting.
|
||||
$visibility = $block->getVisibility();
|
||||
$this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
|
||||
|
||||
// Delete the language.
|
||||
$this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete'));
|
||||
|
||||
// Check that the language is no longer stored in the configuration after
|
||||
// it is deleted.
|
||||
$block = Block::load($block->id());
|
||||
$visibility = $block->getVisibility();
|
||||
$this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
|
||||
|
||||
// Ensure that the block visibility for language is gone from the UI.
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLink('Configure');
|
||||
$elements = $this->xpath('//details[@id="edit-visibility-language"]');
|
||||
$this->assertTrue(empty($elements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests block language visibility with different language types.
|
||||
*/
|
||||
public function testMultipleLanguageTypes() {
|
||||
// Customize content language detection to be different from interface
|
||||
// language detection.
|
||||
$edit = [
|
||||
// Interface language detection: only using session.
|
||||
'language_interface[enabled][language-url]' => FALSE,
|
||||
'language_interface[enabled][language-session]' => TRUE,
|
||||
// Content language detection: only using URL.
|
||||
'language_content[configurable]' => TRUE,
|
||||
'language_content[enabled][language-url]' => TRUE,
|
||||
'language_content[enabled][language-interface]' => FALSE,
|
||||
];
|
||||
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
|
||||
|
||||
// Check if the visibility setting is available with a type setting.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
|
||||
$this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
|
||||
$this->assertField('visibility[language][context_mapping][language]', 'Language type field is visible.');
|
||||
|
||||
// Enable a standard block and set visibility to French only.
|
||||
$block_id = strtolower($this->randomMachineName(8));
|
||||
$edit = [
|
||||
'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface',
|
||||
'visibility[language][langcodes][fr]' => TRUE,
|
||||
'id' => $block_id,
|
||||
'region' => 'sidebar_first',
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
|
||||
|
||||
// Interface negotiation depends on request arguments.
|
||||
$this->drupalGet('node', ['query' => ['language' => 'en']]);
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
$this->drupalGet('node', ['query' => ['language' => 'fr']]);
|
||||
$this->assertText('Powered by Drupal', 'The body of the block appears on the page.');
|
||||
|
||||
// Log in again in order to clear the interface language stored in the
|
||||
// session.
|
||||
$this->drupalLogout();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Content language does not depend on session/request arguments.
|
||||
// It will fall back on English (site default) and not display the block.
|
||||
$this->drupalGet('en');
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
$this->drupalGet('fr');
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
|
||||
// Change visibility to now depend on content language for this block.
|
||||
$edit = [
|
||||
'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content'
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
|
||||
|
||||
// Content language negotiation does not depend on request arguments.
|
||||
// It will fall back on English (site default) and not display the block.
|
||||
$this->drupalGet('node', ['query' => ['language' => 'en']]);
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
$this->drupalGet('node', ['query' => ['language' => 'fr']]);
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
|
||||
// Content language negotiation depends on path prefix.
|
||||
$this->drupalGet('en');
|
||||
$this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
|
||||
$this->drupalGet('fr');
|
||||
$this->assertText('Powered by Drupal', 'The body of the block appears on the page.');
|
||||
}
|
||||
|
||||
}
|
80
web/core/modules/block/src/Tests/BlockRenderOrderTest.php
Normal file
80
web/core/modules/block/src/Tests/BlockRenderOrderTest.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests blocks are being rendered in order by weight.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockRenderOrderTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'block');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Create a test user.
|
||||
$end_user = $this->drupalCreateUser(array(
|
||||
'access content',
|
||||
));
|
||||
$this->drupalLogin($end_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the render order of the blocks.
|
||||
*/
|
||||
function testBlockRenderOrder() {
|
||||
// Enable test blocks and place them in the same region.
|
||||
$region = 'header';
|
||||
$test_blocks = array(
|
||||
'stark_powered' => array(
|
||||
'weight' => '-3',
|
||||
'id' => 'stark_powered',
|
||||
'label' => 'Test block A',
|
||||
),
|
||||
'stark_by' => array(
|
||||
'weight' => '3',
|
||||
'id' => 'stark_by',
|
||||
'label' => 'Test block C',
|
||||
),
|
||||
'stark_drupal' => array(
|
||||
'weight' => '3',
|
||||
'id' => 'stark_drupal',
|
||||
'label' => 'Test block B',
|
||||
),
|
||||
);
|
||||
|
||||
// Place the test blocks.
|
||||
foreach ($test_blocks as $test_block) {
|
||||
$this->drupalPlaceBlock('system_powered_by_block', array(
|
||||
'label' => $test_block['label'],
|
||||
'region' => $region,
|
||||
'weight' => $test_block['weight'],
|
||||
'id' => $test_block['id'],
|
||||
));
|
||||
}
|
||||
|
||||
$this->drupalGet('');
|
||||
$test_content = $this->getRawContent('');
|
||||
|
||||
$controller = $this->container->get('entity_type.manager')->getStorage('block');
|
||||
foreach ($controller->loadMultiple() as $return_block) {
|
||||
$id = $return_block->id();
|
||||
if ($return_block_weight = $return_block->getWeight()) {
|
||||
$this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
|
||||
$position[$id] = strpos($test_content, Html::getClass('block-' . $test_blocks[$id]['id']));
|
||||
}
|
||||
}
|
||||
$this->assertTrue($position['stark_powered'] < $position['stark_by'], 'Blocks with different weight are rendered in the correct order.');
|
||||
$this->assertTrue($position['stark_drupal'] < $position['stark_by'], 'Blocks with identical weight are rendered in alphabetical order.');
|
||||
}
|
||||
|
||||
}
|
119
web/core/modules/block/src/Tests/BlockSystemBrandingTest.php
Normal file
119
web/core/modules/block/src/Tests/BlockSystemBrandingTest.php
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
/**
|
||||
* Tests branding block display.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockSystemBrandingTest extends BlockTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'system');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Set a site slogan.
|
||||
$this->config('system.site')
|
||||
->set('slogan', 'Community plumbing')
|
||||
->save();
|
||||
// Add the system branding block to the page.
|
||||
$this->drupalPlaceBlock('system_branding_block', array('region' => 'header', 'id' => 'site-branding'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests system branding block configuration.
|
||||
*/
|
||||
public function testSystemBrandingSettings() {
|
||||
$site_logo_xpath = '//div[@id="block-site-branding"]//a[@class="site-logo"]';
|
||||
$site_name_xpath = '//div[@id="block-site-branding"]//div[@class="site-name"]';
|
||||
$site_slogan_xpath = '//div[@id="block-site-branding"]//div[@class="site-slogan"]';
|
||||
|
||||
// Set default block settings.
|
||||
$this->drupalGet('');
|
||||
$site_logo_element = $this->xpath($site_logo_xpath);
|
||||
$site_name_element = $this->xpath($site_name_xpath);
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
// Test that all branding elements are displayed.
|
||||
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
|
||||
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
|
||||
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
|
||||
$this->assertCacheTag('config:system.site');
|
||||
|
||||
// Be sure the slogan is XSS-filtered.
|
||||
$this->config('system.site')
|
||||
->set('slogan', '<script>alert("Community carpentry");</script>')
|
||||
->save();
|
||||
$this->drupalGet('');
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
$this->assertEqual($site_slogan_element[0], 'alert("Community carpentry");', 'The site slogan was XSS-filtered.');
|
||||
|
||||
// Turn just the logo off.
|
||||
$this->config('block.block.site-branding')
|
||||
->set('settings.use_site_logo', 0)
|
||||
->save();
|
||||
$this->drupalGet('');
|
||||
$site_logo_element = $this->xpath($site_logo_xpath);
|
||||
$site_name_element = $this->xpath($site_name_xpath);
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
// Re-test all branding elements.
|
||||
$this->assertTrue(empty($site_logo_element), 'The branding block logo was disabled.');
|
||||
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
|
||||
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
|
||||
$this->assertCacheTag('config:system.site');
|
||||
|
||||
// Turn just the site name off.
|
||||
$this->config('block.block.site-branding')
|
||||
->set('settings.use_site_logo', 1)
|
||||
->set('settings.use_site_name', 0)
|
||||
->save();
|
||||
$this->drupalGet('');
|
||||
$site_logo_element = $this->xpath($site_logo_xpath);
|
||||
$site_name_element = $this->xpath($site_name_xpath);
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
// Re-test all branding elements.
|
||||
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
|
||||
$this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.');
|
||||
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
|
||||
$this->assertCacheTag('config:system.site');
|
||||
|
||||
// Turn just the site slogan off.
|
||||
$this->config('block.block.site-branding')
|
||||
->set('settings.use_site_name', 1)
|
||||
->set('settings.use_site_slogan', 0)
|
||||
->save();
|
||||
$this->drupalGet('');
|
||||
$site_logo_element = $this->xpath($site_logo_xpath);
|
||||
$site_name_element = $this->xpath($site_name_xpath);
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
// Re-test all branding elements.
|
||||
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
|
||||
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
|
||||
$this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.');
|
||||
$this->assertCacheTag('config:system.site');
|
||||
|
||||
// Turn the site name and the site slogan off.
|
||||
$this->config('block.block.site-branding')
|
||||
->set('settings.use_site_name', 0)
|
||||
->set('settings.use_site_slogan', 0)
|
||||
->save();
|
||||
$this->drupalGet('');
|
||||
$site_logo_element = $this->xpath($site_logo_xpath);
|
||||
$site_name_element = $this->xpath($site_name_xpath);
|
||||
$site_slogan_element = $this->xpath($site_slogan_xpath);
|
||||
// Re-test all branding elements.
|
||||
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
|
||||
$this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.');
|
||||
$this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.');
|
||||
$this->assertCacheTag('config:system.site');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the block_theme_suggestions_block() function.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockTemplateSuggestionsTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block');
|
||||
|
||||
/**
|
||||
* Tests template suggestions from block_theme_suggestions_block().
|
||||
*/
|
||||
function testBlockThemeHookSuggestions() {
|
||||
// Define a block with a derivative to be preprocessed, which includes both
|
||||
// an underscore (not transformed) and a hyphen (transformed to underscore),
|
||||
// and generates possibilities for each level of derivative.
|
||||
// @todo Clarify this comment.
|
||||
$block = Block::create(array(
|
||||
'plugin' => 'system_menu_block:admin',
|
||||
'region' => 'footer',
|
||||
'id' => 'machinename',
|
||||
));
|
||||
|
||||
$variables = array();
|
||||
$plugin = $block->getPlugin();
|
||||
$variables['elements']['#configuration'] = $plugin->getConfiguration();
|
||||
$variables['elements']['#plugin_id'] = $plugin->getPluginId();
|
||||
$variables['elements']['#id'] = $block->id();
|
||||
$variables['elements']['#base_plugin_id'] = $plugin->getBaseId();
|
||||
$variables['elements']['#derivative_plugin_id'] = $plugin->getDerivativeId();
|
||||
$variables['elements']['content'] = array();
|
||||
$suggestions = block_theme_suggestions_block($variables);
|
||||
$this->assertEqual($suggestions, array('block__system', 'block__system_menu_block', 'block__system_menu_block__admin', 'block__machinename'));
|
||||
}
|
||||
|
||||
}
|
546
web/core/modules/block/src/Tests/BlockTest.php
Normal file
546
web/core/modules/block/src/Tests/BlockTest.php
Normal file
|
@ -0,0 +1,546 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests basic block functionality.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockTest extends BlockTestBase {
|
||||
|
||||
/**
|
||||
* Tests block visibility.
|
||||
*/
|
||||
function testBlockVisibility() {
|
||||
$block_name = 'system_powered_by_block';
|
||||
// Create a random title for the block.
|
||||
$title = $this->randomMachineName(8);
|
||||
// Enable a standard block.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$edit = array(
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'region' => 'sidebar_first',
|
||||
'settings[label]' => $title,
|
||||
'settings[label_display]' => TRUE,
|
||||
);
|
||||
// Set the block to be hidden on any user path, and to be shown only to
|
||||
// authenticated users.
|
||||
$edit['visibility[request_path][pages]'] = '/user*';
|
||||
$edit['visibility[request_path][negate]'] = TRUE;
|
||||
$edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
|
||||
$this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
|
||||
$this->assertFieldChecked('edit-visibility-request-path-negate-0');
|
||||
|
||||
$this->drupalPostForm(NULL, $edit, t('Save block'));
|
||||
$this->assertText('The block configuration has been saved.', 'Block was saved');
|
||||
|
||||
$this->clickLink('Configure');
|
||||
$this->assertFieldChecked('edit-visibility-request-path-negate-1');
|
||||
|
||||
$this->drupalGet('');
|
||||
$this->assertText($title, 'Block was displayed on the front page.');
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
|
||||
|
||||
// Confirm that the block is not displayed to anonymous users.
|
||||
$this->drupalLogout();
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText($title, 'Block was not displayed to anonymous users.');
|
||||
|
||||
// Confirm that an empty block is not displayed.
|
||||
$this->assertNoText('Powered by Drupal', 'Empty block not displayed.');
|
||||
$this->assertNoRaw('sidebar-first', 'Empty sidebar-first region is not displayed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that visibility can be properly toggled.
|
||||
*/
|
||||
public function testBlockToggleVisibility() {
|
||||
$block_name = 'system_powered_by_block';
|
||||
// Create a random title for the block.
|
||||
$title = $this->randomMachineName(8);
|
||||
// Enable a standard block.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$edit = array(
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'region' => 'sidebar_first',
|
||||
'settings[label]' => $title,
|
||||
);
|
||||
$block_id = $edit['id'];
|
||||
// Set the block to be shown only to authenticated users.
|
||||
$edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
|
||||
$this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
|
||||
$this->clickLink('Configure');
|
||||
$this->assertFieldChecked('edit-visibility-user-role-roles-authenticated');
|
||||
|
||||
$edit = [
|
||||
'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, 'Save block');
|
||||
$this->clickLink('Configure');
|
||||
$this->assertNoFieldChecked('edit-visibility-user-role-roles-authenticated');
|
||||
|
||||
// Ensure that no visibility is configured.
|
||||
/** @var \Drupal\block\BlockInterface $block */
|
||||
$block = Block::load($block_id);
|
||||
$visibility_config = $block->getVisibilityConditions()->getConfiguration();
|
||||
$this->assertIdentical([], $visibility_config);
|
||||
$this->assertIdentical([], $block->get('visibility'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test block visibility when leaving "pages" textarea empty.
|
||||
*/
|
||||
function testBlockVisibilityListedEmpty() {
|
||||
$block_name = 'system_powered_by_block';
|
||||
// Create a random title for the block.
|
||||
$title = $this->randomMachineName(8);
|
||||
// Enable a standard block.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$edit = array(
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'region' => 'sidebar_first',
|
||||
'settings[label]' => $title,
|
||||
'visibility[request_path][negate]' => TRUE,
|
||||
);
|
||||
// Set the block to be hidden on any user path, and to be shown only to
|
||||
// authenticated users.
|
||||
$this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
|
||||
$this->assertText('The block configuration has been saved.', 'Block was saved');
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
|
||||
|
||||
$this->drupalGet('USER');
|
||||
$this->assertNoText($title, 'Block was not displayed according to block visibility rules regardless of path case.');
|
||||
|
||||
// Confirm that the block is not displayed to anonymous users.
|
||||
$this->drupalLogout();
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests adding a block from the library page with a weight query string.
|
||||
*/
|
||||
public function testAddBlockFromLibraryWithWeight() {
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
// Test one positive, zero, and one negative weight.
|
||||
foreach (['7', '0', '-9'] as $weight) {
|
||||
$options = [
|
||||
'query' => [
|
||||
'region' => 'sidebar_first',
|
||||
'weight' => $weight,
|
||||
],
|
||||
];
|
||||
$this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
|
||||
|
||||
$block_name = 'system_powered_by_block';
|
||||
$add_url = Url::fromRoute('block.admin_add', [
|
||||
'plugin_id' => $block_name,
|
||||
'theme' => $default_theme
|
||||
]);
|
||||
$links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
|
||||
$this->assertEqual(1, count($links), 'Found one matching link.');
|
||||
$this->assertEqual(t('Place block'), (string) $links[0], 'Found the expected link text.');
|
||||
|
||||
list($path, $query_string) = explode('?', $links[0]['href'], 2);
|
||||
parse_str($query_string, $query_parts);
|
||||
$this->assertEqual($weight, $query_parts['weight'], 'Found the expected weight query string.');
|
||||
|
||||
// Create a random title for the block.
|
||||
$title = $this->randomMachineName(8);
|
||||
$block_id = strtolower($this->randomMachineName(8));
|
||||
$edit = [
|
||||
'id' => $block_id,
|
||||
'settings[label]' => $title,
|
||||
];
|
||||
// Create the block using the link parsed from the library page.
|
||||
$this->drupalPostForm($this->getAbsoluteUrl($links[0]['href']), $edit, t('Save block'));
|
||||
|
||||
// Ensure that the block was created with the expected weight.
|
||||
/** @var \Drupal\block\BlockInterface $block */
|
||||
$block = Block::load($block_id);
|
||||
$this->assertEqual($weight, $block->getWeight(), 'Found the block with expected weight.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test configuring and moving a module-define block to specific regions.
|
||||
*/
|
||||
function testBlock() {
|
||||
// Place page title block to test error messages.
|
||||
$this->drupalPlaceBlock('page_title_block');
|
||||
|
||||
// Select the 'Powered by Drupal' block to be configured and moved.
|
||||
$block = array();
|
||||
$block['id'] = 'system_powered_by_block';
|
||||
$block['settings[label]'] = $this->randomMachineName(8);
|
||||
$block['settings[label_display]'] = TRUE;
|
||||
$block['theme'] = $this->config('system.theme')->get('default');
|
||||
$block['region'] = 'header';
|
||||
|
||||
// Set block title to confirm that interface works and override any custom titles.
|
||||
$this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], array('settings[label]' => $block['settings[label]'], 'settings[label_display]' => $block['settings[label_display]'], 'id' => $block['id'], 'region' => $block['region']), t('Save block'));
|
||||
$this->assertText(t('The block configuration has been saved.'), 'Block title set.');
|
||||
// Check to see if the block was created by checking its configuration.
|
||||
$instance = Block::load($block['id']);
|
||||
|
||||
$this->assertEqual($instance->label(), $block['settings[label]'], 'Stored block title found.');
|
||||
|
||||
// Check whether the block can be moved to all available regions.
|
||||
foreach ($this->regions as $region) {
|
||||
$this->moveBlockToRegion($block, $region);
|
||||
}
|
||||
|
||||
// Set the block to the disabled region.
|
||||
$edit = array();
|
||||
$edit['blocks[' . $block['id'] . '][region]'] = -1;
|
||||
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
|
||||
|
||||
// Confirm that the block is now listed as disabled.
|
||||
$this->assertText(t('The block settings have been updated.'), 'Block successfully move to disabled region.');
|
||||
|
||||
// Confirm that the block instance title and markup are not displayed.
|
||||
$this->drupalGet('node');
|
||||
$this->assertNoText(t($block['settings[label]']));
|
||||
// Check for <div id="block-my-block-instance-name"> if the machine name
|
||||
// is my_block_instance_name.
|
||||
$xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))));
|
||||
$this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
|
||||
|
||||
// Test deleting the block from the edit form.
|
||||
$this->drupalGet('admin/structure/block/manage/' . $block['id']);
|
||||
$this->clickLink(t('Delete'));
|
||||
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $block['settings[label]'])));
|
||||
$this->drupalPostForm(NULL, array(), t('Delete'));
|
||||
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $block['settings[label]'])));
|
||||
|
||||
// Test deleting a block via "Configure block" link.
|
||||
$block = $this->drupalPlaceBlock('system_powered_by_block');
|
||||
$this->drupalGet('admin/structure/block/manage/' . $block->id(), array('query' => array('destination' => 'admin')));
|
||||
$this->clickLink(t('Delete'));
|
||||
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $block->label())));
|
||||
$this->drupalPostForm(NULL, array(), t('Delete'));
|
||||
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $block->label())));
|
||||
$this->assertUrl('admin');
|
||||
$this->assertNoRaw($block->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the block form has a theme selector when not passed via the URL.
|
||||
*/
|
||||
public function testBlockThemeSelector() {
|
||||
// Install all themes.
|
||||
\Drupal::service('theme_handler')->install(['bartik', 'seven', 'stark']);
|
||||
$theme_settings = $this->config('system.theme');
|
||||
foreach (['bartik', 'seven', 'stark'] as $theme) {
|
||||
$this->drupalGet('admin/structure/block/list/' . $theme);
|
||||
$this->assertTitle(t('Block layout') . ' | Drupal');
|
||||
// Select the 'Powered by Drupal' block to be placed.
|
||||
$block = array();
|
||||
$block['id'] = strtolower($this->randomMachineName());
|
||||
$block['theme'] = $theme;
|
||||
$block['region'] = 'content';
|
||||
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
|
||||
$this->assertText(t('The block configuration has been saved.'));
|
||||
$this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . Html::getClass($block['id']));
|
||||
|
||||
// Set the default theme and ensure the block is placed.
|
||||
$theme_settings->set('default', $theme)->save();
|
||||
$this->drupalGet('');
|
||||
$elements = $this->xpath('//div[@id = :id]', array(':id' => Html::getUniqueId('block-' . $block['id'])));
|
||||
$this->assertTrue(!empty($elements), 'The block was found.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test block display of theme titles.
|
||||
*/
|
||||
function testThemeName() {
|
||||
// Enable the help block.
|
||||
$this->drupalPlaceBlock('help_block', array('region' => 'help'));
|
||||
$this->drupalPlaceBlock('local_tasks_block');
|
||||
// Explicitly set the default and admin themes.
|
||||
$theme = 'block_test_specialchars_theme';
|
||||
\Drupal::service('theme_handler')->install(array($theme));
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->assertEscaped('<"Cat" & \'Mouse\'>');
|
||||
$this->drupalGet('admin/structure/block/list/block_test_specialchars_theme');
|
||||
$this->assertEscaped('Demonstrate block regions (<"Cat" & \'Mouse\'>)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test block title display settings.
|
||||
*/
|
||||
function testHideBlockTitle() {
|
||||
$block_name = 'system_powered_by_block';
|
||||
// Create a random title for the block.
|
||||
$title = $this->randomMachineName(8);
|
||||
$id = strtolower($this->randomMachineName(8));
|
||||
// Enable a standard block.
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$edit = array(
|
||||
'id' => $id,
|
||||
'region' => 'sidebar_first',
|
||||
'settings[label]' => $title,
|
||||
);
|
||||
$this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
|
||||
$this->assertText('The block configuration has been saved.', 'Block was saved');
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertNoText($title, 'Block title was not displayed by default.');
|
||||
|
||||
$edit = array(
|
||||
'settings[label_display]' => TRUE,
|
||||
);
|
||||
$this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block'));
|
||||
$this->assertText('The block configuration has been saved.', 'Block was saved');
|
||||
|
||||
$this->drupalGet('admin/structure/block/manage/' . $id);
|
||||
$this->assertFieldChecked('edit-settings-label-display', 'The display_block option has the correct default value on the configuration form.');
|
||||
|
||||
$this->drupalGet('user');
|
||||
$this->assertText($title, 'Block title was displayed when enabled.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a block to a given region via the UI and confirms the result.
|
||||
*
|
||||
* @param array $block
|
||||
* An array of information about the block, including the following keys:
|
||||
* - module: The module providing the block.
|
||||
* - title: The title of the block.
|
||||
* - delta: The block's delta key.
|
||||
* @param string $region
|
||||
* The machine name of the theme region to move the block to, for example
|
||||
* 'header' or 'sidebar_first'.
|
||||
*/
|
||||
function moveBlockToRegion(array $block, $region) {
|
||||
// Set the created block to a specific region.
|
||||
$block += array('theme' => $this->config('system.theme')->get('default'));
|
||||
$edit = array();
|
||||
$edit['blocks[' . $block['id'] . '][region]'] = $region;
|
||||
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
|
||||
|
||||
// Confirm that the block was moved to the proper region.
|
||||
$this->assertText(t('The block settings have been updated.'), format_string('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
|
||||
|
||||
// Confirm that the block is being displayed.
|
||||
$this->drupalGet('');
|
||||
$this->assertText(t($block['settings[label]']), 'Block successfully being displayed on the page.');
|
||||
|
||||
// Confirm that the custom block was found at the proper region.
|
||||
$xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
|
||||
':region-class' => 'region region-' . Html::getClass($region),
|
||||
':block-id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
|
||||
));
|
||||
$this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', array('%region_name' => Html::getClass($region))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that cache tags are properly set and bubbled up to the page cache.
|
||||
*
|
||||
* Verify that invalidation of these cache tags works:
|
||||
* - "block:<block ID>"
|
||||
* - "block_plugin:<block plugin ID>"
|
||||
*/
|
||||
public function testBlockCacheTags() {
|
||||
// The page cache only works for anonymous users.
|
||||
$this->drupalLogout();
|
||||
|
||||
// Enable page caching.
|
||||
$config = $this->config('system.performance');
|
||||
$config->set('cache.page.max_age', 300);
|
||||
$config->save();
|
||||
|
||||
// Place the "Powered by Drupal" block.
|
||||
$block = $this->drupalPlaceBlock('system_powered_by_block', array('id' => 'powered'));
|
||||
|
||||
// Prime the page cache.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
|
||||
|
||||
// Verify a cache hit, but also the presence of the correct cache tags in
|
||||
// both the page and block caches.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
|
||||
$cid_parts = array(\Drupal::url('<front>', array(), array('absolute' => TRUE)), 'html');
|
||||
$cid = implode(':', $cid_parts);
|
||||
$cache_entry = \Drupal::cache('render')->get($cid);
|
||||
$expected_cache_tags = array(
|
||||
'config:block_list',
|
||||
'block_view',
|
||||
'config:block.block.powered',
|
||||
'config:user.role.anonymous',
|
||||
'rendered',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
|
||||
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
|
||||
$cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
|
||||
$expected_cache_tags = array(
|
||||
'block_view',
|
||||
'config:block.block.powered',
|
||||
'rendered',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
|
||||
|
||||
// The "Powered by Drupal" block is modified; verify a cache miss.
|
||||
$block->setRegion('content');
|
||||
$block->save();
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
|
||||
|
||||
// Now we should have a cache hit again.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
|
||||
|
||||
// Place the "Powered by Drupal" block another time; verify a cache miss.
|
||||
$block_2 = $this->drupalPlaceBlock('system_powered_by_block', array('id' => 'powered-2'));
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
|
||||
|
||||
// Verify a cache hit, but also the presence of the correct cache tags.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
|
||||
$cid_parts = array(\Drupal::url('<front>', array(), array('absolute' => TRUE)), 'html');
|
||||
$cid = implode(':', $cid_parts);
|
||||
$cache_entry = \Drupal::cache('render')->get($cid);
|
||||
$expected_cache_tags = array(
|
||||
'config:block_list',
|
||||
'block_view',
|
||||
'config:block.block.powered',
|
||||
'config:block.block.powered-2',
|
||||
'config:user.role.anonymous',
|
||||
'rendered',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$this->assertEqual($cache_entry->tags, $expected_cache_tags);
|
||||
$expected_cache_tags = array(
|
||||
'block_view',
|
||||
'config:block.block.powered',
|
||||
'rendered',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
|
||||
$cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
|
||||
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
|
||||
$expected_cache_tags = array(
|
||||
'block_view',
|
||||
'config:block.block.powered-2',
|
||||
'rendered',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
|
||||
$cache_entry = \Drupal::cache('render')->get('entity_view:block:powered-2:' . implode(':', $keys));
|
||||
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
|
||||
|
||||
// Now we should have a cache hit again.
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
|
||||
|
||||
// Delete the "Powered by Drupal" blocks; verify a cache miss.
|
||||
entity_delete_multiple('block', array('powered', 'powered-2'));
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a link exists to block layout from the appearance form.
|
||||
*/
|
||||
public function testThemeAdminLink() {
|
||||
$this->drupalPlaceBlock('help_block', ['region' => 'help']);
|
||||
$theme_admin = $this->drupalCreateUser([
|
||||
'administer blocks',
|
||||
'administer themes',
|
||||
'access administration pages',
|
||||
]);
|
||||
$this->drupalLogin($theme_admin);
|
||||
$this->drupalGet('admin/appearance');
|
||||
$this->assertText('You can place blocks for each theme on the block layout page');
|
||||
$this->assertLinkByHref('admin/structure/block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that uninstalling a theme removes its block configuration.
|
||||
*/
|
||||
public function testUninstallTheme() {
|
||||
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
|
||||
$theme_handler = \Drupal::service('theme_handler');
|
||||
|
||||
$theme_handler->install(['seven']);
|
||||
$theme_handler->setDefault('seven');
|
||||
$block = $this->drupalPlaceBlock('system_powered_by_block', ['theme' => 'seven', 'region' => 'help']);
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertText('Powered by Drupal');
|
||||
|
||||
$theme_handler->setDefault('classy');
|
||||
$theme_handler->uninstall(['seven']);
|
||||
|
||||
// Ensure that the block configuration does not exist anymore.
|
||||
$this->assertIdentical(NULL, Block::load($block->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block access.
|
||||
*/
|
||||
public function testBlockAccess() {
|
||||
$this->drupalPlaceBlock('test_access', ['region' => 'help']);
|
||||
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertNoText('Hello test world');
|
||||
|
||||
\Drupal::state()->set('test_block_access', TRUE);
|
||||
$this->drupalGet('<front>');
|
||||
$this->assertText('Hello test world');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests block_user_role_delete.
|
||||
*/
|
||||
public function testBlockUserRoleDelete() {
|
||||
$role1 = Role::create(['id' => 'test_role1', 'name' => $this->randomString()]);
|
||||
$role1->save();
|
||||
|
||||
$role2 = Role::create(['id' => 'test_role2', 'name' => $this->randomString()]);
|
||||
$role2->save();
|
||||
|
||||
$block = Block::create([
|
||||
'id' => $this->randomMachineName(),
|
||||
'plugin' => 'system_powered_by_block',
|
||||
]);
|
||||
|
||||
$block->setVisibilityConfig('user_role', [
|
||||
'roles' => [
|
||||
$role1->id() => $role1->id(),
|
||||
$role2->id() => $role2->id(),
|
||||
],
|
||||
]);
|
||||
|
||||
$block->save();
|
||||
|
||||
$this->assertEqual($block->getVisibility()['user_role']['roles'], [
|
||||
$role1->id() => $role1->id(),
|
||||
$role2->id() => $role2->id()
|
||||
]);
|
||||
|
||||
$role1->delete();
|
||||
|
||||
$block = Block::load($block->id());
|
||||
$this->assertEqual($block->getVisibility()['user_role']['roles'], [
|
||||
$role2->id() => $role2->id()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
71
web/core/modules/block/src/Tests/BlockTestBase.php
Normal file
71
web/core/modules/block/src/Tests/BlockTestBase.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
|
||||
/**
|
||||
* Provides setup and helper methods for block module tests.
|
||||
*/
|
||||
abstract class BlockTestBase extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'filter', 'test_page_test', 'help', 'block_test');
|
||||
|
||||
/**
|
||||
* A list of theme regions to test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $regions;
|
||||
|
||||
/**
|
||||
* A test user with administrative privileges.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Use the test page as the front page.
|
||||
$this->config('system.site')->set('page.front', '/test-page')->save();
|
||||
|
||||
// Create Full HTML text format.
|
||||
$full_html_format = FilterFormat::create(array(
|
||||
'format' => 'full_html',
|
||||
'name' => 'Full HTML',
|
||||
));
|
||||
$full_html_format->save();
|
||||
|
||||
// Create and log in an administrative user having access to the Full HTML
|
||||
// text format.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
$full_html_format->getPermissionName(),
|
||||
'access administration pages',
|
||||
));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Define the existing regions.
|
||||
$this->regions = array(
|
||||
'header',
|
||||
'sidebar_first',
|
||||
'content',
|
||||
'sidebar_second',
|
||||
'footer',
|
||||
);
|
||||
$block_storage = $this->container->get('entity_type.manager')->getStorage('block');
|
||||
$blocks = $block_storage->loadByProperties(array('theme' => $this->config('system.theme')->get('default')));
|
||||
foreach ($blocks as $block) {
|
||||
$block->delete();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
312
web/core/modules/block/src/Tests/BlockUiTest.php
Normal file
312
web/core/modules/block/src/Tests/BlockUiTest.php
Normal file
|
@ -0,0 +1,312 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the block configuration UI exists and stores data correctly.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockUiTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'block_test', 'help', 'condition_test');
|
||||
|
||||
protected $regions;
|
||||
|
||||
/**
|
||||
* The submitted block values used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $blockValues;
|
||||
|
||||
/**
|
||||
* The block entities used by this test.
|
||||
*
|
||||
* @var \Drupal\block\BlockInterface[]
|
||||
*/
|
||||
protected $blocks;
|
||||
|
||||
/**
|
||||
* An administrative user to configure the test environment.
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Create and log in an administrative user.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
'access administration pages',
|
||||
));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Enable some test blocks.
|
||||
$this->blockValues = array(
|
||||
array(
|
||||
'label' => 'Tools',
|
||||
'tr' => '5',
|
||||
'plugin_id' => 'system_menu_block:tools',
|
||||
'settings' => array('region' => 'sidebar_second', 'id' => 'tools'),
|
||||
'test_weight' => '-1',
|
||||
),
|
||||
array(
|
||||
'label' => 'Powered by Drupal',
|
||||
'tr' => '16',
|
||||
'plugin_id' => 'system_powered_by_block',
|
||||
'settings' => array('region' => 'footer', 'id' => 'powered'),
|
||||
'test_weight' => '0',
|
||||
),
|
||||
);
|
||||
$this->blocks = array();
|
||||
foreach ($this->blockValues as $values) {
|
||||
$this->blocks[] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test block demo page exists and functions correctly.
|
||||
*/
|
||||
public function testBlockDemoUiPage() {
|
||||
$this->drupalPlaceBlock('help_block', array('region' => 'help'));
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLink(t('Demonstrate block regions (@theme)', array('@theme' => 'Classy')));
|
||||
$elements = $this->xpath('//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), :title)]', array(':title' => 'Highlighted'));
|
||||
$this->assertTrue(!empty($elements), 'Block demo regions are shown.');
|
||||
|
||||
\Drupal::service('theme_handler')->install(array('test_theme'));
|
||||
$this->drupalGet('admin/structure/block/demo/test_theme');
|
||||
$this->assertEscaped('<strong>Test theme</strong>');
|
||||
|
||||
\Drupal::service('theme_handler')->install(['stable']);
|
||||
$this->drupalGet('admin/structure/block/demo/stable');
|
||||
$this->assertResponse(404, 'Hidden themes that are not the default theme are not supported by the block demo screen');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test block admin page exists and functions correctly.
|
||||
*/
|
||||
function testBlockAdminUiPage() {
|
||||
// Visit the blocks admin ui.
|
||||
$this->drupalGet('admin/structure/block');
|
||||
// Look for the blocks table.
|
||||
$blocks_table = $this->xpath("//table[@id='blocks']");
|
||||
$this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
|
||||
// Look for test blocks in the table.
|
||||
foreach ($this->blockValues as $delta => $values) {
|
||||
$block = $this->blocks[$delta];
|
||||
$label = $block->label();
|
||||
$element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
|
||||
$this->assertTrue((string) $element[0] == $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
|
||||
// Look for a test block region select form element.
|
||||
$this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
|
||||
// Move the test block to the header region.
|
||||
$edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
|
||||
// Look for a test block weight select form element.
|
||||
$this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
|
||||
// Change the test block's weight.
|
||||
$edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
|
||||
}
|
||||
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
|
||||
foreach ($this->blockValues as $values) {
|
||||
// Check if the region and weight settings changes have persisted.
|
||||
$this->assertOptionSelected(
|
||||
'edit-blocks-' . $values['settings']['id'] . '-region',
|
||||
'header',
|
||||
'The block "' . $label . '" has the correct region assignment (header).'
|
||||
);
|
||||
$this->assertOptionSelected(
|
||||
'edit-blocks-' . $values['settings']['id'] . '-weight',
|
||||
$values['test_weight'],
|
||||
'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').'
|
||||
);
|
||||
}
|
||||
|
||||
// Add a block with a machine name the same as a region name.
|
||||
$this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']);
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']);
|
||||
$this->assertTrue(!empty($element));
|
||||
|
||||
// Ensure hidden themes do not appear in the UI. Enable another non base
|
||||
// theme and place the local tasks block.
|
||||
$this->assertTrue(\Drupal::service('theme_handler')->themeExists('classy'), 'The classy base theme is enabled');
|
||||
$this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']);
|
||||
\Drupal::service('theme_installer')->install(['stable', 'stark']);
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$theme_handler = \Drupal::service('theme_handler');
|
||||
$this->assertLink($theme_handler->getName('classy'));
|
||||
$this->assertLink($theme_handler->getName('stark'));
|
||||
$this->assertNoLink($theme_handler->getName('stable'));
|
||||
|
||||
$this->drupalGet('admin/structure/block/list/stable');
|
||||
$this->assertResponse(404, 'Placing blocks through UI is not possible for a hidden base theme.');
|
||||
|
||||
\Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save();
|
||||
\Drupal::service('router.builder')->rebuildIfNeeded();
|
||||
$this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']);
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->assertLink($theme_handler->getName('stable'));
|
||||
$this->drupalGet('admin/structure/block/list/stable');
|
||||
$this->assertResponse(200, 'Placing blocks through UI is possible for a hidden base theme that is the admin theme.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block categories on the listing page.
|
||||
*/
|
||||
public function testCandidateBlockList() {
|
||||
$arguments = array(
|
||||
':title' => 'Display message',
|
||||
':category' => 'Block test',
|
||||
':href' => 'admin/structure/block/add/test_block_instantiation/classy',
|
||||
);
|
||||
$pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
|
||||
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The test block appears in the category for its module.');
|
||||
|
||||
// Trigger the custom category addition in block_test_block_alter().
|
||||
$this->container->get('state')->set('block_test_info_alter', TRUE);
|
||||
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
|
||||
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$arguments[':category'] = 'Custom category';
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the behavior of unsatisfied context-aware blocks.
|
||||
*/
|
||||
public function testContextAwareUnsatisfiedBlocks() {
|
||||
$arguments = array(
|
||||
':category' => 'Block test',
|
||||
':href' => 'admin/structure/block/add/test_context_aware_unsatisfied/classy',
|
||||
':text' => 'Test context-aware unsatisfied block',
|
||||
);
|
||||
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$elements = $this->xpath('//tr[.//td/div[text()=:text] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]', $arguments);
|
||||
$this->assertTrue(empty($elements), 'The context-aware test block does not appear.');
|
||||
|
||||
$definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware_unsatisfied');
|
||||
$this->assertTrue(!empty($definition), 'The context-aware test block does not exist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the behavior of context-aware blocks.
|
||||
*/
|
||||
public function testContextAwareBlocks() {
|
||||
$expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()->getUsername() . '</div>';
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText('Test context-aware block');
|
||||
$this->assertNoRaw($expected_text);
|
||||
|
||||
$block_url = 'admin/structure/block/add/test_context_aware/classy';
|
||||
$arguments = array(
|
||||
':title' => 'Test context-aware block',
|
||||
':category' => 'Block test',
|
||||
':href' => $block_url,
|
||||
);
|
||||
$pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
|
||||
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The context-aware test block appears.');
|
||||
$definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
|
||||
$this->assertTrue(!empty($definition), 'The context-aware test block exists.');
|
||||
$edit = [
|
||||
'region' => 'content',
|
||||
'settings[context_mapping][user]' => '@block_test.multiple_static_context:user2',
|
||||
];
|
||||
$this->drupalPostForm($block_url, $edit, 'Save block');
|
||||
|
||||
$this->drupalGet('');
|
||||
$this->assertText('Test context-aware block');
|
||||
$this->assertText('User context found.');
|
||||
$this->assertRaw($expected_text);
|
||||
|
||||
// Test context mapping allows empty selection for optional contexts.
|
||||
$this->drupalGet('admin/structure/block/manage/testcontextawareblock');
|
||||
$edit = [
|
||||
'settings[context_mapping][user]' => '',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, 'Save block');
|
||||
$this->drupalGet('');
|
||||
$this->assertText('No context mapping selected.');
|
||||
$this->assertNoText('User context found.');
|
||||
|
||||
// Tests that conditions with missing context are not displayed.
|
||||
$this->drupalGet('admin/structure/block/manage/testcontextawareblock');
|
||||
$this->assertNoRaw('No existing type');
|
||||
$this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the BlockForm populates machine name correctly.
|
||||
*/
|
||||
public function testMachineNameSuggestion() {
|
||||
$url = 'admin/structure/block/add/test_block_instantiation/classy';
|
||||
$this->drupalGet($url);
|
||||
$this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
|
||||
$this->drupalPostForm($url, array(), 'Save block');
|
||||
|
||||
// Now, check to make sure the form starts by autoincrementing correctly.
|
||||
$this->drupalGet($url);
|
||||
$this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
|
||||
$this->drupalPostForm($url, array(), 'Save block');
|
||||
|
||||
// And verify that it continues working beyond just the first two.
|
||||
$this->drupalGet($url);
|
||||
$this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block placement indicator.
|
||||
*/
|
||||
public function testBlockPlacementIndicator() {
|
||||
// Select the 'Powered by Drupal' block to be placed.
|
||||
$block = array();
|
||||
$block['id'] = strtolower($this->randomMachineName());
|
||||
$block['theme'] = 'classy';
|
||||
$block['region'] = 'content';
|
||||
|
||||
// After adding a block, it will indicate which block was just added.
|
||||
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
|
||||
$this->assertUrl('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
|
||||
|
||||
// Resaving the block page will remove the block indicator.
|
||||
$this->drupalPostForm(NULL, array(), t('Save blocks'));
|
||||
$this->assertUrl('admin/structure/block/list/classy');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if validation errors are passed plugin form to the parent form.
|
||||
*/
|
||||
public function testBlockValidateErrors() {
|
||||
$this->drupalPostForm('admin/structure/block/add/test_settings_validation/classy', ['settings[digits]' => 'abc'], t('Save block'));
|
||||
|
||||
$arguments = [':message' => 'Only digits are allowed'];
|
||||
$pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]';
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue($elements, 'Plugin error message found in parent form.');
|
||||
|
||||
$error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]';
|
||||
$error_class = $this->xpath($error_class_pattern);
|
||||
$this->assertTrue($error_class, 'Plugin error class found in parent form.');
|
||||
}
|
||||
|
||||
}
|
167
web/core/modules/block/src/Tests/BlockXssTest.php
Normal file
167
web/core/modules/block/src/Tests/BlockXssTest.php
Normal file
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\block_content\Entity\BlockContent;
|
||||
use Drupal\block_content\Entity\BlockContentType;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\system\Entity\Menu;
|
||||
use Drupal\views\Entity\View;
|
||||
|
||||
/**
|
||||
* Tests that the block module properly escapes block descriptions.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockXssTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['block', 'block_content', 'menu_ui', 'views'];
|
||||
|
||||
/**
|
||||
* Tests that nothing is escaped other than the blocks explicitly tested.
|
||||
*/
|
||||
public function testNoUnexpectedEscaping() {
|
||||
$this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$this->assertNoEscaped('<');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests XSS in title.
|
||||
*/
|
||||
public function testXssInTitle() {
|
||||
$this->container->get('module_installer')->install(['block_test']);
|
||||
$this->drupalPlaceBlock('test_xss_title', ['label' => '<script>alert("XSS label");</script>']);
|
||||
|
||||
\Drupal::state()->set('block_test.content', $this->randomMachineName());
|
||||
$this->drupalGet('');
|
||||
$this->assertNoRaw('<script>alert("XSS label");</script>', 'The block title was properly sanitized when rendered.');
|
||||
|
||||
$this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$this->drupalGet('admin/structure/block/list/' . $default_theme);
|
||||
$this->assertNoRaw("<script>alert('XSS subject');</script>", 'The block title was properly sanitized in Block Plugin UI Admin page.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests XSS in category.
|
||||
*/
|
||||
public function testXssInCategory() {
|
||||
$this->container->get('module_installer')->install(['block_test']);
|
||||
$this->drupalPlaceBlock('test_xss_title');
|
||||
$this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$this->assertNoRaw("<script>alert('XSS category');</script>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests various modules that provide blocks for XSS.
|
||||
*/
|
||||
public function testBlockXss() {
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
$this->doViewTest();
|
||||
$this->doMenuTest();
|
||||
$this->doBlockContentTest();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$this->assertNoRaw('&lt;', 'The page does not have double escaped HTML tags.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests XSS coming from View block labels.
|
||||
*/
|
||||
protected function doViewTest() {
|
||||
// Create a View without a custom label for its block Display. The
|
||||
// admin_label of the block then becomes just the View's label.
|
||||
$view = View::create([
|
||||
'id' => $this->randomMachineName(),
|
||||
'label' => '<script>alert("view1");</script>',
|
||||
]);
|
||||
$view->addDisplay('block');
|
||||
$view->save();
|
||||
|
||||
// Create a View with a custom label for its block Display. The
|
||||
// admin_label of the block then becomes the View's label combined with
|
||||
// the Display's label.
|
||||
$view = View::create([
|
||||
'id' => $this->randomMachineName(),
|
||||
'label' => '<script>alert("view2");</script>',
|
||||
]);
|
||||
$view->addDisplay('block', 'Fish & chips');
|
||||
$view->save();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
|
||||
// \Drupal\views\Plugin\Derivative\ViewsBlock::getDerivativeDefinitions()
|
||||
// has a different code path for an admin label based only on the View
|
||||
// label versus one based on both the View label and the Display label.
|
||||
// Ensure that this test is covering both code paths by asserting the
|
||||
// absence of a ":" for the first View and the presence of a ":" for the
|
||||
// second one. Note that the second assertion is redundant with the one
|
||||
// further down which also checks for the Display label, but is included
|
||||
// here for clarity.
|
||||
$this->assertNoEscaped('<script>alert("view1");</script>:');
|
||||
$this->assertEscaped('<script>alert("view2");</script>:');
|
||||
|
||||
// Assert that the blocks have their admin labels escaped and
|
||||
// don't appear anywhere unescaped.
|
||||
$this->assertEscaped('<script>alert("view1");</script>');
|
||||
$this->assertNoRaw('<script>alert("view1");</script>');
|
||||
$this->assertEscaped('<script>alert("view2");</script>: Fish & chips');
|
||||
$this->assertNoRaw('<script>alert("view2");</script>');
|
||||
$this->assertNoRaw('Fish & chips');
|
||||
|
||||
// Assert the Display label doesn't appear anywhere double escaped.
|
||||
$this->assertNoRaw('Fish & chips');
|
||||
$this->assertNoRaw('Fish &amp; chips');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests XSS coming from Menu block labels.
|
||||
*/
|
||||
protected function doMenuTest() {
|
||||
Menu::create([
|
||||
'id' => $this->randomMachineName(),
|
||||
'label' => '<script>alert("menu");</script>',
|
||||
])->save();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
|
||||
$this->assertEscaped('<script>alert("menu");</script>');
|
||||
$this->assertNoRaw('<script>alert("menu");</script>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests XSS coming from Block Content block info.
|
||||
*/
|
||||
protected function doBlockContentTest() {
|
||||
BlockContentType::create([
|
||||
'id' => 'basic',
|
||||
'label' => 'basic',
|
||||
'revision' => TRUE,
|
||||
])->save();
|
||||
BlockContent::create([
|
||||
'type' => 'basic',
|
||||
'info' => '<script>alert("block_content");</script>',
|
||||
])->save();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('block.admin_display'));
|
||||
$this->clickLinkPartialName('Place block');
|
||||
|
||||
$this->assertEscaped('<script>alert("block_content");</script>');
|
||||
$this->assertNoRaw('<script>alert("block_content");</script>');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the new default theme gets blocks.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class NewDefaultThemeBlocksTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block');
|
||||
|
||||
/**
|
||||
* Check the enabled Bartik blocks are correctly copied over.
|
||||
*/
|
||||
function testNewDefaultThemeBlocks() {
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
|
||||
// Add two instances of the user login block.
|
||||
$this->drupalPlaceBlock('user_login_block', array(
|
||||
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
|
||||
));
|
||||
$this->drupalPlaceBlock('user_login_block', array(
|
||||
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
|
||||
));
|
||||
|
||||
// Add an instance of a different block.
|
||||
$this->drupalPlaceBlock('system_powered_by_block', array(
|
||||
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
|
||||
));
|
||||
|
||||
// Install a different theme.
|
||||
$new_theme = 'bartik';
|
||||
$this->assertFalse($new_theme == $default_theme, 'The new theme is different from the previous default theme.');
|
||||
\Drupal::service('theme_handler')->install(array($new_theme));
|
||||
$this->config('system.theme')
|
||||
->set('default', $new_theme)
|
||||
->save();
|
||||
|
||||
// Ensure that the new theme has all the blocks as the previous default.
|
||||
$default_block_names = $this->container->get('entity.query')->get('block')
|
||||
->condition('theme', $default_theme)
|
||||
->execute();
|
||||
$new_blocks = $this->container->get('entity.query')->get('block')
|
||||
->condition('theme', $new_theme)
|
||||
->execute();
|
||||
$this->assertTrue(count($default_block_names) == count($new_blocks), 'The new default theme has the same number of blocks as the previous theme.');
|
||||
foreach ($default_block_names as $default_block_name) {
|
||||
// Remove the matching block from the list of blocks in the new theme.
|
||||
// E.g., if the old theme has block.block.stark_admin,
|
||||
// unset block.block.bartik_admin.
|
||||
unset($new_blocks[str_replace($default_theme . '_', $new_theme . '_', $default_block_name)]);
|
||||
}
|
||||
$this->assertTrue(empty($new_blocks), 'The new theme has exactly the same blocks as the previous default theme.');
|
||||
|
||||
// Install a hidden base theme and ensure blocks are not copied.
|
||||
$base_theme = 'test_basetheme';
|
||||
\Drupal::service('theme_handler')->install([$base_theme]);
|
||||
$new_blocks = $this->container->get('entity.query')->get('block')
|
||||
->condition('theme', $base_theme)
|
||||
->execute();
|
||||
$this->assertTrue(empty($new_blocks), 'Installing a hidden base theme does not copy blocks from the default theme.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the block administration page for a non-default theme.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class NonDefaultBlockAdminTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalPlaceBlock('local_tasks_block');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test non-default theme admin.
|
||||
*/
|
||||
function testNonDefaultBlockAdmin() {
|
||||
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes'));
|
||||
$this->drupalLogin($admin_user);
|
||||
$new_theme = 'bartik';
|
||||
\Drupal::service('theme_handler')->install(array($new_theme));
|
||||
$this->drupalGet('admin/structure/block/list/' . $new_theme);
|
||||
$this->assertText('Bartik(' . t('active tab') . ')', 'Tab for non-default theme found.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests\Update;
|
||||
|
||||
use Drupal\system\Tests\Update\UpdatePathTestBase;
|
||||
|
||||
/**
|
||||
* Tests the upgrade path for block with conditions missing context.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2811519
|
||||
*
|
||||
* @group Update
|
||||
*/
|
||||
class BlockConditionMissingSchemaUpdateTest extends UpdatePathTestBase {
|
||||
|
||||
/**
|
||||
* This test does not have a failed update but the configuration has missing
|
||||
* schema so can not do the full post update testing offered by
|
||||
* UpdatePathTestBase.
|
||||
*
|
||||
* @var bool
|
||||
*
|
||||
* @see \Drupal\system\Tests\Update\UpdatePathTestBase::runUpdates()
|
||||
*/
|
||||
protected $checkFailedUpdates = FALSE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['block_test', 'language'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setDatabaseDumpFiles() {
|
||||
$this->databaseDumpFiles = [
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
|
||||
__DIR__ . '/../../../tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that block context mapping is updated properly.
|
||||
*/
|
||||
public function testUpdateHookN() {
|
||||
$this->runUpdates();
|
||||
$this->drupalGet('<front>');
|
||||
// If the block is fixed by block_post_update_fix_negate_in_conditions()
|
||||
// then it will be visible.
|
||||
$this->assertText('Test missing schema on conditions');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests\Update;
|
||||
|
||||
/**
|
||||
* Runs BlockContextMappingUpdateTest with a dump filled with content.
|
||||
*
|
||||
* @group Update
|
||||
*/
|
||||
class BlockContextMappingUpdateFilledTest extends BlockContextMappingUpdateTest {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setDatabaseDumpFiles() {
|
||||
parent::setDatabaseDumpFiles();
|
||||
$this->databaseDumpFiles[0] = __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.filled.standard.php.gz';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests\Update;
|
||||
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\system\Tests\Update\UpdatePathTestBase;
|
||||
|
||||
/**
|
||||
* Tests the upgrade path for block context mapping renames.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2354889
|
||||
*
|
||||
* @group Update
|
||||
*/
|
||||
class BlockContextMappingUpdateTest extends UpdatePathTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['block_test', 'language'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setDatabaseDumpFiles() {
|
||||
$this->databaseDumpFiles = [
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.block-context-manager-2354889.php',
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.language-enabled.php',
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.block-test-enabled.php',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that block context mapping is updated properly.
|
||||
*/
|
||||
public function testUpdateHookN() {
|
||||
$this->runUpdates();
|
||||
$this->assertRaw('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:<ul><li>User login (Visibility: Baloney spam)</li></ul>');
|
||||
|
||||
// Disable maintenance mode.
|
||||
\Drupal::state()->set('system.maintenance_mode', FALSE);
|
||||
|
||||
// We finished updating so we can log in the user now.
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
// The block that we are testing has the following visibility rules:
|
||||
// - only visible on node pages
|
||||
// - only visible to authenticated users.
|
||||
$block_title = 'Test for 2354889';
|
||||
|
||||
// Create two nodes, a page and an article.
|
||||
$page = Node::create([
|
||||
'type' => 'page',
|
||||
'title' => 'Page node',
|
||||
]);
|
||||
$page->save();
|
||||
|
||||
$article = Node::create([
|
||||
'type' => 'article',
|
||||
'title' => 'Article node',
|
||||
]);
|
||||
$article->save();
|
||||
|
||||
// Check that the block appears only on Page nodes for authenticated users.
|
||||
$this->drupalGet('node/' . $page->id());
|
||||
$this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.');
|
||||
|
||||
$this->drupalGet('node/' . $article->id());
|
||||
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Check that the block does not appear on any page for anonymous users.
|
||||
$this->drupalGet('node/' . $page->id());
|
||||
$this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.');
|
||||
|
||||
$this->drupalGet('node/' . $article->id());
|
||||
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
|
||||
|
||||
// Ensure that all the context mappings got updated properly.
|
||||
$block = Block::load('testfor2354889');
|
||||
$visibility = $block->get('visibility');
|
||||
$this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']);
|
||||
$this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
|
||||
$this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']);
|
||||
|
||||
// Check that a block with invalid context is being disabled and that it can
|
||||
// still be edited afterward.
|
||||
$disabled_block = Block::load('thirdtestfor2354889');
|
||||
$this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled');
|
||||
|
||||
$this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney_spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
|
||||
|
||||
$disabled_block_visibility = $disabled_block->get('visibility');
|
||||
$this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
|
||||
|
||||
$admin_user = $this->drupalCreateUser(['administer blocks']);
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
$this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
|
||||
$this->assertResponse('200');
|
||||
}
|
||||
|
||||
}
|
371
web/core/modules/block/src/Tests/Views/DisplayBlockTest.php
Normal file
371
web/core/modules/block/src/Tests/Views/DisplayBlockTest.php
Normal file
|
@ -0,0 +1,371 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Tests\Views;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
||||
use Drupal\views\Entity\View;
|
||||
use Drupal\views\Views;
|
||||
use Drupal\views\Tests\ViewTestBase;
|
||||
use Drupal\views\Tests\ViewTestData;
|
||||
use Drupal\Core\Template\Attribute;
|
||||
|
||||
/**
|
||||
* Tests the block display plugin.
|
||||
*
|
||||
* @group block
|
||||
* @see \Drupal\block\Plugin\views\display\Block
|
||||
*/
|
||||
class DisplayBlockTest extends ViewTestBase {
|
||||
|
||||
use AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'block_test_views', 'test_page_test', 'contextual', 'views_ui');
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_view_block', 'test_view_block2');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
ViewTestData::createTestViews(get_class($this), array('block_test_views'));
|
||||
$this->enableViewsTestModule();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests default and custom block categories.
|
||||
*/
|
||||
public function testBlockCategory() {
|
||||
$this->drupalLogin($this->drupalCreateUser(array('administer views', 'administer blocks')));
|
||||
|
||||
// Create a new view in the UI.
|
||||
$edit = array();
|
||||
$edit['label'] = $this->randomString();
|
||||
$edit['id'] = strtolower($this->randomMachineName());
|
||||
$edit['show[wizard_key]'] = 'standard:views_test_data';
|
||||
$edit['description'] = $this->randomString();
|
||||
$edit['block[create]'] = TRUE;
|
||||
$edit['block[style][row_plugin]'] = 'fields';
|
||||
$this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit'));
|
||||
|
||||
$pattern = '//tr[.//td[text()=:category] and .//td//a[contains(@href, :href)]]';
|
||||
|
||||
// Test that the block was given a default category corresponding to its
|
||||
// base table.
|
||||
$arguments = array(
|
||||
':href' => \Drupal::Url('block.admin_add', array(
|
||||
'plugin_id' => 'views_block:' . $edit['id'] . '-block_1',
|
||||
'theme' => 'classy',
|
||||
)),
|
||||
':category' => t('Lists (Views)'),
|
||||
);
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The test block appears in the category for its base table.');
|
||||
|
||||
// Duplicate the block before changing the category.
|
||||
$this->drupalPostForm('admin/structure/views/view/' . $edit['id'] . '/edit/block_1', array(), t('Duplicate @display_title', array('@display_title' => 'Block')));
|
||||
$this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_2');
|
||||
|
||||
// Change the block category to a random string.
|
||||
$this->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
|
||||
$link = $this->xpath('//a[@id="views-block-1-block-category" and normalize-space(text())=:category]', $arguments);
|
||||
$this->assertTrue(!empty($link));
|
||||
$this->clickLink(t('Lists (Views)'));
|
||||
$category = $this->randomString();
|
||||
$this->drupalPostForm(NULL, array('block_category' => $category), t('Apply'));
|
||||
|
||||
// Duplicate the block after changing the category.
|
||||
$this->drupalPostForm(NULL, array(), t('Duplicate @display_title', array('@display_title' => 'Block')));
|
||||
$this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_3');
|
||||
|
||||
$this->drupalPostForm(NULL, array(), t('Save'));
|
||||
|
||||
// Test that the blocks are listed under the correct categories.
|
||||
$arguments[':category'] = $category;
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$this->clickLinkPartialName('Place block');
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The test block appears in the custom category.');
|
||||
|
||||
$arguments = array(
|
||||
':href' => \Drupal::Url('block.admin_add', array(
|
||||
'plugin_id' => 'views_block:' . $edit['id'] . '-block_2',
|
||||
'theme' => 'classy',
|
||||
)),
|
||||
':category' => t('Lists (Views)'),
|
||||
);
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The first duplicated test block remains in the original category.');
|
||||
|
||||
$arguments = array(
|
||||
':href' => \Drupal::Url('block.admin_add', array(
|
||||
'plugin_id' => 'views_block:' . $edit['id'] . '-block_3',
|
||||
'theme' => 'classy',
|
||||
)),
|
||||
':category' => $category,
|
||||
);
|
||||
$elements = $this->xpath($pattern, $arguments);
|
||||
$this->assertTrue(!empty($elements), 'The second duplicated test block appears in the custom category.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests removing a block display.
|
||||
*/
|
||||
public function testDeleteBlockDisplay() {
|
||||
// To test all combinations possible we first place create two instances
|
||||
// of the block display of the first view.
|
||||
$block_1 = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('label' => 'test_view_block-block_1:1'));
|
||||
$block_2 = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('label' => 'test_view_block-block_1:2'));
|
||||
|
||||
// Then we add one instance of blocks for each of the two displays of the
|
||||
// second view.
|
||||
$block_3 = $this->drupalPlaceBlock('views_block:test_view_block2-block_1', array('label' => 'test_view_block2-block_1'));
|
||||
$block_4 = $this->drupalPlaceBlock('views_block:test_view_block2-block_2', array('label' => 'test_view_block2-block_2'));
|
||||
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertBlockAppears($block_1);
|
||||
$this->assertBlockAppears($block_2);
|
||||
$this->assertBlockAppears($block_3);
|
||||
$this->assertBlockAppears($block_4);
|
||||
|
||||
$block_storage = $this->container->get('entity_type.manager')->getStorage('block');
|
||||
|
||||
// Remove the block display, so both block entities from the first view
|
||||
// should both disappear.
|
||||
$view = Views::getView('test_view_block');
|
||||
$view->initDisplay();
|
||||
$view->displayHandlers->remove('block_1');
|
||||
$view->storage->save();
|
||||
|
||||
$this->assertFalse($block_storage->load($block_1->id()), 'The block for this display was removed.');
|
||||
$this->assertFalse($block_storage->load($block_2->id()), 'The block for this display was removed.');
|
||||
$this->assertTrue($block_storage->load($block_3->id()), 'A block from another view was unaffected.');
|
||||
$this->assertTrue($block_storage->load($block_4->id()), 'A block from another view was unaffected.');
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertNoBlockAppears($block_1);
|
||||
$this->assertNoBlockAppears($block_2);
|
||||
$this->assertBlockAppears($block_3);
|
||||
$this->assertBlockAppears($block_4);
|
||||
|
||||
// Remove the first block display of the second view and ensure the block
|
||||
// instance of the second block display still exists.
|
||||
$view = Views::getView('test_view_block2');
|
||||
$view->initDisplay();
|
||||
$view->displayHandlers->remove('block_1');
|
||||
$view->storage->save();
|
||||
|
||||
$this->assertFalse($block_storage->load($block_3->id()), 'The block for this display was removed.');
|
||||
$this->assertTrue($block_storage->load($block_4->id()), 'A block from another display on the same view was unaffected.');
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertNoBlockAppears($block_3);
|
||||
$this->assertBlockAppears($block_4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the block form for a Views block.
|
||||
*/
|
||||
public function testViewsBlockForm() {
|
||||
$this->drupalLogin($this->drupalCreateUser(array('administer blocks')));
|
||||
$default_theme = $this->config('system.theme')->get('default');
|
||||
$this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
|
||||
$elements = $this->xpath('//input[@name="label"]');
|
||||
$this->assertTrue(empty($elements), 'The label field is not found for Views blocks.');
|
||||
// Test that that machine name field is hidden from display and has been
|
||||
// saved as expected from the default value.
|
||||
$this->assertNoFieldById('edit-machine-name', 'views_block__test_view_block_1', 'The machine name is hidden on the views block form.');
|
||||
// Save the block.
|
||||
$this->drupalPostForm(NULL, array(), t('Save block'));
|
||||
$storage = $this->container->get('entity_type.manager')->getStorage('block');
|
||||
$block = $storage->load('views_block__test_view_block_block_1');
|
||||
// This will only return a result if our new block has been created with the
|
||||
// expected machine name.
|
||||
$this->assertTrue(!empty($block), 'The expected block was loaded.');
|
||||
|
||||
for ($i = 2; $i <= 3; $i++) {
|
||||
// Place the same block again and make sure we have a new ID.
|
||||
$this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, array(), t('Save block'));
|
||||
$block = $storage->load('views_block__test_view_block_block_1_' . $i);
|
||||
// This will only return a result if our new block has been created with the
|
||||
// expected machine name.
|
||||
$this->assertTrue(!empty($block), 'The expected block was loaded.');
|
||||
}
|
||||
|
||||
// Tests the override capability of items per page.
|
||||
$this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
|
||||
$edit = array();
|
||||
$edit['settings[override][items_per_page]'] = 10;
|
||||
|
||||
$this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
|
||||
|
||||
$block = $storage->load('views_block__test_view_block_block_1_4');
|
||||
$config = $block->getPlugin()->getConfiguration();
|
||||
$this->assertEqual(10, $config['items_per_page'], "'Items per page' is properly saved.");
|
||||
|
||||
$edit['settings[override][items_per_page]'] = 5;
|
||||
$this->drupalPostForm('admin/structure/block/manage/views_block__test_view_block_block_1_4', $edit, t('Save block'));
|
||||
|
||||
$block = $storage->load('views_block__test_view_block_block_1_4');
|
||||
|
||||
$config = $block->getPlugin()->getConfiguration();
|
||||
$this->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved.");
|
||||
|
||||
// Tests the override of the label capability.
|
||||
$edit = array();
|
||||
$edit['settings[views_label_checkbox]'] = 1;
|
||||
$edit['settings[views_label]'] = 'Custom title';
|
||||
$this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
|
||||
|
||||
$block = $storage->load('views_block__test_view_block_block_1_5');
|
||||
$config = $block->getPlugin()->getConfiguration();
|
||||
$this->assertEqual('Custom title', $config['views_label'], "'Label' is properly saved.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the actual rendering of the views block.
|
||||
*/
|
||||
public function testBlockRendering() {
|
||||
// Create a block and set a custom title.
|
||||
$block = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('label' => 'test_view_block-block_1:1', 'views_label' => 'Custom title'));
|
||||
$this->drupalGet('');
|
||||
|
||||
$result = $this->xpath('//div[contains(@class, "region-sidebar-first")]/div[contains(@class, "block-views")]/h2');
|
||||
$this->assertEqual((string) $result[0], 'Custom title');
|
||||
|
||||
// Don't override the title anymore.
|
||||
$plugin = $block->getPlugin();
|
||||
$plugin->setConfigurationValue('views_label', '');
|
||||
$block->save();
|
||||
|
||||
$this->drupalGet('');
|
||||
$result = $this->xpath('//div[contains(@class, "region-sidebar-first")]/div[contains(@class, "block-views")]/h2');
|
||||
$this->assertEqual((string) $result[0], 'test_view_block');
|
||||
|
||||
// Hide the title.
|
||||
$block->getPlugin()->setConfigurationValue('label_display', FALSE);
|
||||
$block->save();
|
||||
|
||||
$this->drupalGet('');
|
||||
$result = $this->xpath('//div[contains(@class, "region-sidebar-first")]/div[contains(@class, "block-views")]/h2');
|
||||
$this->assertTrue(empty($result), 'The title is not visible.');
|
||||
|
||||
$this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block' , 'rendered']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the various testcases of empty block rendering.
|
||||
*/
|
||||
public function testBlockEmptyRendering() {
|
||||
$url = new Url('test_page_test.test_page');
|
||||
// Remove all views_test_data entries.
|
||||
\Drupal::database()->truncate('views_test_data')->execute();
|
||||
/** @var \Drupal\views\ViewEntityInterface $view */
|
||||
$view = View::load('test_view_block');
|
||||
$view->invalidateCaches();
|
||||
|
||||
$block = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('label' => 'test_view_block-block_1:1', 'views_label' => 'Custom title'));
|
||||
$this->drupalGet('');
|
||||
$this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
|
||||
|
||||
$display = &$view->getDisplay('block_1');
|
||||
$display['display_options']['block_hide_empty'] = TRUE;
|
||||
$view->save();
|
||||
|
||||
$this->drupalGet($url);
|
||||
$this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
|
||||
// Ensure that the view cachability metadata is propagated even, for an
|
||||
// empty block.
|
||||
$this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' , 'rendered']));
|
||||
$this->assertCacheContexts(['url.query_args:_wrapper_format']);
|
||||
|
||||
// Add a header displayed on empty result.
|
||||
$display = &$view->getDisplay('block_1');
|
||||
$display['display_options']['defaults']['header'] = FALSE;
|
||||
$display['display_options']['header']['example'] = [
|
||||
'field' => 'area_text_custom',
|
||||
'id' => 'area_text_custom',
|
||||
'table' => 'views',
|
||||
'plugin_id' => 'text_custom',
|
||||
'content' => 'test header',
|
||||
'empty' => TRUE,
|
||||
];
|
||||
$view->save();
|
||||
|
||||
$this->drupalGet($url);
|
||||
$this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
|
||||
$this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' , 'rendered']));
|
||||
$this->assertCacheContexts(['url.query_args:_wrapper_format']);
|
||||
|
||||
// Hide the header on empty results.
|
||||
$display = &$view->getDisplay('block_1');
|
||||
$display['display_options']['defaults']['header'] = FALSE;
|
||||
$display['display_options']['header']['example'] = [
|
||||
'field' => 'area_text_custom',
|
||||
'id' => 'area_text_custom',
|
||||
'table' => 'views',
|
||||
'plugin_id' => 'text_custom',
|
||||
'content' => 'test header',
|
||||
'empty' => FALSE,
|
||||
];
|
||||
$view->save();
|
||||
|
||||
$this->drupalGet($url);
|
||||
$this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
|
||||
$this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' , 'rendered']));
|
||||
$this->assertCacheContexts(['url.query_args:_wrapper_format']);
|
||||
|
||||
// Add an empty text.
|
||||
$display = &$view->getDisplay('block_1');
|
||||
$display['display_options']['defaults']['empty'] = FALSE;
|
||||
$display['display_options']['empty']['example'] = [
|
||||
'field' => 'area_text_custom',
|
||||
'id' => 'area_text_custom',
|
||||
'table' => 'views',
|
||||
'plugin_id' => 'text_custom',
|
||||
'content' => 'test empty',
|
||||
];
|
||||
$view->save();
|
||||
|
||||
$this->drupalGet($url);
|
||||
$this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
|
||||
$this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' , 'rendered']));
|
||||
$this->assertCacheContexts(['url.query_args:_wrapper_format']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the contextual links on a Views block.
|
||||
*/
|
||||
public function testBlockContextualLinks() {
|
||||
$this->drupalLogin($this->drupalCreateUser(array('administer views', 'access contextual links', 'administer blocks')));
|
||||
$block = $this->drupalPlaceBlock('views_block:test_view_block-block_1');
|
||||
$cached_block = $this->drupalPlaceBlock('views_block:test_view_block-block_1');
|
||||
$this->drupalGet('test-page');
|
||||
|
||||
$id = 'block:block=' . $block->id() . ':langcode=en|entity.view.edit_form:view=test_view_block:location=block&name=test_view_block&display_id=block_1&langcode=en';
|
||||
$cached_id = 'block:block=' . $cached_block->id() . ':langcode=en|entity.view.edit_form:view=test_view_block:location=block&name=test_view_block&display_id=block_1&langcode=en';
|
||||
// @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
|
||||
$this->assertRaw('<div' . new Attribute(array('data-contextual-id' => $id)) . '></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));
|
||||
$this->assertRaw('<div' . new Attribute(array('data-contextual-id' => $cached_id)) . '></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $cached_id)));
|
||||
|
||||
// Get server-rendered contextual links.
|
||||
// @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
|
||||
$post = array('ids[0]' => $id, 'ids[1]' => $cached_id);
|
||||
$response = $this->drupalPostWithFormat('contextual/render', 'json', $post, array('query' => array('destination' => 'test-page')));
|
||||
$this->assertResponse(200);
|
||||
$json = Json::decode($response);
|
||||
$this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li class="entityviewedit-form"><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>');
|
||||
$this->assertIdentical($json[$cached_id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $cached_block->id() . '">Configure block</a></li><li class="entityviewedit-form"><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>');
|
||||
}
|
||||
|
||||
}
|
29
web/core/modules/block/src/Theme/AdminDemoNegotiator.php
Normal file
29
web/core/modules/block/src/Theme/AdminDemoNegotiator.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block\Theme;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Theme\ThemeNegotiatorInterface;
|
||||
|
||||
/**
|
||||
* Negotiates the theme for the block admin demo page via the URL.
|
||||
*/
|
||||
class AdminDemoNegotiator implements ThemeNegotiatorInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function applies(RouteMatchInterface $route_match) {
|
||||
return $route_match->getRouteName() == 'block.admin_demo';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function determineActiveTheme(RouteMatchInterface $route_match) {
|
||||
// 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 $route_match->getParameter('theme');
|
||||
}
|
||||
|
||||
}
|
39
web/core/modules/block/templates/block.html.twig
Normal file
39
web/core/modules/block/templates/block.html.twig
Normal file
|
@ -0,0 +1,39 @@
|
|||
{#
|
||||
/**
|
||||
* @file
|
||||
* Default theme implementation to display a block.
|
||||
*
|
||||
* Available variables:
|
||||
* - plugin_id: The ID of the block implementation.
|
||||
* - label: The configured label of the block if visible.
|
||||
* - configuration: A list of the block's configuration values.
|
||||
* - label: The configured label for the block.
|
||||
* - label_display: The display settings for the label.
|
||||
* - provider: The module or other provider that provided this block plugin.
|
||||
* - Block plugin specific settings will also be stored here.
|
||||
* - content: The content of this block.
|
||||
* - attributes: array of HTML attributes populated by modules, intended to
|
||||
* be added to the main container tag of this template.
|
||||
* - id: A valid HTML ID and guaranteed unique.
|
||||
* - title_attributes: Same as attributes, except applied to the main title
|
||||
* tag that appears in the template.
|
||||
* - title_prefix: Additional output populated by modules, intended to be
|
||||
* displayed in front of the main title tag that appears in the template.
|
||||
* - title_suffix: Additional output populated by modules, intended to be
|
||||
* displayed after the main title tag that appears in the template.
|
||||
*
|
||||
* @see template_preprocess_block()
|
||||
*
|
||||
* @ingroup themeable
|
||||
*/
|
||||
#}
|
||||
<div{{ attributes }}>
|
||||
{{ title_prefix }}
|
||||
{% if label %}
|
||||
<h2{{ title_attributes }}>{{ label }}</h2>
|
||||
{% endif %}
|
||||
{{ title_suffix }}
|
||||
{% block content %}
|
||||
{{ content }}
|
||||
{% endblock %}
|
||||
</div>
|
28
web/core/modules/block/tests/fixtures/update/block.block.missing_schema.yml
vendored
Normal file
28
web/core/modules/block/tests/fixtures/update/block.block.missing_schema.yml
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
uuid: 1a6c0f14-78dc-4ede-bade-b8ce83881453
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
module:
|
||||
- block_test
|
||||
- system
|
||||
theme:
|
||||
- bartik
|
||||
id: missing_schema
|
||||
theme: bartik
|
||||
region: sidebar_first
|
||||
weight: 0
|
||||
provider: null
|
||||
plugin: system_branding_block
|
||||
settings:
|
||||
id: system_branding_block
|
||||
label: 'Test missing schema on conditions'
|
||||
provider: system
|
||||
label_display: visible
|
||||
use_site_logo: true
|
||||
use_site_name: true
|
||||
use_site_slogan: true
|
||||
visibility:
|
||||
missing_schema:
|
||||
id: missing_schema
|
||||
negate: 0
|
||||
context_mapping: { }
|
49
web/core/modules/block/tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php
vendored
Normal file
49
web/core/modules/block/tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Partial database to mimic the installation of the block_test module.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
$connection = Database::getConnection();
|
||||
|
||||
// Set the schema version.
|
||||
$connection->insert('key_value')
|
||||
->fields([
|
||||
'collection' => 'system.schema',
|
||||
'name' => 'block_test',
|
||||
'value' => 'i:8000;',
|
||||
])
|
||||
->execute();
|
||||
|
||||
// Update core.extension.
|
||||
$extensions = $connection->select('config')
|
||||
->fields('config', ['data'])
|
||||
->condition('collection', '')
|
||||
->condition('name', 'core.extension')
|
||||
->execute()
|
||||
->fetchField();
|
||||
$extensions = unserialize($extensions);
|
||||
$extensions['module']['block_test'] = 8000;
|
||||
$connection->update('config')
|
||||
->fields([
|
||||
'data' => serialize($extensions),
|
||||
])
|
||||
->condition('collection', '')
|
||||
->condition('name', 'core.extension')
|
||||
->execute();
|
||||
|
||||
// Install the block configuration.
|
||||
$config = file_get_contents(__DIR__ . '/block.block.missing_schema.yml');
|
||||
$config = Yaml::parse($config);
|
||||
$connection->insert('config')
|
||||
->fields(['data', 'name', 'collection'])
|
||||
->values([
|
||||
'name' => 'block.block.missing_schema',
|
||||
'data' => serialize($config),
|
||||
'collection' => '',
|
||||
])
|
||||
->execute();
|
|
@ -0,0 +1,8 @@
|
|||
name: 'Block test'
|
||||
type: module
|
||||
description: 'Provides test blocks.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- block
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Provide test blocks.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Block\BlockPluginInterface;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Implements hook_block_alter().
|
||||
*/
|
||||
function block_test_block_alter(&$block_info) {
|
||||
if (\Drupal::state()->get('block_test_info_alter') && isset($block_info['test_block_instantiation'])) {
|
||||
$block_info['test_block_instantiation']['category'] = t('Custom category');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_block_view_BASE_BLOCK_ID_alter().
|
||||
*/
|
||||
function block_test_block_view_test_cache_alter(array &$build, BlockPluginInterface $block) {
|
||||
if (\Drupal::state()->get('block_test_view_alter_suffix') !== NULL) {
|
||||
$build['#attributes']['foo'] = 'bar';
|
||||
}
|
||||
if (\Drupal::state()->get('block_test_view_alter_append_pre_render_prefix') !== NULL) {
|
||||
$build['#pre_render'][] = 'block_test_pre_render_alter_content';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_block_build_BASE_BLOCK_ID_alter().
|
||||
*/
|
||||
function block_test_block_build_test_cache_alter(array &$build, BlockPluginInterface $block) {
|
||||
// Test altering cache keys, contexts, tags and max-age.
|
||||
if (\Drupal::state()->get('block_test_block_alter_cache_key') !== NULL) {
|
||||
$build['#cache']['keys'][] = \Drupal::state()->get('block_test_block_alter_cache_key');
|
||||
}
|
||||
if (\Drupal::state()->get('block_test_block_alter_cache_context') !== NULL) {
|
||||
$build['#cache']['contexts'][] = \Drupal::state()->get('block_test_block_alter_cache_context');
|
||||
}
|
||||
if (\Drupal::state()->get('block_test_block_alter_cache_tag') !== NULL) {
|
||||
$build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], [\Drupal::state()->get('block_test_block_alter_cache_tag')]);
|
||||
}
|
||||
if (\Drupal::state()->get('block_test_block_alter_cache_max_age') !== NULL) {
|
||||
$build['#cache']['max-age'] = \Drupal::state()->get('block_test_block_alter_cache_max_age');
|
||||
}
|
||||
|
||||
// Test setting #create_placeholder.
|
||||
if (\Drupal::state()->get('block_test_block_alter_create_placeholder') !== NULL) {
|
||||
$build['#create_placeholder'] = \Drupal::state()->get('block_test_block_alter_create_placeholder');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #pre_render callback for a block to alter its content.
|
||||
*/
|
||||
function block_test_pre_render_alter_content($build) {
|
||||
$build['#prefix'] = 'Hiya!<br>';
|
||||
return $build;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
block_test.test_multipleforms:
|
||||
path: '/test-multiple-forms'
|
||||
defaults:
|
||||
_controller: '\Drupal\block_test\Controller\TestMultipleFormController::testMultipleForms'
|
||||
_title: 'Multiple forms'
|
||||
requirements:
|
||||
_access: 'TRUE'
|
|
@ -0,0 +1,6 @@
|
|||
services:
|
||||
block_test.multiple_static_context:
|
||||
class: Drupal\block_test\ContextProvider\MultipleStaticContext
|
||||
arguments: ['@current_user', '@entity.manager']
|
||||
tags:
|
||||
- { name: 'context_provider' }
|
|
@ -0,0 +1,17 @@
|
|||
id: test_block
|
||||
theme: stark
|
||||
weight: 0
|
||||
status: true
|
||||
langcode: en
|
||||
region: '-1'
|
||||
plugin: test_html
|
||||
settings:
|
||||
label: 'Test HTML block'
|
||||
provider: block_test
|
||||
label_display: 'hidden'
|
||||
dependencies:
|
||||
module:
|
||||
- block_test
|
||||
theme:
|
||||
- classy
|
||||
visibility: { }
|
|
@ -0,0 +1,10 @@
|
|||
block.settings.test_block_instantiation:
|
||||
type: block_settings
|
||||
label: 'Test block instantiation settings'
|
||||
mapping:
|
||||
display_message:
|
||||
type: string
|
||||
label: 'Message text'
|
||||
|
||||
condition.plugin.baloney_spam:
|
||||
type: condition.plugin
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\ContextProvider;
|
||||
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Plugin\Context\Context;
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\ContextProviderInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
|
||||
/**
|
||||
* Sets multiple contexts for a static value.
|
||||
*/
|
||||
class MultipleStaticContext implements ContextProviderInterface {
|
||||
|
||||
/**
|
||||
* The current user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
/**
|
||||
* The user storage.
|
||||
*
|
||||
* @var \Drupal\user\UserStorageInterface
|
||||
*/
|
||||
protected $userStorage;
|
||||
|
||||
/**
|
||||
* Constructs a new MultipleStaticContext.
|
||||
*
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The current user.
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
*/
|
||||
public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
|
||||
$this->account = $account;
|
||||
$this->userStorage = $entity_manager->getStorage('user');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRuntimeContexts(array $unqualified_context_ids) {
|
||||
$current_user = $this->userStorage->load($this->account->id());
|
||||
|
||||
$context1 = new Context(new ContextDefinition('entity:user', 'User 1'), $current_user);
|
||||
|
||||
$context2 = new Context(new ContextDefinition('entity:user', 'User 2'), $current_user);
|
||||
|
||||
$cacheability = new CacheableMetadata();
|
||||
$cacheability->setCacheContexts(['user']);
|
||||
|
||||
$context1->addCacheableDependency($cacheability);
|
||||
$context2->addCacheableDependency($cacheability);
|
||||
|
||||
return [
|
||||
'user1' => $context1,
|
||||
'user2' => $context2,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAvailableContexts() {
|
||||
return $this->getRuntimeContexts([]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\Form\FormState;
|
||||
|
||||
/**
|
||||
* Controller for block_test module
|
||||
*/
|
||||
class TestMultipleFormController extends ControllerBase {
|
||||
|
||||
public function testMultipleForms() {
|
||||
$form_state = new FormState();
|
||||
$build = [
|
||||
'form1' => $this->formBuilder()->buildForm('\Drupal\block_test\Form\TestForm', $form_state),
|
||||
'form2' => $this->formBuilder()->buildForm('\Drupal\block_test\Form\FavoriteAnimalTestForm', $form_state),
|
||||
];
|
||||
|
||||
// Output all attached placeholders trough drupal_set_message(), so we can
|
||||
// see if there's only one in the tests.
|
||||
$post_render_callable = function ($elements) {
|
||||
$matches = [];
|
||||
preg_match_all('<form\s(.*?)action="(.*?)"(.*)>', $elements, $matches);
|
||||
|
||||
$action_values = $matches[2];
|
||||
|
||||
foreach ($action_values as $action_value) {
|
||||
drupal_set_message('Form action: ' . $action_value);
|
||||
}
|
||||
return $elements;
|
||||
};
|
||||
|
||||
$build['#post_render'] = [$post_render_callable];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Form;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
class FavoriteAnimalTestForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'block_test_form_favorite_animal_test';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['favorite_animal'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Your favorite animal.')
|
||||
];
|
||||
|
||||
$form['submit_animal'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Submit your chosen animal'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
drupal_set_message($this->t('Your favorite animal is: @favorite_animal', ['@favorite_animal' => $form['favorite_animal']['#value']]));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Form;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
class TestForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'block_test_form_test';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['email'] = [
|
||||
'#type' => 'email',
|
||||
'#title' => $this->t('Your .com email address.')
|
||||
];
|
||||
|
||||
$form['show'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Submit'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
if (strpos($form_state->getValue('email'), '.com') === FALSE) {
|
||||
$form_state->setErrorByName('email', $this->t('This is not a .com email address.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
drupal_set_message($this->t('Your email address is @email', ['@email' => $form['email']['#value']]));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\State\StateInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a block to test access.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_access",
|
||||
* admin_label = @Translation("Test block access")
|
||||
* )
|
||||
*/
|
||||
class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* Tests the test access block.
|
||||
*
|
||||
*
|
||||
* @param array $configuration
|
||||
* The plugin configuration, i.e. an array with configuration values keyed
|
||||
* by configuration option name. The special key 'context' may be used to
|
||||
* initialize the defined contexts by setting it to an array of context
|
||||
* values keyed by context names.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\State\StateInterface $state
|
||||
* The state.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('state')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function blockAccess(AccountInterface $account) {
|
||||
return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return ['#markup' => 'Hello test world'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheMaxAge() {
|
||||
return Cache::PERMANENT;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
|
||||
/**
|
||||
* Provides a basic block for testing block instantiation and configuration.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_block_instantiation",
|
||||
* admin_label = @Translation("Display message")
|
||||
* )
|
||||
*/
|
||||
class TestBlockInstantiation extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return array(
|
||||
'display_message' => 'no message set',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function blockAccess(AccountInterface $account) {
|
||||
return AccessResult::allowedIfHasPermission($account, 'access content');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockForm($form, FormStateInterface $form_state) {
|
||||
$form['display_message'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Display message'),
|
||||
'#default_value' => $this->configuration['display_message'],
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockSubmit($form, FormStateInterface $form_state) {
|
||||
$this->configuration['display_message'] = $form_state->getValue('display_message');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return array(
|
||||
'#children' => $this->configuration['display_message'],
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
|
||||
/**
|
||||
* Provides a block to test caching.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_cache",
|
||||
* admin_label = @Translation("Test block caching")
|
||||
* )
|
||||
*/
|
||||
class TestCacheBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$content = \Drupal::state()->get('block_test.content');
|
||||
|
||||
$build = array();
|
||||
if (!empty($content)) {
|
||||
$build['#markup'] = $content;
|
||||
}
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheContexts() {
|
||||
return \Drupal::state()->get('block_test.cache_contexts', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheMaxAge() {
|
||||
return \Drupal::state()->get('block_test.cache_max_age', parent::getCacheMaxAge());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\user\UserInterface;
|
||||
|
||||
/**
|
||||
* Provides a context-aware block.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_context_aware",
|
||||
* admin_label = @Translation("Test context-aware block"),
|
||||
* context = {
|
||||
* "user" = @ContextDefinition("entity:user", required = FALSE)
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TestContextAwareBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
/** @var $user \Drupal\user\UserInterface */
|
||||
$user = $this->getContextValue('user');
|
||||
return array(
|
||||
'#prefix' => '<div id="' . $this->getPluginId() . '--username">',
|
||||
'#suffix' => '</div>',
|
||||
'#markup' => $user ? $user->getUsername() : 'No context mapping selected.' ,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function blockAccess(AccountInterface $account) {
|
||||
if ($this->getContextValue('user') instanceof UserInterface) {
|
||||
drupal_set_message('User context found.');
|
||||
}
|
||||
|
||||
return parent::blockAccess($account);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
|
||||
/**
|
||||
* Provides a context-aware block.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_context_aware_unsatisfied",
|
||||
* admin_label = @Translation("Test context-aware unsatisfied block"),
|
||||
* context = {
|
||||
* "user" = @ContextDefinition("entity:foobar")
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TestContextAwareUnsatisfiedBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return [
|
||||
'#markup' => 'test',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
|
||||
/**
|
||||
* Provides a block to test caching.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_form_in_block",
|
||||
* admin_label = @Translation("Test form block caching")
|
||||
* )
|
||||
*/
|
||||
class TestFormBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return \Drupal::formBuilder()->getForm('Drupal\block_test\Form\TestForm');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
|
||||
/**
|
||||
* Provides a block to test HTML.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_html",
|
||||
* admin_label = @Translation("Test HTML block")
|
||||
* )
|
||||
*/
|
||||
class TestHtmlBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return array(
|
||||
'#attributes' => \Drupal::state()->get('block_test.attributes'),
|
||||
'#children' => \Drupal::state()->get('block_test.content'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
|
||||
/**
|
||||
* Provides a block with multiple forms.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_multiple_forms_block",
|
||||
* forms = {
|
||||
* "secondary" = "\Drupal\block_test\PluginForm\EmptyBlockForm"
|
||||
* },
|
||||
* admin_label = @Translation("Multiple forms test block")
|
||||
* )
|
||||
*/
|
||||
class TestMultipleFormsBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Provides a test settings validation block.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_settings_validation",
|
||||
* admin_label = @Translation("Test settings validation block"),
|
||||
* )
|
||||
*/
|
||||
class TestSettingsValidationBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockForm($form, FormStateInterface $form_state) {
|
||||
return ['digits' => ['#type' => 'textfield']] + $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockValidate($form, FormStateInterface $form_state) {
|
||||
if (!ctype_digit($form_state->getValue('digits'))) {
|
||||
$form_state->setErrorByName('digits', $this->t('Only digits are allowed'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return ['#markup' => 'foo'];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Block;
|
||||
|
||||
/**
|
||||
* Provides a block to test XSS in title.
|
||||
*
|
||||
* @Block(
|
||||
* id = "test_xss_title",
|
||||
* admin_label = "<script>alert('XSS subject');</script>"
|
||||
* )
|
||||
*/
|
||||
class TestXSSTitleBlock extends TestCacheBlock {
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Condition\ConditionPluginBase;
|
||||
|
||||
/**
|
||||
* Provides a 'baloney_spam' condition.
|
||||
*
|
||||
* @Condition(
|
||||
* id = "baloney_spam",
|
||||
* label = @Translation("Baloney spam"),
|
||||
* )
|
||||
*/
|
||||
class BaloneySpam extends ConditionPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function evaluate() {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function summary() {
|
||||
return 'Summary';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Condition\ConditionPluginBase;
|
||||
|
||||
/**
|
||||
* Provides a 'missing_schema' condition.
|
||||
*
|
||||
* @Condition(
|
||||
* id = "missing_schema",
|
||||
* label = @Translation("Missing schema"),
|
||||
* )
|
||||
*/
|
||||
class MissingSchema extends ConditionPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function evaluate() {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function summary() {
|
||||
return 'Summary';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\block_test\PluginForm;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\PluginFormBase;
|
||||
|
||||
/**
|
||||
* Provides a form for a block that is empty.
|
||||
*/
|
||||
class EmptyBlockForm extends PluginFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
// Intentionally empty.
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
name: '<"Cat" & ''Mouse''>'
|
||||
type: theme
|
||||
description: 'Theme for testing special characters in block admin.'
|
||||
core: 8.x
|
||||
regions:
|
||||
content: Content
|
||||
help: Help
|
|
@ -0,0 +1,16 @@
|
|||
name: 'Block test theme'
|
||||
type: theme
|
||||
description: 'Theme for testing the block system'
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
regions:
|
||||
sidebar_first: 'Left sidebar'
|
||||
sidebar_second: 'Right sidebar'
|
||||
content: Content
|
||||
header: Header
|
||||
footer: Footer
|
||||
highlighted: Highlighted
|
||||
help: Help
|
||||
regions_hidden:
|
||||
- sidebar_first
|
||||
- sidebar_second
|
|
@ -0,0 +1,9 @@
|
|||
name: 'Block test views'
|
||||
type: module
|
||||
description: 'Provides a view and block to test block displays in views.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- block
|
||||
- views
|
|
@ -0,0 +1,45 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies: { }
|
||||
id: test_view_block
|
||||
label: test_view_block
|
||||
module: views
|
||||
description: ''
|
||||
tag: ''
|
||||
base_table: views_test_data
|
||||
base_field: id
|
||||
core: 8.x
|
||||
display:
|
||||
default:
|
||||
display_plugin: default
|
||||
id: default
|
||||
display_title: Master
|
||||
position: null
|
||||
display_options:
|
||||
access:
|
||||
type: none
|
||||
cache:
|
||||
type: tag
|
||||
query:
|
||||
type: views_query
|
||||
exposed_form:
|
||||
type: basic
|
||||
pager:
|
||||
type: some
|
||||
options:
|
||||
items_per_page: 5
|
||||
style:
|
||||
type: default
|
||||
row:
|
||||
type: fields
|
||||
fields:
|
||||
name:
|
||||
id: name
|
||||
table: views_test_data
|
||||
field: name
|
||||
title: test_view_block
|
||||
block_1:
|
||||
display_plugin: block
|
||||
id: block_1
|
||||
display_title: Block
|
||||
position: null
|
|
@ -0,0 +1,57 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
module:
|
||||
- user
|
||||
id: test_view_block2
|
||||
label: test_view_block2
|
||||
module: views
|
||||
description: ''
|
||||
tag: ''
|
||||
base_table: views_test_data
|
||||
base_field: id
|
||||
core: 8.x
|
||||
display:
|
||||
default:
|
||||
display_plugin: default
|
||||
id: default
|
||||
display_title: Master
|
||||
position: null
|
||||
display_options:
|
||||
access:
|
||||
type: perm
|
||||
cache:
|
||||
type: tag
|
||||
query:
|
||||
type: views_query
|
||||
exposed_form:
|
||||
type: basic
|
||||
pager:
|
||||
type: some
|
||||
options:
|
||||
items_per_page: 5
|
||||
style:
|
||||
type: default
|
||||
row:
|
||||
type: fields
|
||||
fields:
|
||||
name:
|
||||
id: name
|
||||
table: views_test_data
|
||||
field: name
|
||||
title: test_view_block2
|
||||
block_1:
|
||||
display_plugin: block
|
||||
id: block_1
|
||||
display_title: Block
|
||||
position: null
|
||||
block_2:
|
||||
display_plugin: block
|
||||
id: block_2
|
||||
display_title: Block
|
||||
position: null
|
||||
block_3:
|
||||
display_plugin: block
|
||||
id: block_3
|
||||
display_title: Block
|
||||
position: null
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\block\Kernel;
|
||||
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\config\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the block config schema.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockConfigSchemaTest extends KernelTestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array(
|
||||
'block',
|
||||
'aggregator',
|
||||
'book',
|
||||
'block_content',
|
||||
'comment',
|
||||
'forum',
|
||||
'node',
|
||||
'statistics',
|
||||
// BlockManager->getModuleName() calls system_get_info().
|
||||
'system',
|
||||
'taxonomy',
|
||||
'user',
|
||||
'text',
|
||||
);
|
||||
|
||||
/**
|
||||
* The typed config manager.
|
||||
*
|
||||
* @var \Drupal\Core\Config\TypedConfigManagerInterface
|
||||
*/
|
||||
protected $typedConfig;
|
||||
|
||||
/**
|
||||
* The block manager.
|
||||
*
|
||||
* @var \Drupal\Core\Block\BlockManagerInterface
|
||||
*/
|
||||
protected $blockManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->typedConfig = \Drupal::service('config.typed');
|
||||
$this->blockManager = \Drupal::service('plugin.manager.block');
|
||||
$this->installEntitySchema('block_content');
|
||||
$this->installEntitySchema('taxonomy_term');
|
||||
$this->installEntitySchema('node');
|
||||
$this->installSchema('book', array('book'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block config schema for block plugins.
|
||||
*/
|
||||
public function testBlockConfigSchema() {
|
||||
foreach ($this->blockManager->getDefinitions() as $block_id => $definition) {
|
||||
$id = strtolower($this->randomMachineName());
|
||||
$block = Block::create(array(
|
||||
'id' => $id,
|
||||
'theme' => 'classy',
|
||||
'weight' => 00,
|
||||
'status' => TRUE,
|
||||
'region' => 'content',
|
||||
'plugin' => $block_id,
|
||||
'settings' => array(
|
||||
'label' => $this->randomMachineName(),
|
||||
'provider' => 'system',
|
||||
'label_display' => FALSE,
|
||||
),
|
||||
'visibility' => array(),
|
||||
));
|
||||
$block->save();
|
||||
|
||||
$config = $this->config("block.block.$id");
|
||||
$this->assertEqual($config->get('id'), $id);
|
||||
$this->assertConfigSchema($this->typedConfig, $config->getName(), $config->get());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
103
web/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php
Normal file
103
web/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\block\Kernel;
|
||||
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\block\BlockInterface;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the block plugin can work properly without a supporting entity.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockInterfaceTest extends KernelTestBase {
|
||||
|
||||
public static $modules = array('system', 'block', 'block_test', 'user');
|
||||
|
||||
/**
|
||||
* Test configuration and subsequent form() and build() method calls.
|
||||
*
|
||||
* This test is attempting to test the existing block plugin api and all
|
||||
* functionality that is expected to remain consistent. The arrays that are
|
||||
* used for comparison can change, but only to include elements that are
|
||||
* contained within BlockBase or the plugin being tested. Likely these
|
||||
* comparison arrays should get smaller, not larger, as more form/build
|
||||
* elements are moved into a more suitably responsible class.
|
||||
*
|
||||
* Instantiation of the plugin is the primary element being tested here. The
|
||||
* subsequent method calls are just attempting to cause a failure if a
|
||||
* dependency outside of the plugin configuration is required.
|
||||
*/
|
||||
public function testBlockInterface() {
|
||||
$manager = $this->container->get('plugin.manager.block');
|
||||
$configuration = array(
|
||||
'label' => 'Custom Display Message',
|
||||
);
|
||||
$expected_configuration = array(
|
||||
'id' => 'test_block_instantiation',
|
||||
'label' => 'Custom Display Message',
|
||||
'provider' => 'block_test',
|
||||
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
|
||||
'display_message' => 'no message set',
|
||||
);
|
||||
// Initial configuration of the block at construction time.
|
||||
/** @var $display_block \Drupal\Core\Block\BlockPluginInterface */
|
||||
$display_block = $manager->createInstance('test_block_instantiation', $configuration);
|
||||
$this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block was configured correctly.');
|
||||
|
||||
// Updating an element of the configuration.
|
||||
$display_block->setConfigurationValue('display_message', 'My custom display message.');
|
||||
$expected_configuration['display_message'] = 'My custom display message.';
|
||||
$this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.');
|
||||
$definition = $display_block->getPluginDefinition();
|
||||
|
||||
$expected_form = array(
|
||||
'provider' => array(
|
||||
'#type' => 'value',
|
||||
'#value' => 'block_test',
|
||||
),
|
||||
'admin_label' => array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Block description'),
|
||||
'#plain_text' => $definition['admin_label'],
|
||||
),
|
||||
'label' => array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => 'Title',
|
||||
'#maxlength' => 255,
|
||||
'#default_value' => 'Custom Display Message',
|
||||
'#required' => TRUE,
|
||||
),
|
||||
'label_display' => array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => 'Display title',
|
||||
'#default_value' => TRUE,
|
||||
'#return_value' => 'visible',
|
||||
),
|
||||
'context_mapping' => array(),
|
||||
'display_message' => array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Display message'),
|
||||
'#default_value' => 'My custom display message.',
|
||||
),
|
||||
);
|
||||
$form_state = new FormState();
|
||||
// Ensure there are no form elements that do not belong to the plugin.
|
||||
$actual_form = $display_block->buildConfigurationForm(array(), $form_state);
|
||||
// Remove the visibility sections, as that just tests condition plugins.
|
||||
unset($actual_form['visibility'], $actual_form['visibility_tabs']);
|
||||
$this->assertIdentical($this->castSafeStrings($actual_form), $this->castSafeStrings($expected_form), 'Only the expected form elements were present.');
|
||||
|
||||
$expected_build = array(
|
||||
'#children' => 'My custom display message.',
|
||||
);
|
||||
// Ensure the build array is proper.
|
||||
$this->assertIdentical($display_block->build(), $expected_build, 'The plugin returned the appropriate build array.');
|
||||
|
||||
// Ensure the machine name suggestion is correct. In truth, this is actually
|
||||
// testing BlockBase's implementation, not the interface itself.
|
||||
$this->assertIdentical($display_block->getMachineNameSuggestion(), 'displaymessage', 'The plugin returned the expected machine name suggestion.');
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Reference in a new issue