From 22eb3dd737cbf6ebf4e8d2d1fbdb99da4e8c3726 Mon Sep 17 00:00:00 2001
From: Tomasz Kryszan <tomasz.kryszan@ez.no>
Date: Fri, 18 Aug 2023 14:56:59 +0200
Subject: [PATCH] Added EmbeddedItemEditFormExtension

---
 .../config/services/ui_config/common.yaml     |  2 +
 .../Twig/EmbeddedItemEditFormExtension.php    | 52 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 src/bundle/Templating/Twig/EmbeddedItemEditFormExtension.php

diff --git a/src/bundle/Resources/config/services/ui_config/common.yaml b/src/bundle/Resources/config/services/ui_config/common.yaml
index 316a31fc35..8ba39dd7c1 100644
--- a/src/bundle/Resources/config/services/ui_config/common.yaml
+++ b/src/bundle/Resources/config/services/ui_config/common.yaml
@@ -96,6 +96,8 @@ services:
 
     Ibexa\Bundle\AdminUi\Templating\Twig\ContentTypeIconExtension: ~
 
+    Ibexa\Bundle\AdminUi\Templating\Twig\EmbeddedItemEditFormExtension: ~
+
     Ibexa\AdminUi\UI\Config\Provider\UserContentTypes:
         tags:
             - { name: ibexa.admin_ui.config.provider, key: 'userContentTypes' }
diff --git a/src/bundle/Templating/Twig/EmbeddedItemEditFormExtension.php b/src/bundle/Templating/Twig/EmbeddedItemEditFormExtension.php
new file mode 100644
index 0000000000..2ea759d786
--- /dev/null
+++ b/src/bundle/Templating/Twig/EmbeddedItemEditFormExtension.php
@@ -0,0 +1,52 @@
+<?php
+
+/**
+ * @copyright Copyright (C) Ibexa AS. All rights reserved.
+ * @license For full copyright and license information view LICENSE file distributed with this source code.
+ */
+declare(strict_types=1);
+
+namespace Ibexa\Bundle\AdminUi\Templating\Twig;
+
+use Ibexa\AdminUi\Form\Data\Content\Draft\ContentEditData;
+use Ibexa\AdminUi\Form\Factory\FormFactory;
+use Symfony\Component\Form\FormView;
+use Symfony\Component\Routing\RouterInterface;
+use Twig\Extension\AbstractExtension;
+use Twig\TwigFunction;
+
+final class EmbeddedItemEditFormExtension extends AbstractExtension
+{
+    private FormFactory $formFactory;
+
+    private RouterInterface $router;
+
+    public function __construct(
+        FormFactory $formFactory,
+        RouterInterface $router
+    ) {
+        $this->formFactory = $formFactory;
+        $this->router = $router;
+    }
+
+    public function getFunctions(): array
+    {
+        return [
+            new TwigFunction(
+                'ibexa_render_embedded_item_edit_form',
+                [$this, 'renderEmbeddedItemEditForm']
+            ),
+        ];
+    }
+
+    public function renderEmbeddedItemEditForm(): FormView
+    {
+        return $this->formFactory->contentEdit(
+            new ContentEditData(),
+            'embedded_item_edit',
+            [
+                'action' => $this->router->generate('ibexa.content.edit'),
+            ]
+        )->createView();
+    }
+}