Skip to content

Commit 4655b4f

Browse files
authored
Add a form so shortcodes can be tried out in the "guide" (#31)
This adds a textarea to the example pages shown in the "shortcode guide", so that users can modify the example code given and preview the results.
1 parent 07c82e1 commit 4655b4f

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"symfony/config": "^4.4|^5.0|^6.0",
1212
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
1313
"symfony/deprecation-contracts": "^2.5|^3.0",
14+
"symfony/form": "^4.4|^5.0|^6.0",
1415
"symfony/http-foundation": "^5.3|^6.0",
1516
"symfony/http-kernel": "^4.4|^5.0|^6.0",
1617
"thunderer/shortcode": "^0.6.5|^0.7",

src/Controller/GuideController.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Webfactory\ShortcodeBundle\Controller;
44

5+
use Symfony\Component\Form\Extension\Core\Type\FormType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
7+
use Symfony\Component\Form\FormFactoryInterface;
58
use Symfony\Component\HttpFoundation\Request;
69
use Symfony\Component\HttpFoundation\Response;
710
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -13,8 +16,6 @@
1316
*/
1417
final class GuideController
1518
{
16-
private $twig;
17-
1819
/**
1920
* @var array
2021
*
@@ -28,34 +29,69 @@ final class GuideController
2829
*/
2930
private $shortcodeTags;
3031

32+
/**
33+
* @var Environment|Twig_Environment
34+
*/
35+
private $twig;
36+
37+
/**
38+
* @var ?FormFactoryInterface
39+
*/
40+
private $formFactory;
41+
3142
/**
3243
* @param Twig_Environment|Environment $twig
3344
*/
34-
public function __construct(array $shortcodeTags, $twig)
45+
public function __construct(array $shortcodeTags, $twig, FormFactoryInterface $formFactory = null)
3546
{
36-
$this->shortcodeTags = $shortcodeTags;
47+
$this->shortcodeTags = array_combine(array_map(function (array $definition): string { return $definition['shortcode']; }, $shortcodeTags), $shortcodeTags);
3748
$this->twig = $twig;
49+
$this->formFactory = $formFactory;
3850
}
3951

4052
public function listAction(): Response
4153
{
4254
return new Response($this->twig->render('@WebfactoryShortcode/Guide/list.html.twig', ['shortcodeTags' => $this->shortcodeTags]));
4355
}
4456

45-
public function detailAction($shortcode, Request $request): Response
57+
public function detailAction(string $shortcode, Request $request): Response
4658
{
47-
foreach ($this->shortcodeTags as $shortcodeTag) {
48-
if ($shortcodeTag['shortcode'] === $shortcode) {
49-
// if custom parameters are provided, replace the example
50-
$customParameters = $request->get('customParameters');
51-
if ($customParameters) {
52-
$shortcodeTag['example'] = $shortcode.' '.$customParameters;
53-
}
54-
55-
return new Response($this->twig->render('@WebfactoryShortcode/Guide/detail.html.twig', ['shortcodeTag' => $shortcodeTag]));
59+
if (!isset($this->shortcodeTags[$shortcode])) {
60+
throw new NotFoundHttpException();
61+
}
62+
63+
$shortcodeTag = $this->shortcodeTags[$shortcode];
64+
65+
// if custom parameters are provided, replace the example
66+
$customParameters = $request->get('customParameters');
67+
if ($customParameters) {
68+
$shortcodeTag['example'] = $shortcode.' '.$customParameters;
69+
}
70+
71+
$example = '['.($shortcodeTag['example'] ?? $shortcode).']';
72+
73+
if ($this->formFactory) {
74+
$formBuilder = $this->formFactory->createBuilder(FormType::class, ['example' => $example], ['method' => 'GET']);
75+
$formBuilder->add('example', TextareaType::class);
76+
$form = $formBuilder->getForm();
77+
78+
$form->handleRequest($request);
79+
80+
if ($form->isSubmitted()) {
81+
$example = $form->getData()['example'];
5682
}
83+
} else {
84+
$form = null;
5785
}
5886

59-
throw new NotFoundHttpException();
87+
return new Response(
88+
$this->twig->render(
89+
'@WebfactoryShortcode/Guide/detail.html.twig', [
90+
'shortcodeTag' => $shortcodeTag,
91+
'example' => $example,
92+
'form' => $form ? $form->createView() : null,
93+
]
94+
)
95+
);
6096
}
6197
}

src/Resources/views/Guide/detail.html.twig

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
{% endblock %}
66

77
{% block content %}
8-
<h1>Shortcode {{ shortcodeTag.shortcode }}</h1>
8+
<h1>Shortcode preview</h1>
9+
<p><b><tt>[{{ shortcodeTag.shortcode }}]</tt></b></p>
910
<p>
1011
{% if shortcodeTag.description is defined %}
1112
{{ shortcodeTag.description }}
@@ -14,17 +15,24 @@
1415
{% endif %}
1516
</p>
1617

17-
<h2>Example</h2>
18-
{% if shortcodeTag.example is defined %}
19-
<p>[{{ shortcodeTag.example }}] becomes:</p>
20-
<div id="rendered-example">
21-
{{ ('[' ~ shortcodeTag.example ~ ']') |shortcodes }}
22-
</div>
18+
<p>
19+
<a href="{{ path('webfactory.shortcode.guide-list') }}">Back to the shortcodes list</a>
20+
</p>
21+
22+
{% if form %}
23+
{{ form_start(form) }}
24+
{{ form_widget(form) }}
25+
<input type="submit" value="Show example" />
26+
{{ form_end(form) }}
27+
{% elseif shortcodeTag.example is defined %}
28+
<p>Example code: {{ example }}</p>
2329
{% else %}
2430
<p>No example configured.</p>
2531
{% endif %}
2632

27-
<p>
28-
<a href="{{ path('webfactory.shortcode.guide-list') }}">Back to the shortcodes list</a>
29-
</p>
33+
{% if shortcodeTag.example is defined %}
34+
<div id="rendered-example">
35+
{{ example|shortcodes }}
36+
</div>
37+
{% endif %}
3038
{% endblock %}

src/Resources/views/Guide/list.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
{% if shortcodeTag.description is defined %}
2727
{{ shortcodeTag.description }}
2828
{% else %}
29-
No description configured
29+
No description available
3030
{% endif %}
3131
</td>
3232
<td>
3333
{% if shortcodeTag.example is defined %}
3434
[{{ shortcodeTag.example }}]
3535
{% else %}
36-
No example configured
36+
No example available
3737
{% endif %}
3838
</td>
3939
</tr>
@@ -42,6 +42,6 @@
4242
</table>
4343
{% else %}
4444
<p>No shortcodes configured.</p>
45-
<p>See the <a href="https://github.com/webfactory/WebfactoryShortcodeBundle/blob/master/README.md">README.md of the WebfactoryShortcodeBundle</a> to configure some.</p>
45+
<p>See the <a href="https://github.com/webfactory/WebfactoryShortcodeBundle/blob/master/README.md">WebfactoryShortcodeBundle README.md</a> on how to configure this documentation.</p>
4646
{% endif %}
4747
{% endblock %}

0 commit comments

Comments
 (0)