Skip to content

Instantly share code, notes, and snippets.

@LionsAd
Created May 26, 2015 15:18
Show Gist options
  • Select an option

  • Save LionsAd/c3096a5563152d7b37a4 to your computer and use it in GitHub Desktop.

Select an option

Save LionsAd/c3096a5563152d7b37a4 to your computer and use it in GitHub Desktop.
Cache contexts for $access
diff --git a/core/modules/block/src/BlockRepository.php b/core/modules/block/src/BlockRepository.php
index 485a66f..e2aa34e 100644
--- a/core/modules/block/src/BlockRepository.php
+++ b/core/modules/block/src/BlockRepository.php
@@ -77,9 +77,8 @@ public function getVisibleBlocksPerRegion(array $contexts) {
foreach ($this->blockStorage->loadByProperties(array('theme' => $this->getTheme())) as $block_id => $block) {
/** @var \Drupal\block\BlockInterface $block */
// Set the contexts on the block before checking access.
- if ($block->setContexts($contexts)->access('view')) {
- $full[$block->getRegion()][$block_id] = $block;
- }
+ $block->setContexts($contexts);
+ $full[$block->getRegion()][$block_id] = $block;
}
// Merge it with the actual values to maintain the region ordering.
diff --git a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
index 7b67093..d8b7635 100644
--- a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
+++ b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
@@ -12,6 +12,7 @@
use Drupal\block\Event\BlockEvents;
use Drupal\Core\Block\MainContentBlockPluginInterface;
use Drupal\Core\Block\MessagesBlockPluginInterface;
+use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Display\PageVariantInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
@@ -133,6 +134,14 @@ public function build() {
foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts) as $region => $blocks) {
/** @var $blocks \Drupal\block\BlockInterface[] */
foreach ($blocks as $key => $block) {
+ $access = $block->access('view', NULL, TRUE);
+ if ($access->isForbidden()) {
+ $build[$region][$key] = [];
+ CacheableMetadata::createFromObject($access)
+ ->applyTo($build[$region][$key]);
+
+ continue;
+ }
$block_plugin = $block->getPlugin();
if ($block_plugin instanceof MainContentBlockPluginInterface) {
$block_plugin->setMainContent($this->mainContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment