chill-bundles/Resources/views/Form/fields.html.twig

97 lines
3.4 KiB
Twig

{#
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{# CustomFields Title #}
{% block custom_field_title_widget %}
<span class="cf-{{ form.vars.attr.type }}">
{{ form.vars.attr.title }}
</span>
{% endblock custom_field_title_widget %}
{# CustomFields Choice #}
{# render an alement in a choice list #}
{% block cf_choices_list_widget %}
{{ form_row(form.name) }}
{{ form_row(form.active) }}
{{ form_row(form.slug) }}
{% endblock cf_choices_list_widget %}
{# CFChoice : render the different elements in a choice list #}
{% block cf_choices_row %}
<h3>{{ 'Choices'|trans }}</h3>
<div id="{{ form.vars.id }}" data-prototype="{{- form_row(form.vars.prototype.children.name)
~ form_row(form.vars.prototype.children.active)
~ form_row(form.vars.prototype.children.slug) -}}">
<table><tbody>
{% for choice in form %}
<tr><td>
{{ form_row(choice.name) }}
{{ form_row(choice.active) }}
{{ form_row(choice.slug) }}
</td></tr>
{% endfor %}
</tbody></table>
</div>
{# we use javascrit to add an additional element. All functions are personnalized with the id ( = form.vars.id) #}
<script type="text/javascript">
function addElementInDiv(div_id) {
var div = $('#' + div_id);
var prototype = div.data('prototype');
var index = div.data('index');
var add_element_link = $('#' + div_id + '_add_element_link');
var new_fields = prototype.replace(/__name__label__/g, index);
var new_fields = prototype.replace(/__name__/g, index);
div.data('index', index + 1);
console.log(index);
add_element_link.before(new_fields);
}
function initializeCFChoiceOptionsChoices(div_id) {
var add_element_link = $('<a id="' + div_id + '_add_element_link"" href="#" class="sc-button bt-submit">{{ 'Add an element'|trans }}</a>');
var div = $('#' + div_id);
div.append(add_element_link);
div.data('index', div.find(':input').length / 5);
add_element_link.on('click', function (e) {
e.preventDefault();
addElementInDiv(div_id);
});
}
jQuery(document).ready(initializeCFChoiceOptionsChoices('{{ form.vars.id }}'));
</script>
{% endblock cf_choices_row %}
{% block choice_with_other_widget %}
<div {{ block('widget_container_attributes') }}>
{%- for child in form.children._choices %}
{{- form_widget(child) -}}
{{- form_label(child) -}}
{%- if child.vars.value == '_other' -%}
{{- form_widget(form.children._other) -}}
{%- endif -%}
{% endfor -%}
</div>
{% endblock choice_with_other_widget %}