Browse Source

Clean up drupal_block()

merge-requests/4/head 8.x-2.2
Chi 6 years ago
parent
commit
019921476d
  1. 39
      src/TwigExtension.php

39
src/TwigExtension.php

@ -124,46 +124,29 @@ class TwigExtension extends \Twig_Extension {
$block_plugin->setTitle($title); $block_plugin->setTitle($title);
} }
$content = $block_plugin->build();
if ($content && !Element::isEmpty($content)) {
if ($wrapper) {
$build = [ $build = [
'#theme' => 'block', 'content' => $block_plugin->build(),
'#attributes' => [],
'#contextual_links' => [],
'#configuration' => $block_plugin->getConfiguration(),
'#plugin_id' => $block_plugin->getPluginId(),
'#base_plugin_id' => $block_plugin->getBaseId(),
'#derivative_plugin_id' => $block_plugin->getDerivativeId(),
'content' => $content,
'#cache' => [ '#cache' => [
'contexts' => $block_plugin->getCacheContexts(), 'contexts' => $block_plugin->getCacheContexts(),
'tags' => $block_plugin->getCacheTags(), 'tags' => $block_plugin->getCacheTags(),
'max-age' => $block_plugin->getCacheMaxAge(), 'max-age' => $block_plugin->getCacheMaxAge(),
], ],
]; ];
}
else {
$build = $content;
}
}
else {
// Preserve cache metadata of empty blocks.
$build = [
'#markup' => '',
'#cache' => isset($content['#cache']) ? $content['#cache'] : [],
];
}
if ($block_plugin instanceof TitleBlockPluginInterface) { if ($block_plugin instanceof TitleBlockPluginInterface) {
$build['#cache']['contexts'][] = 'url'; $build['#cache']['contexts'][] = 'url';
} }
if (!empty($content)) { if ($wrapper) {
CacheableMetadata::createFromRenderArray($build) $build += [
->merge(CacheableMetadata::createFromRenderArray($content)) '#theme' => 'block',
->applyTo($build); '#attributes' => [],
'#contextual_links' => [],
'#configuration' => $block_plugin->getConfiguration(),
'#plugin_id' => $block_plugin->getPluginId(),
'#base_plugin_id' => $block_plugin->getBaseId(),
'#derivative_plugin_id' => $block_plugin->getDerivativeId(),
];
} }
return $build; return $build;

Loading…
Cancel
Save