Search code examples
phpformssymfonyoptgroup

3 level optgroup with Symfony


I'm using Symfony and I would like to do a 3 Levels optgroup what I want is :
Category 1 (Disabled)
--ChildCategory 1.1
--ChildCategory 1.2 (Disabled)
----ChildChildCategory 1.2.1
Category 2

But all i got is :
Category 1 (Disabled)
--ChildCategory 1.1
ChildCategory 1.2 (Disabled)
--ChildChildCategory 1.2.1
Category 2

My HTML Code :

<select id="event_category" name="event[category]" class="form-control">
<optgroup label="Category 1">
    <option value="8">Category 1.1</option>
</optgroup>
<optgroup label="Category 1.2">
    <option value="7">Category 1.2.1</option>
</optgroup>
    <option value="3">Category 2</option>
</select>

I'm creation an array with this code to add it to my formType :

$arrayCategories = array();
        foreach ($categories as $category)
        {
            if (count($category->getChildren()) > 0)
            {
                $arrayCategories[$category->getTitle()] = array();

                foreach ($category->getChildren() as $child)
                {
                    if (count($child->getChildren()) > 0)
                    {
                        $arrayCategories[$category->getTitle()][$child->getTitle()] = array();

                        foreach ($child->getChildren() as $child2)
                        {
                            $arrayCategories[$category->getTitle()][$child->getTitle()][$child2->getTitle().' - '. $child2->getLangLabel()] = $child2->getId();
                        }
                    }
                    else
                    {
                        $arrayCategories[$category->getTitle()][$child->getTitle().' - '. $child->getLangLabel()] = $child->getId();
                    }
                }
            }
            else
            {
                $arrayCategories[$category->getTitle().' - '. $category->getLangLabel()] = $category->getId();
            }
        }

Thanks for you help !


Solution

  • I found a solution ! findAll categories and render them to my twig page, and to display them :

    <select id="event_category" class="form-control">
         {% for category in categories %}
              {% if category.children|length == 0 %}
    
                  <option value="{{ category.id }}">{{ category.title }} - {{ category.getLangLabel() }}</option>
              {% else %}
                  <option value="{{ category.id }}" disabled>{{ category.title }} - {{ category.getLangLabel() }}</option>
                   {% for child in category.children %}
    
                       {% if child.children|length == 0 %}
                         <option value="{{ child.id }}">&nbsp;&nbsp;&nbsp;&nbsp;{{ child.title }} - {{ child.getLangLabel() }}</option>
                                            {% else %}
                                                <option value="{{ child.id }}" disabled>&nbsp;&nbsp;&nbsp;&nbsp;{{ child.title }} - {{ child.getLangLabel() }}</option>
                                                {% for secondChild in child.children %}
                                                    <option value="{{ secondChild.id }}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ secondChild.title }} - {{ secondChild.getLangLabel() }}</option>
                                                {% endfor %}
                                            {% endif %}
                                        {% endfor %}
                                    {% endif %}
                                {% endfor %}
    

    Thanks for help ! :)