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

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\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  15. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreNotExistsException;
  16. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface;
  17. use Sulu\Component\Content\Compat\PropertyInterface;
  18. use Sulu\Component\Content\SimpleContentType;
  19. /**
  20. * Link selection content type for linking to different providers.
  21. */
  22. class Link extends SimpleContentType
  23. {
  24. /**
  25. * @deprecated
  26. */
  27. public const LINK_TYPE_EXTERNAL = 'external';
  28. public function __construct(
  29. private LinkProviderPoolInterface $providerPool,
  30. private ?ReferenceStorePoolInterface $referenceStorePool = null,
  31. ) {
  32. if (!$this->referenceStorePool instanceof ReferenceStorePoolInterface) {
  33. @trigger_deprecation('sulu/sulu', '2.5.27', 'Initializing "' . __CLASS__ . '" without referenceStorePool is deprecated.');
  34. }
  35. parent::__construct('Link');
  36. }
  37. /**
  38. * @param mixed[] $value
  39. */
  40. protected function encodeValue($value): string
  41. {
  42. return (string) \json_encode($value, \defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR : 0);
  43. }
  44. /**
  45. * @param string|null $value
  46. *
  47. * @return mixed[]
  48. */
  49. protected function decodeValue($value): array
  50. {
  51. if (null === $value) {
  52. return [];
  53. }
  54. return \json_decode($value, true, 512, \defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR : 0);
  55. }
  56. /**
  57. * @return mixed[]
  58. */
  59. public function getViewData(PropertyInterface $property): array
  60. {
  61. $value = $property->getValue();
  62. if (!$value) {
  63. return [];
  64. }
  65. $result = [
  66. 'provider' => $value['provider'] ?? null,
  67. 'locale' => $value['locale'],
  68. ];
  69. if (isset($value['target'])) {
  70. $result['target'] = $value['target'];
  71. }
  72. if (isset($value['title'])) {
  73. $result['title'] = $value['title'];
  74. }
  75. if (isset($value['rel'])) {
  76. $result['rel'] = $value['rel'];
  77. }
  78. $result['href'] = $value['href'] ?? null;
  79. return $result;
  80. }
  81. public function getContentData(PropertyInterface $property): ?string
  82. {
  83. $value = $property->getValue();
  84. $locale = $property->getStructure()->getLanguageCode();
  85. if (!$value || !isset($value['provider'])) {
  86. return null;
  87. }
  88. $provider = $this->providerPool->getProvider($value['provider']);
  89. $linkItems = $provider->preload([$value['href']], $locale, true);
  90. if (0 === \count($linkItems)) {
  91. return null;
  92. }
  93. $url = \reset($linkItems)->getUrl();
  94. if (isset($value['query'])) {
  95. $url = \sprintf('%s?%s', $url, $value['query']);
  96. }
  97. if (isset($value['anchor'])) {
  98. $url = \sprintf('%s#%s', $url, $value['anchor']);
  99. }
  100. $referenceStore = $this->getReferenceStore($value['provider']);
  101. if (!$referenceStore) {
  102. return $url;
  103. }
  104. foreach ($linkItems as $linkItem) {
  105. if ($linkItem->getId()) {
  106. $referenceStore->add($linkItem->getId());
  107. }
  108. }
  109. return $url;
  110. }
  111. public function importData(
  112. NodeInterface $node,
  113. PropertyInterface $property,
  114. $value,
  115. $userId,
  116. $webspaceKey,
  117. $languageCode,
  118. $segmentKey = null
  119. ): void {
  120. $property->setValue(\json_decode($value, true));
  121. $this->write($node, $property, $userId, $webspaceKey, $languageCode, $segmentKey);
  122. }
  123. private function getReferenceStore(string $provider): ?ReferenceStoreInterface
  124. {
  125. try {
  126. if (!$this->referenceStorePool instanceof ReferenceStorePoolInterface) {
  127. return null;
  128. }
  129. return $this->referenceStorePool->getStore($provider);
  130. } catch (ReferenceStoreNotExistsException $exception) {
  131. return null;
  132. }
  133. }
  134. }