Created
May 15, 2015 12:46
-
-
Save DavidBadura/94e8636f8fec5d9a2a03 to your computer and use it in GitHub Desktop.
Symfony2 Datalist Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form\Extension; | |
use Symfony\Component\Form\AbstractTypeExtension; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormView; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/** | |
* @author David Badura <[email protected]> | |
*/ | |
class DataListExtension extends AbstractTypeExtension | |
{ | |
/** | |
* @param FormView $view | |
* @param FormInterface $form | |
* @param array $options | |
*/ | |
public function buildView(FormView $view, FormInterface $form, array $options) | |
{ | |
if (!$options['datalist']) { | |
return; | |
} | |
$dataListId = $view->vars['id'] . '_datalist'; | |
$view->vars['attr']['list'] = $dataListId; | |
$view->vars['datalist_id'] = $dataListId; | |
$view->vars['datalist'] = $options['datalist']; | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(['datalist' => []]); | |
} | |
/** | |
* Returns the name of the type being extended. | |
* | |
* @return string The name of the type being extended | |
*/ | |
public function getExtendedType() | |
{ | |
return 'text'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% block form_widget_simple -%} | |
{{ parent() }} | |
{% if datalist is defined %} | |
<datalist id="{{ datalist_id }}"> | |
{% for item in datalist %} | |
<option value="{{ item }}"> | |
{% endfor %} | |
</datalist> | |
{% endif %} | |
{%- endblock %} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<service id="form.typ_extension.data_list" class="AppBundle\Form\Extension\DataListExtension"> | |
<tag name="form.type_extension" alias="text"/> | |
</service> | |
</services> | |
</container> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update for Symfony 4.4:
And the configuration in
services.xml
(orservices.yaml
) is not necessary any more.