Update to Drupal 8.0.0-beta15. For more information, see: https://www.drupal.org/node/2563023
This commit is contained in:
parent
2720a9ec4b
commit
f3791f1da3
1898 changed files with 54300 additions and 11481 deletions
|
@ -7,19 +7,59 @@
|
|||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Block\MainContentBlockPluginInterface;
|
||||
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\EntityViewBuilderInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Language\LanguageManagerInterface;
|
||||
use Drupal\Core\Render\Element;
|
||||
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}
|
||||
*/
|
||||
|
@ -40,13 +80,9 @@ class BlockViewBuilder extends EntityViewBuilder {
|
|||
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
|
||||
/** @var \Drupal\block\BlockInterface[] $entities */
|
||||
$build = array();
|
||||
foreach ($entities as $entity) {
|
||||
foreach ($entities as $entity) {
|
||||
$entity_id = $entity->id();
|
||||
$plugin = $entity->getPlugin();
|
||||
$plugin_id = $plugin->getPluginId();
|
||||
$base_id = $plugin->getBaseId();
|
||||
$derivative_id = $plugin->getDerivativeId();
|
||||
$configuration = $plugin->getConfiguration();
|
||||
|
||||
$cache_tags = Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags());
|
||||
$cache_tags = Cache::mergeTags($cache_tags, $plugin->getCacheTags());
|
||||
|
@ -54,20 +90,6 @@ class BlockViewBuilder extends EntityViewBuilder {
|
|||
// Create the render array for the block as a whole.
|
||||
// @see template_preprocess_block().
|
||||
$build[$entity_id] = array(
|
||||
'#theme' => 'block',
|
||||
'#attributes' => array(),
|
||||
// All blocks get a "Configure block" contextual link.
|
||||
'#contextual_links' => array(
|
||||
'block' => array(
|
||||
'route_parameters' => array('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(),
|
||||
'#cache' => [
|
||||
'keys' => ['entity_view', 'block', $entity->id()],
|
||||
'contexts' => Cache::mergeContexts(
|
||||
|
@ -77,22 +99,94 @@ class BlockViewBuilder extends EntityViewBuilder {
|
|||
'tags' => $cache_tags,
|
||||
'max-age' => $plugin->getCacheMaxAge(),
|
||||
],
|
||||
'#pre_render' => [
|
||||
[$this, 'buildBlock'],
|
||||
],
|
||||
// Add the entity so that it can be used in the #pre_render method.
|
||||
'#block' => $entity,
|
||||
);
|
||||
$build[$entity_id]['#configuration']['label'] = SafeMarkup::checkPlain($configuration['label']);
|
||||
|
||||
// Don't run in ::buildBlock() to ensure cache keys can be altered. If an
|
||||
// alter hook wants to modify the block contents, it can append another
|
||||
// #pre_render hook.
|
||||
$this->moduleHandler()->alter(array('block_view', "block_view_$base_id"), $build[$entity_id], $plugin);
|
||||
// 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) {
|
||||
// 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();
|
||||
|
||||
// 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.
|
||||
* @param $langcode
|
||||
* The langcode 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, $langcode) {
|
||||
return static::buildPreRenderableBlock(entity_load('block', $entity_id), \Drupal::service('module_handler'));
|
||||
}
|
||||
|
||||
/**
|
||||
* #pre_render callback for building a block.
|
||||
*
|
||||
|
@ -102,7 +196,7 @@ class BlockViewBuilder extends EntityViewBuilder {
|
|||
* - if there is content, moves the contextual links from the block content to
|
||||
* the block itself.
|
||||
*/
|
||||
public function buildBlock($build) {
|
||||
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.
|
||||
|
|
Reference in a new issue