From 9f70debc4dfbd48277b0a374cbc1ff952dd12ae5 Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Wed, 21 Apr 2021 14:40:22 +0300 Subject: [PATCH 1/7] Symfony 5 compatibility --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 439baf6..ab5c5eb 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -18,8 +18,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('lifo_typeahead'); + $treeBuilder = new TreeBuilder('lifo_typeahead'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() From d5a2f6d89ddefb3f4fefb895e86348a1a0fe0d3c Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Wed, 21 Apr 2021 15:05:28 +0300 Subject: [PATCH 2/7] Update twig extension to be compatible with Symfony 5 and new Twig versions --- Twig/Extension/TypeaheadTwigExtension.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Twig/Extension/TypeaheadTwigExtension.php b/Twig/Extension/TypeaheadTwigExtension.php index 85b372d..b593f1a 100644 --- a/Twig/Extension/TypeaheadTwigExtension.php +++ b/Twig/Extension/TypeaheadTwigExtension.php @@ -2,12 +2,11 @@ namespace Lifo\TypeaheadBundle\Twig\Extension; -use Assetic\Asset\AssetCollection; -use Assetic\Asset\StringAsset; -use Assetic\Filter\Yui\JsCompressorFilter; use Lifo\TypeaheadBundle\Form\Type\TypeaheadType; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; -class TypeaheadTwigExtension extends \Twig_Extension +class TypeaheadTwigExtension extends AbstractExtension { public function getName() { @@ -17,7 +16,7 @@ public function getName() public function getFunctions() { return array( - new \Twig_SimpleFunction('lifo_typeahead_init', array($this, 'initTypeaheadFunction'), array('needs_environment' => true, 'is_safe' => array('html'))), + new TwigFunction('lifo_typeahead_init', array($this, 'initTypeaheadFunction'), array('needs_environment' => true, 'is_safe' => array('html'))), ); } From 5361f470c89bc387b6b76b2efb331302728be6e5 Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Wed, 21 Apr 2021 17:09:05 +0300 Subject: [PATCH 3/7] Adjusting the configuration to support Symfony 5 and to keep backward compatibility --- DependencyInjection/Configuration.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ab5c5eb..8b4eac6 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -18,8 +18,13 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder('lifo_typeahead'); - $rootNode = $treeBuilder->getRootNode(); + if (Kernel::VERSION_ID < 40000) { + $treeBuilder = new TreeBuilder(); + $rootNode = $treeBuilder->root('lifo_typeahead'); + } else { + $treeBuilder = new TreeBuilder('lifo_typeahead'); + $rootNode = $treeBuilder->getRootNode(); + } $rootNode ->children() From 819cb30bb1eec4a927f96afc068583e1e986d4ec Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Wed, 21 Apr 2021 17:11:49 +0300 Subject: [PATCH 4/7] Correctly fix the last commit with using an use statement... --- DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8b4eac6..8394647 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -4,6 +4,7 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; +use Symfony\Component\HttpKernel\Kernel; class Configuration implements ConfigurationInterface { From 566520874813e62bc71416dcab9ac5c97407762b Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Thu, 22 Apr 2021 11:13:37 +0300 Subject: [PATCH 5/7] Make sure it loads the correct namespaced template file in Symfony 4 and up. --- DependencyInjection/LifoTypeaheadExtension.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/LifoTypeaheadExtension.php b/DependencyInjection/LifoTypeaheadExtension.php index 6caecdf..d60b919 100644 --- a/DependencyInjection/LifoTypeaheadExtension.php +++ b/DependencyInjection/LifoTypeaheadExtension.php @@ -83,7 +83,10 @@ protected function configureTwigBundle(ContainerBuilder $container, array $confi { if ($container->hasExtension('twig')) { $resources = array('LifoTypeaheadBundle:Form:typeahead.html.twig'); - if (Kernel::VERSION_ID >= '20600') { + if (Kernel::VERSION_ID >= 40000) { + $resources = array('@LifoTypeaheadBundle/Form/typeahead.html.twig'); + $container->prependExtensionConfig('twig', array('form_themes' => $resources)); + } elseif (Kernel::VERSION_ID >= 20600) { $container->prependExtensionConfig('twig', array('form_themes' => $resources)); } else { $container->prependExtensionConfig('twig', array('form' => array('resources' => $resources))); From 4e6b909d0241444cdb8345190a67422112e1e3e7 Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Thu, 22 Apr 2021 11:15:20 +0300 Subject: [PATCH 6/7] Correct the resource file to load in symfony 4 and later. --- DependencyInjection/LifoTypeaheadExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/LifoTypeaheadExtension.php b/DependencyInjection/LifoTypeaheadExtension.php index d60b919..b6f0071 100644 --- a/DependencyInjection/LifoTypeaheadExtension.php +++ b/DependencyInjection/LifoTypeaheadExtension.php @@ -84,7 +84,7 @@ protected function configureTwigBundle(ContainerBuilder $container, array $confi if ($container->hasExtension('twig')) { $resources = array('LifoTypeaheadBundle:Form:typeahead.html.twig'); if (Kernel::VERSION_ID >= 40000) { - $resources = array('@LifoTypeaheadBundle/Form/typeahead.html.twig'); + $resources = array('@LifoTypeahead/Form/typeahead.html.twig'); $container->prependExtensionConfig('twig', array('form_themes' => $resources)); } elseif (Kernel::VERSION_ID >= 20600) { $container->prependExtensionConfig('twig', array('form_themes' => $resources)); From fae3a987e265054b31521c41a9832efc00072c35 Mon Sep 17 00:00:00 2001 From: Tom Valk Date: Thu, 22 Apr 2021 11:35:22 +0300 Subject: [PATCH 7/7] Update form template to support new twig syntax (but with backward compatibility) Unfortunately losing the cleanness but I had to remove the nested for .. if for compatibility issues, and the 'spaceless' filters as it's now applied differently which is not backward compatible. --- Resources/views/Form/typeahead.html.twig | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Resources/views/Form/typeahead.html.twig b/Resources/views/Form/typeahead.html.twig index 0523927..e59939d 100644 --- a/Resources/views/Form/typeahead.html.twig +++ b/Resources/views/Form/typeahead.html.twig @@ -1,5 +1,4 @@ {% block entity_typeahead_widget %} -{% spaceless %} {% set main_full_name, main_id, main_value, main_attr = full_name, id, value, attr %} {# create visible autocomplete input #} @@ -41,21 +40,19 @@ {% set id, full_name = main_id, main_full_name %} {{ block('entity_typeahead_multiple_widget') }} {% endif %} -{% endspaceless %} {% endblock %} {% block entity_typeahead_multiple_widget %} -{% spaceless %}
    - {% for child in form.vars.data if child is not null %} - {{ block('entity_typeahead_list_widget') }} + {% for child in form.vars.data %} + {% if child is not null %} + {{ block('entity_typeahead_list_widget') }} + {% endif %} {% endfor %}
-{% endspaceless %} {% endblock %} {% block entity_typeahead_list_widget %} -{% spaceless %} {% if simple %} {% set _id, _value, _render = child, child, child %} {% else %} @@ -65,5 +62,4 @@ {{ child is not null ? _render : '' }} -{% endspaceless %} {% endblock %}