Browse Source

Support Drupal 10

merge-requests/14/head
Chi 3 years ago
parent
commit
530adc97b8
  1. 2
      composer.json
  2. 6
      src/TwigTweakExtension.php
  3. 14
      src/UrlExtractor.php
  4. 2
      src/View/RegionViewBuilder.php
  5. 5
      tests/src/Functional/TwigTweakTest.php
  6. 5
      tests/src/Kernel/AbstractExtractorTestCase.php
  7. 2
      tests/src/Kernel/UrlExtractorTest.php
  8. 2
      tests/twig_tweak_test/twig_tweak_test.info.yml
  9. 2
      twig_tweak.info.yml
  10. 2
      twig_tweak.services.yml

2
composer.json

@ -12,7 +12,7 @@
"require": { "require": {
"php": ">=7.3", "php": ">=7.3",
"ext-json": "*", "ext-json": "*",
"drupal/core": "^9.0", "drupal/core": "^9.3 || ^10.0",
"twig/twig": "^2.12", "twig/twig": "^2.12",
"symfony/polyfill-php80": "^1.17" "symfony/polyfill-php80": "^1.17"
}, },

6
src/TwigTweakExtension.php

@ -324,7 +324,8 @@ class TwigTweakExtension extends AbstractExtension {
if ($route = \Drupal::routeMatch()->getRouteObject()) { if ($route = \Drupal::routeMatch()->getRouteObject()) {
$title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route); $title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route);
} }
$build['#markup'] = render($title); $build['#markup'] = is_array($title) ?
\Drupal::service('renderer')->render($title) : $title;
$build['#cache']['contexts'] = ['url']; $build['#cache']['contexts'] = ['url'];
return $build; return $build;
} }
@ -503,7 +504,8 @@ class TwigTweakExtension extends AbstractExtension {
return NULL; return NULL;
} }
return file_url_transform_relative($image_style->buildUrl($path)); return \Drupal::service('file_url_generator')
->transformRelative($image_style->buildUrl($path));
} }
/** /**

14
src/UrlExtractor.php

@ -7,6 +7,7 @@ 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;
@ -24,11 +25,19 @@ class UrlExtractor {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The file URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected $fileUrlGenerator;
/** /**
* Constructs a UrlExtractor object. * Constructs a UrlExtractor object.
*/ */
public function __construct(EntityTypeManagerInterface $entity_type_manager) { public function __construct(EntityTypeManagerInterface $entity_type_manager, FileUrlGeneratorInterface $file_url_generator) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->fileUrlGenerator = $file_url_generator;
} }
/** /**
@ -44,8 +53,7 @@ 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 = file_create_url($input); return $this->fileUrlGenerator->{$relative ? 'generateString' : 'generateAbsoluteString'}($input);
return $relative ? file_url_transform_relative($url) : $url;
} }
elseif ($input instanceof LinkItemInterface) { elseif ($input instanceof LinkItemInterface) {
return $input->getUrl()->toString(); return $input->getUrl()->toString();

2
src/View/RegionViewBuilder.php

@ -7,7 +7,7 @@ use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface; use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
/** /**

5
tests/src/Functional/TwigTweakTest.php

@ -5,6 +5,7 @@ namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait; use Drupal\Tests\TestFileCreationTrait;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
@ -53,14 +54,14 @@ final class TwigTweakTest extends BrowserTestBase {
$image_file = File::create([ $image_file = File::create([
'uri' => $test_files[0]->uri, 'uri' => $test_files[0]->uri,
'uuid' => 'b2c22b6f-7bf8-4da4-9de5-316e93487518', 'uuid' => 'b2c22b6f-7bf8-4da4-9de5-316e93487518',
'status' => FILE_STATUS_PERMANENT, 'status' => FileInterface::STATUS_PERMANENT,
]); ]);
$image_file->save(); $image_file->save();
$media_file = File::create([ $media_file = File::create([
'uri' => $test_files[8]->uri, 'uri' => $test_files[8]->uri,
'uuid' => '5dd794d0-cb75-4130-9296-838aebc1fe74', 'uuid' => '5dd794d0-cb75-4130-9296-838aebc1fe74',
'status' => FILE_STATUS_PERMANENT, 'status' => FileInterface::STATUS_PERMANENT,
]); ]);
$media_file->save(); $media_file->save();

5
tests/src/Kernel/AbstractExtractorTestCase.php

@ -3,6 +3,7 @@
namespace Drupal\Tests\twig_tweak\Kernel; namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\KernelTests\KernelTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media; use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
@ -57,14 +58,14 @@ abstract class AbstractExtractorTestCase extends KernelTestBase {
$image_file = File::create([ $image_file = File::create([
'uri' => $test_files[0]->uri, 'uri' => $test_files[0]->uri,
'uuid' => 'a2cb2b6f-7bf8-4da4-9de5-316e93487518', 'uuid' => 'a2cb2b6f-7bf8-4da4-9de5-316e93487518',
'status' => FILE_STATUS_PERMANENT, 'status' => FileInterface::STATUS_PERMANENT,
]); ]);
$image_file->save(); $image_file->save();
$media_file = File::create([ $media_file = File::create([
'uri' => $test_files[2]->uri, 'uri' => $test_files[2]->uri,
'uuid' => '5dd794d0-cb75-4130-9296-838aebc1fe74', 'uuid' => '5dd794d0-cb75-4130-9296-838aebc1fe74',
'status' => FILE_STATUS_PERMANENT, 'status' => FileInterface::STATUS_PERMANENT,
]); ]);
$media_file->save(); $media_file->save();

2
tests/src/Kernel/UrlExtractorTest.php

@ -15,7 +15,7 @@ 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 = file_create_url(''); $base_url = \Drupal::service('file_url_generator')->generateAbsoluteString('');
$request = \Drupal::request(); $request = \Drupal::request();
$absolute_url = "{$request->getScheme()}://{$request->getHost()}/foo/bar.txt"; $absolute_url = "{$request->getScheme()}://{$request->getHost()}/foo/bar.txt";

2
tests/twig_tweak_test/twig_tweak_test.info.yml

@ -2,7 +2,7 @@ name: Twig tweak test
type: module type: module
description: Support module for Twig tweak testing. description: Support module for Twig tweak testing.
package: Testing package: Testing
core_version_requirement: ^9 core_version_requirement: ^9 || ^10
dependencies: dependencies:
- drupal:system (>= 9.0) - drupal:system (>= 9.0)
- drupal:block - drupal:block

2
twig_tweak.info.yml

@ -1,6 +1,6 @@
name: Twig Tweak name: Twig Tweak
type: module type: module
description: Provides some extra Twig functions and filters. description: Provides some extra Twig functions and filters.
core_version_requirement: ^9 core_version_requirement: ^9 || ^10
dependencies: dependencies:
- drupal:system (>=9.0) - drupal:system (>=9.0)

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'] arguments: ['@entity_type.manager', '@file_url_generator']
twig_tweak.uri_extractor: twig_tweak.uri_extractor:
class: Drupal\twig_tweak\UriExtractor class: Drupal\twig_tweak\UriExtractor

Loading…
Cancel
Save