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

Open in your IDE?
  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.         return $result;
  70.     }
  71.     public function getContentData(PropertyInterface $property): ?string
  72.     {
  73.         $value $property->getValue();
  74.         if (!$value || !isset($value['provider'])) {
  75.             return null;
  76.         }
  77.         if (self::LINK_TYPE_EXTERNAL === $value['provider']) {
  78.             return $value['href'];
  79.         }
  80.         $provider $this->providerPool->getProvider($value['provider']);
  81.         $linkItems $provider->preload([$value['href']], $value['locale'], true);
  82.         if (=== \count($linkItems)) {
  83.             return null;
  84.         }
  85.         $url = \reset($linkItems)->getUrl();
  86.         if (isset($value['anchor'])) {
  87.             $url = \sprintf('%s#%s'$url$value['anchor']);
  88.         }
  89.         return $url;
  90.     }
  91.     public function importData(
  92.         NodeInterface $node,
  93.         PropertyInterface $property,
  94.         $value,
  95.         $userId,
  96.         $webspaceKey,
  97.         $languageCode,
  98.         $segmentKey null
  99.     ): void {
  100.         $property->setValue(\json_decode($valuetrue));
  101.         $this->write($node$property$userId$webspaceKey$languageCode$segmentKey);
  102.     }
  103. }