From 0f49ad32bd9ea59afd273e46d522cc5e537f443a Mon Sep 17 00:00:00 2001 From: Chi Date: Fri, 15 Sep 2017 13:29:59 +0500 Subject: [PATCH] Removed legacy code. --- src/TwigExtension.php | 53 +++++++------------ tests/src/Functional/TwigTweakTest.php | 4 -- .../templates/twig-tweak-test.html.twig | 1 - 3 files changed, 19 insertions(+), 39 deletions(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index cd382ab..a54ee10 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -3,7 +3,6 @@ namespace Drupal\twig_tweak; use Drupal\Core\Block\TitleBlockPluginInterface; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Drupal\image\Entity\ImageStyle; @@ -12,8 +11,7 @@ use Symfony\Cmf\Component\Routing\RouteObjectInterface; /** * Twig extension with some useful functions and filters. * - * As version 1.7 all dependencies are instantiated on demand for performance - * reasons. + * Dependency injection is not used for performance reason. */ class TwigExtension extends \Twig_Extension { @@ -111,7 +109,7 @@ class TwigExtension extends \Twig_Extension { /* @var $blocks \Drupal\block\BlockInterface[] */ foreach ($blocks as $id => $block) { - if ($this->entityAccess($block)) { + if ($block->access('view')) { $block_plugin = $block->getPlugin(); if ($block_plugin instanceof TitleBlockPluginInterface) { $request = \Drupal::request(); @@ -138,16 +136,18 @@ class TwigExtension extends \Twig_Extension { * @param string $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. + * @param bool $check_access + * (Optional) Indicates that access check is required. * * @return null|array * A render array for the entity or NULL if the entity does not exist. */ - public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) { + public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL, $check_access = TRUE) { $entity_type_manager = \Drupal::entityTypeManager(); $entity = $id ? $entity_type_manager->getStorage($entity_type)->load($id) : \Drupal::routeMatch()->getParameter($entity_type); - if ($entity && $this->entityAccess($entity)) { + if ($entity && (!$check_access || $entity->access('view'))) { $render_controller = $entity_type_manager->getViewBuilder($entity_type); return $render_controller->view($entity, $view_mode, $langcode); } @@ -166,16 +166,18 @@ class TwigExtension extends \Twig_Extension { * (optional) The view mode that should be used to render the field. * @param string $langcode * (optional) Language code to load translation. + * @param bool $check_access + * (Optional) Indicates that access check is required. * * @return null|array * A render array for the field or NULL if the value does not exist. */ - public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) { + public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL, $check_access = TRUE) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $id ? \Drupal::entityTypeManager()->getStorage($entity_type)->load($id) : \Drupal::routeMatch()->getParameter($entity_type); - if ($entity && $this->entityAccess($entity)) { + if ($entity && (!$check_access || $entity->access('view'))) { if ($langcode && $entity->hasTranslation($langcode)) { $entity = $entity->getTranslation($langcode); } @@ -235,8 +237,7 @@ class TwigExtension extends \Twig_Extension { */ public function drupalForm($form_id) { $form_builder = \Drupal::formBuilder(); - $args = func_get_args(); - return call_user_func_array([$form_builder, 'getForm'], $args); + return call_user_func_array([$form_builder, 'getForm'], func_get_args()); } /** @@ -280,6 +281,9 @@ class TwigExtension extends \Twig_Extension { /** * Dumps information about variables. + * + * @param mixed $var + * The variable to dump. */ public function drupalDump($var) { $var_dumper = '\Symfony\Component\VarDumper\VarDumper'; @@ -294,10 +298,13 @@ class TwigExtension extends \Twig_Extension { /** * An alias for self::drupalDump(). * + * @param mixed $var + * The variable to dump. + * * @see \Drupal\twig_tweak\TwigExtension::drupalDump(); */ - public function dd() { - $this->drupalDump(func_get_args()); + public function dd($var) { + $this->drupalDump($var); } /** @@ -385,11 +392,6 @@ class TwigExtension extends \Twig_Extension { * The new text if matches are found, otherwise unchanged text. */ public function pregReplaceFilter($text, $pattern, $replacement) { - // BC layer. Before version 8.x-1.8 the pattern was without delimiters. - // @todo Remove this in Drupal 9. - if (strpos($pattern, '/') !== 0) { - return preg_replace("/$pattern/", $replacement, $text); - } return preg_replace($pattern, $replacement, $text); } @@ -478,21 +480,4 @@ class TwigExtension extends \Twig_Extension { return $output; } - /** - * Checks view access to a given entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * Entity to check access. - * - * @return bool - * The access check result. - * - * @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" - // option provides a workaround for possible BC issues. - return !Settings::get('twig_tweak_check_access', TRUE) || $entity->access('view'); - } - } diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index affcbf9..43bdb2f 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -151,10 +151,6 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-preg-replace" and text() = "FOO-bar"]'; $this->assertByXpath($xpath); - // Test preg replacement (legacy). - $xpath = '//div[@class = "tt-preg-replace-legacy" and text() = "foo-bar"]'; - $this->assertByXpath($xpath); - // Test image style. $xpath = '//div[@class = "tt-image-style" and contains(text(), "styles/thumbnail/public/images/ocean.jpg")]'; $this->assertByXpath($xpath); 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 07253b6..a46539c 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -37,7 +37,6 @@
{{ drupal_url('node/1', {absolute: true}) }}
{{ 'Site name: [site:name]' | token_replace }}
{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}
-
{{ 'foo' | preg_replace('(foo)', '$1-bar') }}
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
{{ 'Привет!' | transliterate('ru') }}
{{ 'bold strong' | check_markup('twig_tweak_test') }}