Chi
5 years ago
10 changed files with 258 additions and 134 deletions
@ -0,0 +1,84 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\twig_tweak; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\ContentEntityInterface; |
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||||
|
use Drupal\Core\Field\EntityReferenceFieldItemListInterface; |
||||||
|
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; |
||||||
|
use Drupal\file\FileInterface; |
||||||
|
use Drupal\media\MediaInterface; |
||||||
|
use Drupal\media\Plugin\media\Source\OEmbedInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* URI extractor service. |
||||||
|
*/ |
||||||
|
class UriExtractor { |
||||||
|
|
||||||
|
/** |
||||||
|
* The entity type manager. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a UrlExtractor object. |
||||||
|
*/ |
||||||
|
public function __construct(EntityTypeManagerInterface $entity_type_manager) { |
||||||
|
$this->entityTypeManager = $entity_type_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a URI to the file. |
||||||
|
* |
||||||
|
* @param object|null $input |
||||||
|
* An object that contains the URI. |
||||||
|
* |
||||||
|
* @return string|null |
||||||
|
* A URI that may be used to access the file. |
||||||
|
*/ |
||||||
|
public function extractUri(?object $input): ?string { |
||||||
|
if ($input instanceof ContentEntityInterface) { |
||||||
|
return self::getUriFromEntity($input); |
||||||
|
} |
||||||
|
elseif ($input instanceof EntityReferenceFieldItemListInterface) { |
||||||
|
if ($item = $input->first()) { |
||||||
|
return $this->getUriFromEntity($item->entity); |
||||||
|
} |
||||||
|
} |
||||||
|
elseif ($input instanceof EntityReferenceItem) { |
||||||
|
return self::getUriFromEntity($input->entity); |
||||||
|
} |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Extracts file URI from content entity. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\ContentEntityInterface $entity |
||||||
|
* Entity object that contains information about the file. |
||||||
|
* |
||||||
|
* @return string|null |
||||||
|
* A URI that may be used to access the file. |
||||||
|
*/ |
||||||
|
private function getUriFromEntity(ContentEntityInterface $entity): ?string { |
||||||
|
if ($entity instanceof MediaInterface) { |
||||||
|
$source = $entity->getSource(); |
||||||
|
$value = $source->getSourceFieldValue($entity); |
||||||
|
if ($source instanceof OEmbedInterface) { |
||||||
|
return $value; |
||||||
|
} |
||||||
|
/** @var \Drupal\file\FileInterface $file */ |
||||||
|
$file = $this->entityTypeManager->getStorage('file')->load($value); |
||||||
|
if ($file) { |
||||||
|
return $file->getFileUri(); |
||||||
|
} |
||||||
|
} |
||||||
|
elseif ($entity instanceof FileInterface) { |
||||||
|
return $entity->getFileUri(); |
||||||
|
} |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,91 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\twig_tweak\Kernel; |
||||||
|
|
||||||
|
use Drupal\file\Entity\File; |
||||||
|
use Drupal\KernelTests\KernelTestBase; |
||||||
|
use Drupal\media\Entity\Media; |
||||||
|
use Drupal\node\Entity\Node; |
||||||
|
use Drupal\Tests\TestFileCreationTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* A base class of URL and URI extractor tests. |
||||||
|
*/ |
||||||
|
abstract class AbstractExtractorTest extends KernelTestBase { |
||||||
|
|
||||||
|
use TestFileCreationTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* A node to test. |
||||||
|
* |
||||||
|
* @var \Drupal\node\NodeInterface |
||||||
|
*/ |
||||||
|
protected $node; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static $modules = [ |
||||||
|
'twig_tweak', |
||||||
|
'twig_tweak_test', |
||||||
|
'system', |
||||||
|
'views', |
||||||
|
'node', |
||||||
|
'block', |
||||||
|
'image', |
||||||
|
'field', |
||||||
|
'text', |
||||||
|
'media', |
||||||
|
'file', |
||||||
|
'user', |
||||||
|
'filter', |
||||||
|
]; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function setUp(): void { |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
$this->installConfig(['node', 'twig_tweak_test']); |
||||||
|
$this->installSchema('file', 'file_usage'); |
||||||
|
$this->installEntitySchema('file'); |
||||||
|
$this->installEntitySchema('media'); |
||||||
|
|
||||||
|
$test_files = $this->getTestFiles('image'); |
||||||
|
// |
||||||
|
$image_file = File::create([ |
||||||
|
'uri' => $test_files[0]->uri, |
||||||
|
'uuid' => 'a2cb2b6f-7bf8-4da4-9de5-316e93487518', |
||||||
|
'status' => FILE_STATUS_PERMANENT, |
||||||
|
]); |
||||||
|
$image_file->save(); |
||||||
|
|
||||||
|
$media_file = File::create([ |
||||||
|
'uri' => $test_files[2]->uri, |
||||||
|
'uuid' => '5dd794d0-cb75-4130-9296-838aebc1fe74', |
||||||
|
'status' => FILE_STATUS_PERMANENT, |
||||||
|
]); |
||||||
|
$media_file->save(); |
||||||
|
|
||||||
|
$media = Media::create([ |
||||||
|
'bundle' => 'image', |
||||||
|
'name' => 'Image 1', |
||||||
|
'field_media_image' => ['target_id' => $media_file->id()], |
||||||
|
]); |
||||||
|
$media->save(); |
||||||
|
|
||||||
|
$node_values = [ |
||||||
|
'title' => 'Alpha', |
||||||
|
'type' => 'page', |
||||||
|
'field_image' => [ |
||||||
|
'target_id' => $image_file->id(), |
||||||
|
], |
||||||
|
'field_media' => [ |
||||||
|
'target_id' => $media->id(), |
||||||
|
], |
||||||
|
]; |
||||||
|
$this->node = Node::create($node_values); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,61 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\twig_tweak\Kernel; |
||||||
|
|
||||||
|
/** |
||||||
|
* A test for URI extractor service. |
||||||
|
* |
||||||
|
* @group twig_tweak |
||||||
|
*/ |
||||||
|
final class UriExtractorTest extends AbstractExtractorTest { |
||||||
|
|
||||||
|
/** |
||||||
|
* Test callback. |
||||||
|
*/ |
||||||
|
public function testUrlExtractor(): void { |
||||||
|
|
||||||
|
$extractor = $this->container->get('twig_tweak.uri_extractor'); |
||||||
|
|
||||||
|
$url = $extractor->extractUri(NULL); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('title')); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_image')[0]); |
||||||
|
self::assertSame('public://image-test.png', $url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_image')[1]); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_image')); |
||||||
|
self::assertSame('public://image-test.png', $url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_image')->entity); |
||||||
|
self::assertSame('public://image-test.png', $url); |
||||||
|
|
||||||
|
$this->node->get('field_image')->removeItem(0); |
||||||
|
$url = $extractor->extractUri($this->node->get('field_image')); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_media')[0]); |
||||||
|
self::assertSame('public://image-test.gif', $url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_media')[1]); |
||||||
|
self::assertNull($url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_media')); |
||||||
|
self::assertSame('public://image-test.gif', $url); |
||||||
|
|
||||||
|
$url = $extractor->extractUri($this->node->get('field_media')->entity); |
||||||
|
self::assertSame('public://image-test.gif', $url); |
||||||
|
|
||||||
|
$this->node->get('field_media')->removeItem(0); |
||||||
|
$url = $extractor->extractUri($this->node->get('field_media')); |
||||||
|
self::assertNull($url); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue