Skip to content

Commit

Permalink
[Improvement] Add path to video (#673)
Browse files Browse the repository at this point in the history
* Add normalizer interface to video

* Apply php-cs-fixer changes

* Add subtype and fullpath to manytoone relation

* Apply php-cs-fixer changes

* Add normalizer check

* Move subtype to data

* Apply php-cs-fixer changes
  • Loading branch information
mattamon authored Jan 13, 2025
1 parent acab670 commit 4d769cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/DataObject/Data/Adapter/ManyToOneRelationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
Expand All @@ -25,14 +26,15 @@
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Normalizer\NormalizerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use function is_array;

/**
* @internal
*/
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class ManyToOneRelationAdapter implements SetterDataInterface
final readonly class ManyToOneRelationAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ElementProviderTrait;

Expand All @@ -59,4 +61,20 @@ public function getDataForSetter(
return null;
}
}

public function normalize(mixed $value, Data $fieldDefinition): mixed
{
if (!$fieldDefinition instanceof NormalizerInterface) {
return null;
}

$data = $fieldDefinition->normalize($value);

if (!empty($data)) {
$data['fullPath'] = $value->getRealFullPath();
$data['subtype'] = $value->getType();
}

return $data;
}
}
26 changes: 24 additions & 2 deletions src/DataObject/Data/Adapter/VideoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter;

use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
Expand All @@ -26,14 +27,15 @@
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\Video;
use Pimcore\Normalizer\NormalizerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use function is_array;

/**
* @internal
*/
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class VideoAdapter implements SetterDataInterface
final readonly class VideoAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ElementProviderTrait;

Expand All @@ -49,7 +51,6 @@ public function getDataForSetter(
?FieldContextData $contextData = null
): ?Video {
$adapterData = $data[$key] ?? null;

if (!is_array($adapterData)) {
return null;
}
Expand Down Expand Up @@ -85,4 +86,25 @@ private function getAssetByPath(?string $path): ?Asset
{
return $path ? $this->assetResolver->getByPath($path) : null;
}

public function normalize(mixed $value, Data $fieldDefinition): mixed
{

if (!$value instanceof Video || !$fieldDefinition instanceof NormalizerInterface) {
return null;
}

$data = $fieldDefinition->normalize($value);

if (isset($data['poster'])) {
$data['poster']['fullPath'] = $value->getPoster()?->getRealFullPath();
}

if (isset($data['data'])) {
$data['data']['fullPath'] = $value->getData()?->getRealFullPath();
$data['data']['subtype'] = $value->getData()?->getType();
}

return $data;
}
}

0 comments on commit 4d769cf

Please sign in to comment.