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", diff --git a/src/Controller/GuideController.php b/src/Controller/GuideController.php index 04c3530..e6aa6ea 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 5acb42d..36f9134 100644 --- a/src/Resources/views/Guide/list.html.twig +++ b/src/Resources/views/Guide/list.html.twig @@ -26,14 +26,14 @@ {% if shortcodeTag.description is defined %} {{ shortcodeTag.description }} {% else %} - No description configured + No description available {% endif %} {% if shortcodeTag.example is defined %} [{{ shortcodeTag.example }}] {% else %} - No example configured + No example available {% endif %} @@ -42,6 +42,6 @@ {% 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 %}