<?php

/*
 * This file is part of the MopaBootstrapBundle.
 *
 * (c) Philipp A. Mohrenweiser <phiamo@googlemail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mopa\Bundle\BootstrapBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Add a new twig.form.resources
 *
 * @author Philipp A. Mohrenweiser <phiamo@googlemail.com>
 */
class FormPass implements CompilerPassInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (($template = $container->getParameter('mopa_bootstrap.form.templating')) !== false) {
            $resources = $container->getParameter('twig.form.resources');
            # Ensure it wasnt already aded via config
            if (!in_array($template, $resources)) {
                //Ensures Mopa comes in before other resources
                $coreResource = array_shift($resources);
                array_unshift($resources, $template);
                array_unshift($resources, $coreResource);

                $container->setParameter('twig.form.resources', $resources);
            }
        }
    }
}

<?php

/*
 * This file is part of the MopaBootstrapBundle.
 *
 * (c) Philipp A. Mohrenweiser <phiamo@googlemail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mopa\Bundle\BootstrapBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Add a new twig.form.resources
 *
 * @author Philipp A. Mohrenweiser <phiamo@googlemail.com>
 */
class FormPass implements CompilerPassInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (($template = $container->getParameter('mopa_bootstrap.form.templating')) !== false) {
            $resources = $container->getParameter('twig.form.resources');
            # Ensure it wasnt already aded via config
            if (!in_array($template, $resources)) {                
                // If form_div_layout.html.twig is found, insert Mopa right after
                if (($key = array_search('form_div_layout.html.twig', $resources)) !== false) {
                    array_splice($resources, ++$key, 0, $template);
                }
                // Else insert Mopa in first position
                else {
                    array_unshift($resources, $template);
                }

                $container->setParameter('twig.form.resources', $resources);
            }
        }
    }
}

Fix template insertion in twig.form.resources
Fix template insertion in twig.form.resources

We can't be sure that the core template will be in first position (see https://github.com/symfony-cmf/RoutingExtraBundle/pull/48/files#r2514138)

Now the core template is searched and if it exists, mopa's template is inserted right after.