From 0f96b04d9f1aa409cc090ace02a9a5a5612ffe32 Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 13 Jan 2025 15:33:09 +0100 Subject: [PATCH] Get fullpath for link --- src/DataObject/Data/Adapter/LinkAdapter.php | 48 ++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/DataObject/Data/Adapter/LinkAdapter.php b/src/DataObject/Data/Adapter/LinkAdapter.php index 84377dcf..c7ac61b1 100644 --- a/src/DataObject/Data/Adapter/LinkAdapter.php +++ b/src/DataObject/Data/Adapter/LinkAdapter.php @@ -17,20 +17,30 @@ namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter; use Exception; +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; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait; use Pimcore\Model\DataObject\ClassDefinition\Data; use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\DataObject\Data\Link; +use Pimcore\Normalizer\NormalizerInterface; use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; /** * @internal */ #[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)] -final readonly class LinkAdapter implements SetterDataInterface +final readonly class LinkAdapter implements SetterDataInterface, DataNormalizerInterface { + use ElementProviderTrait; + public function __construct(private ServiceResolverInterface $serviceResolver) + { + } + /** * @throws Exception */ @@ -51,4 +61,40 @@ public function getDataForSetter( return $link; } + + public function normalize(mixed $value, Data $fieldDefinition): mixed + { + if (!$value instanceof Link) { + return null; + } + + $data = $fieldDefinition->normalize($value); + + $data['fullPath'] = $this->getFullPath($value); + + return $data; + } + + private function getFullPath(Link $link): ?string + { + $fullPath = null; + + if($link->getDirect() !== null) { + return $link->getDirect(); + } + + if ($link->getInternal() && $link->getInternalType()) { + try { + $element = $this->getElement( + $this->serviceResolver, + $link->getInternalType(), + $link->getInternal() + ); + $fullPath = $element->getRealFullPath(); + } catch (NotFoundException ) { + return null; + } + } + return $fullPath; + } }