diff --git a/src/TwigExtension.php b/src/TwigExtension.php index e2da2b5..cd382ab 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -24,7 +24,6 @@ class TwigExtension extends \Twig_Extension { return [ new \Twig_SimpleFunction('drupal_view', 'views_embed_view'), new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), - new \Twig_SimpleFunction('drupal_plugin_block', [$this, 'drupalPluginBlock']), new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']), new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), @@ -68,47 +67,23 @@ class TwigExtension extends \Twig_Extension { } /** - * Builds the render array for the provided block. + * Builds the render array for the provided block plugin. * * @param mixed $id - * The ID of the block to render. - * @param bool $check_access - * (Optional) Indicates that access check is required. - * - * @return null|array - * A render array for the block or NULL if the block does not exist. - */ - public function drupalBlock($id, $check_access = TRUE) { - $entity_type_manager = \Drupal::entityTypeManager(); - $block = $entity_type_manager->getStorage('block')->load($id); - if ($block && (!$check_access || $this->entityAccess($block))) { - return $entity_type_manager->getViewBuilder('block')->view($block); - } - } - - /** - * Builds the render array for the provided plugin block. - * - * @param mixed $id - * The ID of the block to render. - * @param bool $check_access - * (Optional) Indicates that access check is required. - * @param array $config + * The ID of block plugin to render. + * @param array $configuration * (Optional) Pass on any configuration to the plugin block. * * @return null|array * A render array for the block or NULL if the block does not exist. */ - public function drupalPluginBlock($id, $check_access = TRUE, $config = []) { - $block_manager = \Drupal::service('plugin.manager.block'); - $plugin_block = $block_manager->createInstance($id, $config); - $access_result = $plugin_block->access(\Drupal::currentUser()); - - if ($plugin_block && (!$check_access || (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result))) { - return []; + public function drupalBlock($id, array $configuration = []) { + /** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */ + $block_plugin = \Drupal::service('plugin.manager.block') + ->createInstance($id, $configuration); + if ($block_plugin->access(\Drupal::currentUser())) { + return $block_plugin->build(); } - - return $plugin_block->build(); } /** @@ -512,7 +487,7 @@ class TwigExtension extends \Twig_Extension { * @return bool * The access check result. * - * @TODO Remove "check_access" option in 9.x. + * @todo Remove "check_access" option in 9.x. */ protected function entityAccess(EntityInterface $entity) { // Prior version 8.x-1.7 entity access was not checked. The "check_access" diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 3108f5c..affcbf9 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -65,9 +65,9 @@ class TwigTweakTest extends BrowserTestBase { $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li'; $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]'); - // Test block. + // Test block plugin. $xpath = '//div[@class = "tt-block"]'; - $xpath .= '/div[@id="block-classy-powered-by-drupal"]/span[contains(., "Powered by Drupal")]'; + $xpath .= '/img[contains(@src, "/core/themes/classy/logo.svg") and @alt="Home"]'; $this->assertByXpath($xpath); // Test region. diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index bf08269..07253b6 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -19,7 +19,7 @@
{{ drupal_view('twig_tweak_test') }}
{{ drupal_view('twig_tweak_test', 'page_1') }}
{{ drupal_view('twig_tweak_test', 'page_1', 1) }}
-
{{ drupal_block('classy_powered_by_drupal') }}
+
{{ drupal_block('system_branding_block', {use_site_name: false}) }}
{{ drupal_region('sidebar_first') }}
{{ drupal_entity('node', 1) }}
{{ drupal_entity('node', 1, 'teaser') }}