vendor/sulu/sulu/src/Sulu/Component/Content/Types/Link.php line 107

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Sulu.
  5.  *
  6.  * (c) Sulu GmbH
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Sulu\Component\Content\Types;
  12. use PHPCR\NodeInterface;
  13. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderPoolInterface;
  14. use Sulu\Component\Content\Compat\PropertyInterface;
  15. use Sulu\Component\Content\SimpleContentType;
  16. /**
  17.  * Link selection content type for linking to different providers.
  18.  */
  19. class Link extends SimpleContentType
  20. {
  21.     public const LINK_TYPE_EXTERNAL 'external';
  22.     /**
  23.      * @var LinkProviderPoolInterface
  24.      */
  25.     private $providerPool;
  26.     public function __construct(LinkProviderPoolInterface $providerPool)
  27.     {
  28.         parent::__construct('Link');
  29.         $this->providerPool $providerPool;
  30.     }
  31.     /**
  32.      * @param mixed[] $value
  33.      */
  34.     protected function encodeValue($value): string
  35.     {
  36.         return (string) \json_encode($value\defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR 0);
  37.     }
  38.     /**
  39.      * @param string|null $value
  40.      *
  41.      * @return mixed[]
  42.      */
  43.     protected function decodeValue($value): array
  44.     {
  45.         if (null === $value) {
  46.             return [];
  47.         }
  48.         return \json_decode($valuetrue512\defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR 0);
  49.     }
  50.     /**
  51.      * @return mixed[]
  52.      */
  53.     public function getViewData(PropertyInterface $property): array
  54.     {
  55.         $value $property->getValue();
  56.         if (!$value) {
  57.             return [];
  58.         }
  59.         $result = [
  60.             'provider' => $value['provider'],
  61.             'locale' => $value['locale'],
  62.         ];
  63.         if (isset($value['target'])) {
  64.             $result['target'] = $value['target'];
  65.         }
  66.         if (isset($value['title'])) {
  67.             $result['title'] = $value['title'];
  68.         }
  69.         if (isset($value['rel'])) {
  70.             $result['rel'] = $value['rel'];
  71.         }
  72.         return $result;
  73.     }
  74.     public function getContentData(PropertyInterface $property): ?string
  75.     {
  76.         $value $property->getValue();
  77.         if (!$value || !isset($value['provider'])) {
  78.             return null;
  79.         }
  80.         if (self::LINK_TYPE_EXTERNAL === $value['provider']) {
  81.             return $value['href'];
  82.         }
  83.         $provider $this->providerPool->getProvider($value['provider']);
  84.         $linkItems $provider->preload([$value['href']], $value['locale'], true);
  85.         if (=== \count($linkItems)) {
  86.             return null;
  87.         }
  88.         $url \reset($linkItems)->getUrl();
  89.         if (isset($value['query'])) {
  90.             $url \sprintf('%s?%s'$url$value['query']);
  91.         }
  92.         if (isset($value['anchor'])) {
  93.             $url \sprintf('%s#%s'$url$value['anchor']);
  94.         }
  95.         return $url;
  96.     }
  97.     public function importData(
  98.         NodeInterface $node,
  99.         PropertyInterface $property,
  100.         $value,
  101.         $userId,
  102.         $webspaceKey,
  103.         $languageCode,
  104.         $segmentKey null
  105.     ): void {
  106.         $property->setValue(\json_decode($valuetrue));
  107.         $this->write($node$property$userId$webspaceKey$languageCode$segmentKey);
  108.     }
  109. }