1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Content\Media\Cms; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity; |
6
|
|
|
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection; |
7
|
|
|
use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver; |
8
|
|
|
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection; |
9
|
|
|
use Shopware\Core\Content\Cms\DataResolver\FieldConfig; |
10
|
|
|
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext; |
11
|
|
|
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext; |
12
|
|
|
use Shopware\Core\Content\Cms\SalesChannel\Struct\ImageStruct; |
13
|
|
|
use Shopware\Core\Content\Media\MediaDefinition; |
14
|
|
|
use Shopware\Core\Content\Media\MediaEntity; |
15
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
16
|
|
|
|
17
|
|
|
class YoutubeVideoCmsElementResolver extends AbstractCmsElementResolver |
18
|
|
|
{ |
19
|
|
|
public function getType(): string |
20
|
|
|
{ |
21
|
|
|
return 'youtube-video'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection |
25
|
|
|
{ |
26
|
|
|
$config = $slot->getFieldConfig(); |
27
|
|
|
$mediaConfig = $config->get('previewMedia'); |
28
|
|
|
|
29
|
|
|
if (!$mediaConfig || $mediaConfig->isMapped() || $mediaConfig->getValue() === null) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$criteria = new Criteria([$mediaConfig->getValue()]); |
34
|
|
|
|
35
|
|
|
$criteriaCollection = new CriteriaCollection(); |
36
|
|
|
$criteriaCollection->add('media_' . $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria); |
37
|
|
|
|
38
|
|
|
return $criteriaCollection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void |
42
|
|
|
{ |
43
|
|
|
$config = $slot->getFieldConfig(); |
44
|
|
|
$image = new ImageStruct(); |
45
|
|
|
$slot->setData($image); |
46
|
|
|
|
47
|
|
|
$mediaConfig = $config->get('previewMedia'); |
48
|
|
|
if ($mediaConfig && $mediaConfig->getValue()) { |
49
|
|
|
$this->addMediaEntity($slot, $image, $result, $mediaConfig, $resolverContext); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function addMediaEntity(CmsSlotEntity $slot, ImageStruct $image, ElementDataCollection $result, FieldConfig $config, ResolverContext $resolverContext): void |
54
|
|
|
{ |
55
|
|
|
if ($config->isMapped() && $resolverContext instanceof EntityResolverContext) { |
56
|
|
|
/** @var MediaEntity|null $media */ |
57
|
|
|
$media = $this->resolveEntityValue($resolverContext->getEntity(), $config->getValue()); |
58
|
|
|
|
59
|
|
|
if ($media !== null) { |
60
|
|
|
$image->setMediaId($media->getUniqueIdentifier()); |
61
|
|
|
$image->setMedia($media); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($config->isStatic()) { |
66
|
|
|
$image->setMediaId($config->getValue()); |
67
|
|
|
|
68
|
|
|
$searchResult = $result->get('media_' . $slot->getUniqueIdentifier()); |
69
|
|
|
if (!$searchResult) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @var MediaEntity|null $media */ |
74
|
|
|
$media = $searchResult->get($config->getValue()); |
75
|
|
|
if (!$media) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$image->setMedia($media); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|