Created
November 7, 2020 17:37
-
-
Save cesurapp/fb822e5ece331277026ca173e1884c3d to your computer and use it in GitHub Desktop.
Symfony 4.4+ Remove Multiple Choice Option
This file contains 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 | |
/** | |
* 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]; | |
} | |
} |
This file contains 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 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