Browse Source

Revert "Remove usage of deprecated functions"

This reverts commit 0014fee04c.
merge-requests/14/head
Chi 3 years ago
parent
commit
c853e4027a
  1. 12
      src/TwigTweakExtension.php
  2. 15
      src/UrlExtractor.php
  3. 4
      tests/src/Kernel/UrlExtractorTest.php
  4. 2
      twig_tweak.services.yml

12
src/TwigTweakExtension.php

@ -318,12 +318,9 @@ class TwigTweakExtension extends AbstractExtension {
\Drupal::request(), \Drupal::request(),
\Drupal::routeMatch()->getRouteObject() \Drupal::routeMatch()->getRouteObject()
); );
return [ $build['#markup'] = render($title);
'#markup' => is_array($title) ? \Drupal::service('renderer')->render($title) : $title, $build['#cache']['contexts'] = ['url'];
'#cache' => [ return $build;
'context' => ['url'],
],
];
} }
/** /**
@ -500,8 +497,7 @@ class TwigTweakExtension extends AbstractExtension {
return NULL; return NULL;
} }
return \Drupal::service('file_url_generator') return file_url_transform_relative($image_style->buildUrl($path));
->transformRelative($image_style->buildUrl($path));
} }
/** /**

15
src/UrlExtractor.php

@ -7,7 +7,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldItemList; use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\link\LinkItemInterface; use Drupal\link\LinkItemInterface;
use Drupal\media\MediaInterface; use Drupal\media\MediaInterface;
@ -25,19 +24,11 @@ class UrlExtractor {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The fire URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrlGenerator;
/** /**
* Constructs a UrlExtractor object. * 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->entityTypeManager = $entity_type_manager;
$this->fileUrlGenerator = $file_url_generator;
} }
/** /**
@ -53,8 +44,8 @@ class UrlExtractor {
*/ */
public function extractUrl($input, bool $relative = TRUE): ?string { public function extractUrl($input, bool $relative = TRUE): ?string {
if (is_string($input)) { if (is_string($input)) {
$url = $this->fileUrlGenerator->generateAbsoluteString($input); $url = file_create_url($input);
return $relative ? $this->fileUrlGenerator->transformRelative($url) : $url; return $relative ? file_url_transform_relative($url) : $url;
} }
elseif ($input instanceof LinkItemInterface) { elseif ($input instanceof LinkItemInterface) {
return $input->getUrl()->toString(); return $input->getUrl()->toString();

4
tests/src/Kernel/UrlExtractorTest.php

@ -15,9 +15,9 @@ final class UrlExtractorTest extends AbstractExtractorTestCase {
public function testUrlExtractor(): void { public function testUrlExtractor(): void {
$extractor = $this->container->get('twig_tweak.url_extractor'); $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"; $absolute_url = "{$request->getScheme()}://{$request->getHost()}/foo/bar.txt";
$url = $extractor->extractUrl($absolute_url); $url = $extractor->extractUrl($absolute_url);
self::assertSame('/foo/bar.txt', $url); self::assertSame('/foo/bar.txt', $url);

2
twig_tweak.services.yml

@ -34,7 +34,7 @@ services:
twig_tweak.url_extractor: twig_tweak.url_extractor:
class: Drupal\twig_tweak\UrlExtractor class: Drupal\twig_tweak\UrlExtractor
arguments: ['@entity_type.manager', '@file_url_generator'] arguments: ['@entity_type.manager']
twig_tweak.uri_extractor: twig_tweak.uri_extractor:
class: Drupal\twig_tweak\UriExtractor class: Drupal\twig_tweak\UriExtractor

Loading…
Cancel
Save