From c853e4027a25c6226fb678805f904e4d13e702dd Mon Sep 17 00:00:00 2001 From: Chi Date: Mon, 27 Sep 2021 12:26:02 +0500 Subject: [PATCH] Revert "Remove usage of deprecated functions" This reverts commit 0014fee04cf441c0b4e67c93590883437dcd303c. --- src/TwigTweakExtension.php | 12 ++++-------- src/UrlExtractor.php | 15 +++------------ tests/src/Kernel/UrlExtractorTest.php | 4 ++-- twig_tweak.services.yml | 2 +- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 365886a..4d7ad01 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -318,12 +318,9 @@ class TwigTweakExtension extends AbstractExtension { \Drupal::request(), \Drupal::routeMatch()->getRouteObject() ); - return [ - '#markup' => is_array($title) ? \Drupal::service('renderer')->render($title) : $title, - '#cache' => [ - 'context' => ['url'], - ], - ]; + $build['#markup'] = render($title); + $build['#cache']['contexts'] = ['url']; + return $build; } /** @@ -500,8 +497,7 @@ class TwigTweakExtension extends AbstractExtension { return NULL; } - return \Drupal::service('file_url_generator') - ->transformRelative($image_style->buildUrl($path)); + return file_url_transform_relative($image_style->buildUrl($path)); } /** diff --git a/src/UrlExtractor.php b/src/UrlExtractor.php index ccb696a..fee6727 100644 --- a/src/UrlExtractor.php +++ b/src/UrlExtractor.php @@ -7,7 +7,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldItemList; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; -use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\file\FileInterface; use Drupal\link\LinkItemInterface; use Drupal\media\MediaInterface; @@ -25,19 +24,11 @@ class UrlExtractor { */ protected $entityTypeManager; - /** - * The fire URL generator. - * - * @var \Drupal\Core\File\FileUrlGeneratorInterface - */ - protected $fileUrlGenerator; - /** * Constructs a UrlExtractor object. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, FileUrlGeneratorInterface $file_url_generator) { + public function __construct(EntityTypeManagerInterface $entity_type_manager) { $this->entityTypeManager = $entity_type_manager; - $this->fileUrlGenerator = $file_url_generator; } /** @@ -53,8 +44,8 @@ class UrlExtractor { */ public function extractUrl($input, bool $relative = TRUE): ?string { if (is_string($input)) { - $url = $this->fileUrlGenerator->generateAbsoluteString($input); - return $relative ? $this->fileUrlGenerator->transformRelative($url) : $url; + $url = file_create_url($input); + return $relative ? file_url_transform_relative($url) : $url; } elseif ($input instanceof LinkItemInterface) { return $input->getUrl()->toString(); diff --git a/tests/src/Kernel/UrlExtractorTest.php b/tests/src/Kernel/UrlExtractorTest.php index 1ffbfca..1064358 100644 --- a/tests/src/Kernel/UrlExtractorTest.php +++ b/tests/src/Kernel/UrlExtractorTest.php @@ -15,9 +15,9 @@ final class UrlExtractorTest extends AbstractExtractorTestCase { public function testUrlExtractor(): void { $extractor = $this->container->get('twig_tweak.url_extractor'); - $base_url = $this->container->get('file_url_generator')->generateAbsoluteString(''); + $base_url = file_create_url(''); - $request = $this->container->get('request_stack')->getCurrentRequest(); + $request = \Drupal::request(); $absolute_url = "{$request->getScheme()}://{$request->getHost()}/foo/bar.txt"; $url = $extractor->extractUrl($absolute_url); self::assertSame('/foo/bar.txt', $url); diff --git a/twig_tweak.services.yml b/twig_tweak.services.yml index f5c6ef3..71d2365 100644 --- a/twig_tweak.services.yml +++ b/twig_tweak.services.yml @@ -34,7 +34,7 @@ services: twig_tweak.url_extractor: class: Drupal\twig_tweak\UrlExtractor - arguments: ['@entity_type.manager', '@file_url_generator'] + arguments: ['@entity_type.manager'] twig_tweak.uri_extractor: class: Drupal\twig_tweak\UriExtractor