vendor/pimcore/pimcore/lib/Templating/Renderer/EditableRenderer.php line 142

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Templating\Renderer;
  15. use Pimcore\Document\Editable\EditmodeEditableDefinitionCollector;
  16. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  17. use Pimcore\Model\Document\Editable;
  18. use Pimcore\Model\Document\Editable\LazyLoadingInterface;
  19. use Pimcore\Model\Document\Editable\Loader\EditableLoaderInterface;
  20. use Pimcore\Model\Document\PageSnippet;
  21. use Psr\Log\LoggerAwareInterface;
  22. use Psr\Log\LoggerAwareTrait;
  23. /**
  24. * @internal
  25. */
  26. class EditableRenderer implements LoggerAwareInterface
  27. {
  28. use LoggerAwareTrait;
  29. /**
  30. * @var EditableLoaderInterface
  31. */
  32. protected $editableLoader;
  33. /**
  34. * @var EditmodeResolver
  35. */
  36. protected $editmodeResolver;
  37. protected ?EditmodeEditableDefinitionCollector $configCollector;
  38. /**
  39. * @param EditableLoaderInterface $editableLoader
  40. * @param EditmodeResolver $editmodeResolver
  41. * @param EditmodeEditableDefinitionCollector $configCollector
  42. */
  43. public function __construct(EditableLoaderInterface $editableLoader, EditmodeResolver $editmodeResolver, EditmodeEditableDefinitionCollector $configCollector)
  44. {
  45. $this->editableLoader = $editableLoader;
  46. $this->editmodeResolver = $editmodeResolver;
  47. $this->configCollector = $configCollector;
  48. }
  49. /**
  50. * @param string $type
  51. *
  52. * @return bool
  53. */
  54. public function editableExists($type)
  55. {
  56. return $this->editableLoader->supports($type);
  57. }
  58. /**
  59. * @param PageSnippet $document
  60. * @param string $type
  61. * @param string $name
  62. * @param array $config
  63. * @param bool|null $editmode
  64. *
  65. * @return Editable\EditableInterface
  66. *
  67. * @throws \Exception
  68. */
  69. public function getEditable(PageSnippet $document, string $type, string $name, array $config = [], bool $editmode = null): Editable\EditableInterface
  70. {
  71. $type = strtolower($type);
  72. $originalName = $name;
  73. $name = Editable::buildEditableName($type, $originalName, $document);
  74. $realName = Editable::buildEditableRealName($originalName, $document);
  75. if (null === $editmode) {
  76. $editmode = $this->editmodeResolver->isEditmode();
  77. }
  78. $editable = $document->getEditable($name);
  79. if ($editable instanceof Editable\EditableInterface && $editable->getType() === $type) {
  80. // call the load() method if it exists to reinitialize the data (eg. from serializing, ...)
  81. //TODO Pimcore 11: remove method_exists BC layer
  82. if ($editable instanceof LazyLoadingInterface || method_exists($editable, 'load')) {
  83. if (!$editable instanceof LazyLoadingInterface) {
  84. trigger_deprecation('pimcore/pimcore', '10.3',
  85. sprintf('Usage of method_exists is deprecated since version 10.3 and will be removed in Pimcore 11.' .
  86. 'Implement the %s interface instead.', LazyLoadingInterface::class));
  87. }
  88. $editable->load();
  89. }
  90. } else {
  91. $editable = $this->editableLoader->build($type);
  92. $editable->setName($name);
  93. $document->setEditable($editable);
  94. //set default value on initial build
  95. if (isset($config['defaultValue'])) {
  96. $editable->setDataFromResource($config['defaultValue']);
  97. }
  98. }
  99. $editable->setDocument($document);
  100. $editable->setEditmode($editmode);
  101. // set the real name of this editable, without the prefixes and suffixes from blocks and areablocks
  102. $editable->setRealName($realName);
  103. $editable->setConfig($config);
  104. if ($editmode) {
  105. $editable->setEditableDefinitionCollector($this->configCollector);
  106. }
  107. return $editable;
  108. }
  109. /**
  110. * Renders an editable
  111. *
  112. * @param PageSnippet $document
  113. * @param string $type
  114. * @param string $name
  115. * @param array $options
  116. * @param bool|null $editmode
  117. *
  118. * @return Editable\EditableInterface
  119. *
  120. * @throws \Exception
  121. */
  122. public function render(PageSnippet $document, string $type, string $name, array $options = [], bool $editmode = null)
  123. {
  124. return $this->getEditable($document, $type, $name, $options, $editmode);
  125. }
  126. }