From ed267ddc0c89f9649f6bff2206155f92ef346e79 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 17 Jan 2024 16:38:24 +0100 Subject: [PATCH 1/3] Add a form to try out shortcodes in the "guide" --- src/Controller/GuideController.php | 66 +++++++++++++++++----- src/Resources/views/Guide/detail.html.twig | 28 +++++---- src/Resources/views/Guide/list.html.twig | 8 +-- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/Controller/GuideController.php b/src/Controller/GuideController.php index 04c3530..b2e585a 100644 --- a/src/Controller/GuideController.php +++ b/src/Controller/GuideController.php @@ -2,6 +2,9 @@ namespace Webfactory\ShortcodeBundle\Controller; +use Symfony\Component\Form\Extension\Core\Type\FormType; +use Symfony\Component\Form\Extension\Core\Type\TextareaType; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -13,8 +16,6 @@ */ final class GuideController { - private $twig; - /** * @var array * @@ -28,13 +29,24 @@ final class GuideController */ private $shortcodeTags; + /** + * @var Environment|Twig_Environment + */ + private $twig; + + /** + * @var ?FormFactoryInterface + */ + private $formFactory; + /** * @param Twig_Environment|Environment $twig */ - public function __construct(array $shortcodeTags, $twig) + public function __construct(array $shortcodeTags, $twig, FormFactoryInterface $formFactory = null) { - $this->shortcodeTags = $shortcodeTags; + $this->shortcodeTags = array_combine(array_map(function (array $definition): string { return $definition['shortcode']; }, $shortcodeTags), $shortcodeTags); $this->twig = $twig; + $this->formFactory = $formFactory; } public function listAction(): Response @@ -42,20 +54,44 @@ public function listAction(): Response return new Response($this->twig->render('@WebfactoryShortcode/Guide/list.html.twig', ['shortcodeTags' => $this->shortcodeTags])); } - public function detailAction($shortcode, Request $request): Response + public function detailAction(string $shortcode, Request $request): Response { - foreach ($this->shortcodeTags as $shortcodeTag) { - if ($shortcodeTag['shortcode'] === $shortcode) { - // if custom parameters are provided, replace the example - $customParameters = $request->get('customParameters'); - if ($customParameters) { - $shortcodeTag['example'] = $shortcode.' '.$customParameters; - } - - return new Response($this->twig->render('@WebfactoryShortcode/Guide/detail.html.twig', ['shortcodeTag' => $shortcodeTag])); + if (!isset($this->shortcodeTags[$shortcode])) { + throw new NotFoundHttpException(); + } + + $shortcodeTag = $this->shortcodeTags[$shortcode]; + + // if custom parameters are provided, replace the example + $customParameters = $request->get('customParameters'); + if ($customParameters) { + $shortcodeTag['example'] = $shortcode.' '.$customParameters; + } + + $example = '[' . ($shortcodeTag['example'] ?? $shortcode ) . ']'; + + if ($this->formFactory) { + $formBuilder = $this->formFactory->createBuilder(FormType::class, ['example' => $example], ['method' => 'GET']); + $formBuilder->add('example', TextareaType::class); + $form = $formBuilder->getForm(); + + $form->handleRequest($request); + + if ($form->isSubmitted()) { + $example = $form->getData()['example']; } + } else { + $form = null; } - throw new NotFoundHttpException(); + return new Response( + $this->twig->render( + '@WebfactoryShortcode/Guide/detail.html.twig', [ + 'shortcodeTag' => $shortcodeTag, + 'example' => $example, + 'form' => $form ? $form->createView() : null, + ] + ) + ); } } diff --git a/src/Resources/views/Guide/detail.html.twig b/src/Resources/views/Guide/detail.html.twig index 55ff717..8c1403d 100644 --- a/src/Resources/views/Guide/detail.html.twig +++ b/src/Resources/views/Guide/detail.html.twig @@ -5,7 +5,8 @@ {% endblock %} {% block content %} -

Shortcode {{ shortcodeTag.shortcode }}

+

Shortcode preview

+

[{{ shortcodeTag.shortcode }}]

{% if shortcodeTag.description is defined %} {{ shortcodeTag.description }} @@ -14,17 +15,24 @@ {% endif %}

-

Example

- {% if shortcodeTag.example is defined %} -

[{{ shortcodeTag.example }}] becomes:

-
- {{ ('[' ~ shortcodeTag.example ~ ']') |shortcodes }} -
+

+ Back to the shortcodes list +

+ + {% if form %} + {{ form_start(form) }} + {{ form_widget(form) }} + + {{ form_end(form) }} + {% elseif shortcodeTag.example is defined %} +

Example code: {{ example }}

{% else %}

No example configured.

{% endif %} -

- Back to the shortcodes list -

+ {% if shortcodeTag.example is defined %} +
+ {{ example|shortcodes }} +
+ {% endif %} {% endblock %} diff --git a/src/Resources/views/Guide/list.html.twig b/src/Resources/views/Guide/list.html.twig index 5c7ec09..d2b666c 100644 --- a/src/Resources/views/Guide/list.html.twig +++ b/src/Resources/views/Guide/list.html.twig @@ -5,7 +5,7 @@ {% endblock %} {% block content %} -

List of Shortcode

+

List of shortcodes

{% if shortcodeTags %} @@ -26,14 +26,14 @@ {% if shortcodeTag.description is defined %} {{ shortcodeTag.description }} {% else %} - No description configured + No description available {% endif %} @@ -42,6 +42,6 @@
{% if shortcodeTag.example is defined %} [{{ shortcodeTag.example }}] {% else %} - No example configured + No example available {% endif %}
{% else %}

No shortcodes configured.

-

See the README.md of the WebfactoryShortcodeBundle to configure some.

+

See the WebfactoryShortcodeBundle README.md on how to configure this documentation.

{% endif %} {% endblock %} From 8a57ebe9dae82a2c697d593820417a36ae326704 Mon Sep 17 00:00:00 2001 From: mpdude Date: Wed, 17 Jan 2024 15:40:11 +0000 Subject: [PATCH 2/3] Fix CS with PHP-CS-Fixer --- src/Controller/GuideController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/GuideController.php b/src/Controller/GuideController.php index b2e585a..e6aa6ea 100644 --- a/src/Controller/GuideController.php +++ b/src/Controller/GuideController.php @@ -68,7 +68,7 @@ public function detailAction(string $shortcode, Request $request): Response $shortcodeTag['example'] = $shortcode.' '.$customParameters; } - $example = '[' . ($shortcodeTag['example'] ?? $shortcode ) . ']'; + $example = '['.($shortcodeTag['example'] ?? $shortcode).']'; if ($this->formFactory) { $formBuilder = $this->formFactory->createBuilder(FormType::class, ['example' => $example], ['method' => 'GET']); From a6d359ce40871047b891efda07a91395c2356593 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 17 Jan 2024 16:47:25 +0100 Subject: [PATCH 3/3] Add missing dependency --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 72dda00..9275dcd 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "symfony/config": "^4.4|^5.0|^6.0", "symfony/dependency-injection": "^4.4|^5.0|^6.0", "symfony/deprecation-contracts": "^2.5|^3.0", + "symfony/form": "^4.4|^5.0|^6.0", "symfony/http-foundation": "^5.3|^6.0", "symfony/http-kernel": "^4.4|^5.0|^6.0", "thunderer/shortcode": "^0.6.5|^0.7",