vendor/pimcore/pimcore/lib/Twig/Extension/DocumentEditableExtension.php line 92

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\Twig\Extension;
  15. use Pimcore\Model\Document\Editable\BlockInterface;
  16. use Pimcore\Model\Document\PageSnippet;
  17. use Pimcore\Templating\Renderer\EditableRenderer;
  18. use Twig\Extension\AbstractExtension;
  19. use Twig\TwigFunction;
  20. /**
  21. * @internal
  22. */
  23. class DocumentEditableExtension extends AbstractExtension
  24. {
  25. /**
  26. * @var EditableRenderer
  27. */
  28. protected $editableRenderer;
  29. /**
  30. * @param EditableRenderer $editableRenderer
  31. */
  32. public function __construct(EditableRenderer $editableRenderer)
  33. {
  34. $this->editableRenderer = $editableRenderer;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getFunctions(): array
  40. {
  41. return [
  42. new TwigFunction('pimcore_*', [$this, 'renderEditable'], [
  43. 'needs_context' => true,
  44. 'is_safe' => ['html'],
  45. ]),
  46. new TwigFunction('pimcore_iterate_block', [$this, 'getBlockIterator']),
  47. ];
  48. // @phpstan-ignore-next-line those are just for auto-complete, not nice, but works ;-)
  49. new TwigFunction('pimcore_area');
  50. new TwigFunction('pimcore_areablock');
  51. new TwigFunction('pimcore_block');
  52. new TwigFunction('pimcore_checkbox');
  53. new TwigFunction('pimcore_date');
  54. new TwigFunction('pimcore_embed');
  55. new TwigFunction('pimcore_image');
  56. new TwigFunction('pimcore_input');
  57. new TwigFunction('pimcore_link');
  58. new TwigFunction('pimcore_multiselect');
  59. new TwigFunction('pimcore_numeric');
  60. new TwigFunction('pimcore_pdf');
  61. new TwigFunction('pimcore_relation');
  62. new TwigFunction('pimcore_relations');
  63. new TwigFunction('pimcore_renderlet');
  64. new TwigFunction('pimcore_scheduledblock');
  65. new TwigFunction('pimcore_select');
  66. new TwigFunction('pimcore_snippet');
  67. new TwigFunction('pimcore_table');
  68. new TwigFunction('pimcore_textarea');
  69. new TwigFunction('pimcore_video');
  70. new TwigFunction('pimcore_wysiwyg');
  71. }
  72. /**
  73. * @internal
  74. *
  75. * @param array $context
  76. * @param string $type
  77. * @param string $name
  78. * @param array $options
  79. *
  80. * @return \Pimcore\Model\Document\Editable\EditableInterface|string
  81. *
  82. * @throws \Exception
  83. */
  84. public function renderEditable(array $context, string $type, string $name, array $options = [])
  85. {
  86. $document = $context['document'];
  87. $editmode = $context['editmode'];
  88. if (!($document instanceof PageSnippet)) {
  89. return '';
  90. }
  91. return $this->editableRenderer->render($document, $type, $name, $options, $editmode);
  92. }
  93. /**
  94. * Returns an iterator which can be used instead of while($block->loop())
  95. *
  96. * @internal
  97. *
  98. * @param BlockInterface $block
  99. *
  100. * @return \Generator
  101. */
  102. public function getBlockIterator(BlockInterface $block): \Generator
  103. {
  104. return $block->getIterator();
  105. }
  106. }