Skip to content

Commit

Permalink
Get fullpath for link
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Jan 13, 2025
1 parent 02582a1 commit 0f96b04
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/DataObject/Data/Adapter/LinkAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);

Check failure on line 71 in src/DataObject/Data/Adapter/LinkAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.2, lowest, false) / Static analysis with phpstan

Call to an undefined method Pimcore\Model\DataObject\ClassDefinition\Data::normalize().

Check failure on line 71 in src/DataObject/Data/Adapter/LinkAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.3, highest, 11.x-dev as 11.99.9, true) / Static analysis with phpstan

Call to an undefined method Pimcore\Model\DataObject\ClassDefinition\Data::normalize().

Check failure on line 71 in src/DataObject/Data/Adapter/LinkAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.3, highest, false) / Static analysis with phpstan

Call to an undefined method Pimcore\Model\DataObject\ClassDefinition\Data::normalize().

$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;
}
}

0 comments on commit 0f96b04

Please sign in to comment.