b1034f3b0c74 — Eddie Barraco 5 years ago
Allow to filter by the showed article tags
M src/Form/TagLabelFilterType.php +7 -2
@@ 22,13 22,18 @@ class TagLabelFilterType extends Abstrac
             ])
             ->get('labels')
             ->addModelTransformer(new CallbackTransformer(
-                function ($tagsAsArray) {
-                    return null;
+                function ($labels) {
+                    if (is_array($labels)) {
+                        return implode(' ', $labels);
+                    }
+
+                    return $labels;
                 },
                 function ($tagsAsString) {
                     if (null === $tagsAsString) {
                         return [];
                     }
+
                     return explode(' ', $tagsAsString);
                 }
             ))

          
A => src/Twig/AppExtension.php +40 -0
@@ 0,0 1,40 @@ 
+<?php
+
+namespace App\Twig;
+
+use App\Form\TagLabelFilterType;
+use App\Entity\Article;
+use Twig\Extension\AbstractExtension;
+use Twig\TwigFilter;
+use Symfony\Component\Form\FormFactoryInterface;
+
+class AppExtension extends AbstractExtension
+{
+    private $formFactory;
+
+    public function __construct(FormFactoryInterface $formFactory)
+    {
+        $this->formFactory = $formFactory;
+    }
+
+    public function getFilters()
+    {
+        return [
+            new TwigFilter('tag_filter_from_article', [$this, 'formatTagFilterFromArticle']),
+        ];
+    }
+
+    public function formatTagFilterFromArticle(Article $article)
+    {
+        $labels = [];
+        foreach ($article->tags as $tag) {
+            $labels[] = $tag->label;
+        }
+
+        $form = $this->formFactory->create(TagLabelFilterType::class, [
+            'labels' => $labels
+        ]);
+
+        return $form->createView();
+    }
+}

          
M templates/article/archive.html.twig +3 -1
@@ 10,7 10,9 @@ 
                     <a href="{{ path('article_show', {'id': article.id}) }}">{{ article.title }}</a>
                 </span>
             </h2>
-            {{ article.teasing|raw }}
+            <div>
+                {{ article.teasing|raw }}
+            </div>
         </div>
         {% endfor %}
 

          
M templates/article/show.html.twig +1 -0
@@ 14,4 14,5 @@ 
 
 {% block sidebar %}
     <img src="{{ asset('img/icon.jpg') }}" class="icon"/>
+    {{ form(article|tag_filter_from_article, {'action': path('article_index')}) }}
 {% endblock %}