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