Skip to content

Instantly share code, notes, and snippets.

@cesurapp
Created November 7, 2020 17:37
Show Gist options
  • Save cesurapp/fb822e5ece331277026ca173e1884c3d to your computer and use it in GitHub Desktop.
Save cesurapp/fb822e5ece331277026ca173e1884c3d to your computer and use it in GitHub Desktop.
Symfony 4.4+ Remove Multiple Choice Option
<?php
/**
* ChoiceType "preferred_choice_twice" Extension
*
* @author Ramazan APAYDIN <[email protected]>
*/
class ChoiceTypeExtension extends AbstractTypeExtension
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);
if (is_array($view->vars['preferred_choices'])) {
foreach ($view->vars['preferred_choices'] as $index => $choices) {
unset($view->vars['choices'][$index]);
}
}
}
public static function getExtendedTypes(): iterable
{
return [ChoiceType::class];
}
}
{%- block choice_widget_collapsed -%}
{%- if required and placeholder is none and not placeholder_in_choices and not multiple and (attr.size is not defined or attr.size <= 1) -%}
{% set required = false %}
{%- endif -%}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{%- if placeholder is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
{%- endif -%}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
{% set render_preferred_choices = false %}
{{- block('choice_widget_options') -}}
{%- if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- endif -%}
{%- endif -%}
{%- set options = choices -%}
{%- set render_preferred_choices = false -%}
{{- block('choice_widget_options') -}}
</select>
{%- endblock choice_widget_collapsed -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment