Browse Source

Issue #2906140 Code cleanup.

merge-requests/4/head
Chi 7 years ago
parent
commit
8118336510
  1. 45
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 2
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

45
src/TwigExtension.php

@ -24,7 +24,6 @@ class TwigExtension extends \Twig_Extension {
return [ return [
new \Twig_SimpleFunction('drupal_view', 'views_embed_view'), new \Twig_SimpleFunction('drupal_view', 'views_embed_view'),
new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), 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_region', [$this, 'drupalRegion']),
new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']),
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), 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 * @param mixed $id
* The ID of the block to render. * The ID of block plugin to render.
* @param bool $check_access * @param array $configuration
* (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
* (Optional) Pass on any configuration to the plugin block. * (Optional) Pass on any configuration to the plugin block.
* *
* @return null|array * @return null|array
* A render array for the block or NULL if the block does not exist. * A render array for the block or NULL if the block does not exist.
*/ */
public function drupalPluginBlock($id, $check_access = TRUE, $config = []) { public function drupalBlock($id, array $configuration = []) {
$block_manager = \Drupal::service('plugin.manager.block'); /** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */
$plugin_block = $block_manager->createInstance($id, $config); $block_plugin = \Drupal::service('plugin.manager.block')
$access_result = $plugin_block->access(\Drupal::currentUser()); ->createInstance($id, $configuration);
if ($block_plugin->access(\Drupal::currentUser())) {
if ($plugin_block && (!$check_access || (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result))) { return $block_plugin->build();
return [];
} }
return $plugin_block->build();
} }
/** /**
@ -512,7 +487,7 @@ class TwigExtension extends \Twig_Extension {
* @return bool * @return bool
* The access check result. * 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) { protected function entityAccess(EntityInterface $entity) {
// Prior version 8.x-1.7 entity access was not checked. The "check_access" // Prior version 8.x-1.7 entity access was not checked. The "check_access"

4
tests/src/Functional/TwigTweakTest.php

@ -65,9 +65,9 @@ class TwigTweakTest extends BrowserTestBase {
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li'; $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]');
// Test block. // Test block plugin.
$xpath = '//div[@class = "tt-block"]'; $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); $this->assertByXpath($xpath);
// Test region. // Test region.

2
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -19,7 +19,7 @@
<div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div> <div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div>
<div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div> <div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div>
<div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div> <div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div>
<div class="tt-block">{{ drupal_block('classy_powered_by_drupal') }}</div> <div class="tt-block">{{ drupal_block('system_branding_block', {use_site_name: false}) }}</div>
<div class="tt-region">{{ drupal_region('sidebar_first') }}</div> <div class="tt-region">{{ drupal_region('sidebar_first') }}</div>
<div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div> <div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div>
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div> <div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>

Loading…
Cancel
Save