form_admin_fields.html.twig 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. {#
  2. This file is part of the Sonata package.
  3. (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  4. For the full copyright and license information, please view the LICENSE
  5. file that was distributed with this source code.
  6. #}
  7. {% extends 'form_div_layout.html.twig' %}
  8. {% block form_widget -%}
  9. {{ parent() }}
  10. {% if sonata_help is defined and sonata_help %}
  11. <span class="help-block sonata-ba-field-widget-help">{{ sonata_help|raw }}</span>
  12. {% endif %}
  13. {%- endblock form_widget %}
  14. {% block form_widget_simple %}
  15. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  16. {{ parent() }}
  17. {% endblock form_widget_simple %}
  18. {% block textarea_widget %}
  19. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  20. {{ parent() }}
  21. {% endblock textarea_widget %}
  22. {# Labels #}
  23. {% block form_label %}
  24. {% spaceless %}
  25. {% set label_class = "" %}
  26. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  27. {% set label_class = " control-label col-sm-3" %}
  28. {% else %}
  29. {% set label_class = " control-label" %}
  30. {% endif%}
  31. {#{{ sonata_admin.admin.getConfigurationPool().getOption('form_type') }}#}
  32. {% if label is not sameas(false) %}
  33. {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ label_class }) %}
  34. {% if not compound %}
  35. {% set label_attr = label_attr|merge({'for': id}) %}
  36. {% endif %}
  37. {% if required %}
  38. {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
  39. {% endif %}
  40. {% if label is empty %}
  41. {% set label = name|humanize %}
  42. {% endif %}
  43. {% if in_list_checkbox is defined and in_list_checkbox and widget is defined %}
  44. <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  45. {{ widget|raw }}
  46. <span>
  47. {% if not sonata_admin.admin %}
  48. {{- label|trans({}, translation_domain) -}}
  49. {% else %}
  50. {{- label|trans({}, sonata_admin.field_description.translationDomain) -}}
  51. {% endif%}
  52. </span>
  53. </label>
  54. {% else %}
  55. <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
  56. {% if not sonata_admin.admin%}
  57. {{- label|trans({}, translation_domain) -}}
  58. {% else %}
  59. {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
  60. {% endif %}
  61. </label>
  62. {% endif %}
  63. {% endif %}
  64. {% endspaceless %}
  65. {% endblock form_label %}
  66. {% block widget_container_attributes_choice_widget %}
  67. {% spaceless %}
  68. id="{{ id }}"
  69. {% for attrname,attrvalue in attr %}{{attrname}}="{% if attrname == 'class' %}list-unstyled {% endif%}{{attrvalue}}" {% endfor %}
  70. {% if "class" not in attr %}class="list-unstyled"{%endif %}
  71. {% endspaceless %}
  72. {% endblock %}
  73. {% block choice_widget_expanded %}
  74. {% spaceless %}
  75. <ul {{ block('widget_container_attributes') }}>
  76. {% for child in form %}
  77. <li>
  78. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  79. {{ form_label(child) }}
  80. </li>
  81. {% endfor %}
  82. </ul>
  83. {% endspaceless %}
  84. {% endblock choice_widget_expanded %}
  85. {% block choice_widget %}
  86. {% spaceless %}
  87. {% if compound %}
  88. <ul {{ block('widget_container_attributes_choice_widget') }}>
  89. {% for child in form %}
  90. <li>
  91. {% set form_widget_content %}
  92. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  93. {% endset %}
  94. {{ form_label(child, child.vars.label|default(null), { 'in_list_checkbox' : true, 'widget' : form_widget_content } ) }}
  95. </li>
  96. {% endfor %}
  97. </ul>
  98. {% else %}
  99. {% if sonata_admin.admin and not sonata_admin.admin.getConfigurationPool().getOption('use_select2') %}
  100. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  101. {% endif %}
  102. <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
  103. {% if empty_value is not none %}
  104. <option value="">
  105. {% if not sonata_admin.admin %}
  106. {{- empty_value|trans({}, translation_domain) -}}
  107. {% else %}
  108. {{- empty_value|trans({}, sonata_admin.field_description.translationDomain) -}}
  109. {% endif%}
  110. </option>
  111. {% endif %}
  112. {% if preferred_choices|length > 0 %}
  113. {% set options = preferred_choices %}
  114. {{ block('choice_widget_options') }}
  115. {% if choices|length > 0 %}
  116. <option disabled="disabled">{{ separator }}</option>
  117. {% endif %}
  118. {% endif %}
  119. {% set options = choices %}
  120. {{ block('choice_widget_options') }}
  121. </select>
  122. {% endif %}
  123. {% endspaceless %}
  124. {% endblock choice_widget %}
  125. {% block form_row %}
  126. {% set label_class = "" %}
  127. {% set div_class = "" %}
  128. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  129. {% set label_class = "control-label col-sm-3" %}
  130. {% set div_class = "col-sm-9 col-md-9" %}
  131. {% else %}
  132. {% set label_class = "control-label" %}
  133. {% endif%}
  134. {% if sonata_admin is not defined or not sonata_admin_enabled or not sonata_admin.field_description %}
  135. <div class="form-group {% if errors|length > 0%} has-error{% endif %}">
  136. {{ form_label(form, label|default(null)) }}
  137. <div class="{% if label is sameas(false) %}sonata-collection-row-without-label{% endif %}">
  138. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  139. {% if errors|length > 0 %}
  140. <div class="help-block sonata-ba-field-error-messages">
  141. {{ form_errors(form) }}
  142. </div>
  143. {% endif %}
  144. </div>
  145. </div>
  146. {% else %}
  147. <div class="form-group{% if errors|length > 0%} has-error{%endif%}" id="sonata-ba-field-container-{{ id }}">
  148. {% block label %}
  149. {% if sonata_admin.field_description.options.name is defined %}
  150. {{ form_label(form, sonata_admin.field_description.options.name, { 'attr' : {'class' : label_class} }) }}
  151. {% else %}
  152. {{ form_label(form, label|default(null), { 'attr' : {'class' : label_class} }) }}
  153. {% endif %}
  154. {% endblock %}
  155. {% set has_label = sonata_admin.field_description.options.name is defined or label is not sameas(false) %}
  156. <div class="{{ div_class }} sonata-ba-field sonata-ba-field-{{ sonata_admin.edit }}-{{ sonata_admin.inline }} {% if errors|length > 0 %}sonata-ba-field-error{% endif %} {% if not has_label %}sonata-collection-row-without-label{% endif %}">
  157. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  158. {% if errors|length > 0 %}
  159. <div class="help-block sonata-ba-field-error-messages">
  160. {{ form_errors(form) }}
  161. </div>
  162. {% endif %}
  163. {% if sonata_admin.field_description.help %}
  164. <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
  165. {% endif %}
  166. </div>
  167. </div>
  168. {% endif %}
  169. {% endblock form_row %}
  170. {% block sonata_type_native_collection_widget_row %}
  171. {% spaceless %}
  172. <div class="sonata-collection-row">
  173. {% if allow_delete %}
  174. <a href="#" class="btn sonata-collection-delete"><i class="fa fa-minus-circle"></i></a>
  175. {% endif %}
  176. {{ form_row(child) }}
  177. </div>
  178. {% endspaceless %}
  179. {% endblock sonata_type_native_collection_widget_row %}
  180. {% block sonata_type_native_collection_widget %}
  181. {% spaceless %}
  182. {% if prototype is defined %}
  183. {% set child = prototype %}
  184. {% set attr = attr|merge({'data-prototype': block('sonata_type_native_collection_widget_row'), 'data-prototype-name': prototype.vars.name, 'class': attr.class|default('') }) %}
  185. {% endif %}
  186. <div {{ block('widget_container_attributes') }}>
  187. {{ form_errors(form) }}
  188. {% for child in form %}
  189. {{ block('sonata_type_native_collection_widget_row') }}
  190. {% endfor %}
  191. {{ form_rest(form) }}
  192. {% if allow_add %}
  193. <div><a href="#" class="btn sonata-collection-add"><i class="fa fa-plus-circle"></i></a></div>
  194. {% endif %}
  195. </div>
  196. {% endspaceless %}
  197. {% endblock sonata_type_native_collection_widget %}
  198. {% block sonata_type_immutable_array_widget %}
  199. {% spaceless %}
  200. <div {{ block('widget_container_attributes') }}>
  201. {{ form_errors(form) }}
  202. {% for key, child in form %}
  203. {{ block('sonata_type_immutable_array_widget_row') }}
  204. {% endfor %}
  205. {{ form_rest(form) }}
  206. </div>
  207. {% endspaceless %}
  208. {% endblock sonata_type_immutable_array_widget %}
  209. {% block sonata_type_immutable_array_widget_row %}
  210. {% spaceless %}
  211. <div class="form-group{% if errors|length > 0%} error{%endif%}" id="sonata-ba-field-container-{{ id }}-{{ key }}">
  212. {{ form_label(child) }}
  213. {% set div_class = "" %}
  214. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  215. {% set div_class = "col-sm-9 col-md-9" %}
  216. {% endif%}
  217. <div class="{{ div_class }} sonata-ba-field sonata-ba-field-{{ sonata_admin.edit }}-{{ sonata_admin.inline }} {% if errors|length > 0 %}sonata-ba-field-error{% endif %}">
  218. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  219. </div>
  220. {% if errors|length > 0 %}
  221. <div class="help-block sonata-ba-field-error-messages">
  222. {{ form_errors(child) }}
  223. </div>
  224. {% endif %}
  225. </div>
  226. {% endspaceless %}
  227. {% endblock %}
  228. {% block sonata_type_model_autocomplete_widget %}
  229. {% spaceless %}
  230. {{ form_widget(form.title) }}
  231. {% for child in form %}
  232. {% if not child.rendered %}
  233. {{ form_widget(child) }}
  234. {% endif %}
  235. {% endfor %}
  236. <script>
  237. (function ($) {
  238. var autocompleteInput = $("#{{ form.title.vars.id }}");
  239. autocompleteInput.select2({
  240. placeholder: "{{ placeholder }}",
  241. allowClear: {{ required ? 'false' : 'true' }},
  242. enable: {{ disabled ? 'false' : 'true' }},
  243. readonly: {{ read_only ? 'true' : 'false' }},
  244. minimumInputLength: {{ minimum_input_length }},
  245. multiple: {{ multiple ? 'true' : 'false' }},
  246. ajax: {
  247. url: "{{ url ?: url(route.name, route.parameters|default([])) }}",
  248. dataType: 'json',
  249. quietMillis: 100,
  250. data: function (term, page) { // page is the one-based page number tracked by Select2
  251. return {
  252. //search term
  253. "{{ req_param_name_search }}": term,
  254. // page size
  255. "{{ req_param_name_items_per_page }}": {{ items_per_page }},
  256. // page number
  257. "{{ req_param_name_page_number }}": page,
  258. // admin
  259. 'uniqid': "{{ sonata_admin.admin.uniqid }}",
  260. 'code': "{{ sonata_admin.admin.code }}",
  261. 'field': "{{ name }}"
  262. // other parameters
  263. {% if req_params is not empty %},
  264. {%- for key, value in req_params -%}
  265. "{{- key|e('js') -}}": "{{- value|e('js') -}}"
  266. {%- if not loop.last -%}, {% endif -%}
  267. {%- endfor -%}
  268. {% endif %}
  269. };
  270. },
  271. results: function (data, page) {
  272. // notice we return the value of more so Select2 knows if more results can be loaded
  273. return {results: data.items, more: data.more};
  274. }
  275. },
  276. formatResult: function (item) {
  277. return {% block sonata_type_model_autocomplete_dropdown_item_format %}'<div class="sonata-autocomplete-dropdown-item">'+item.label+'</div>'{% endblock %};// format of one dropdown item
  278. },
  279. formatSelection: function (item) {
  280. return {% block sonata_type_model_autocomplete_selection_format %}item.label{% endblock %};// format selected item '<b>'+item.label+'</b>';
  281. },
  282. dropdownCssClass: "{{ dropdown_css_class }}",
  283. escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
  284. });
  285. autocompleteInput.on("change", function(e) {
  286. // console.log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));
  287. // add new input
  288. var el = null;
  289. if (undefined !== e.added) {
  290. var addedItems = e.added;
  291. if(!$.isArray(addedItems)) {
  292. addedItems = [addedItems];
  293. }
  294. var length = addedItems.length;
  295. for (var i = 0; i < length; i++) {
  296. el = addedItems[i];
  297. $("#{{ form.identifiers.vars.id }}").append('<input type="hidden" name="{{ form.identifiers.vars.full_name }}[]" value="'+el.id+'" />');
  298. }
  299. }
  300. // remove input
  301. if (undefined !== e.removed && null !== e.removed) {
  302. var removedItems = e.removed;
  303. if(!$.isArray(removedItems)) {
  304. removedItems = [removedItems];
  305. }
  306. var length = removedItems.length;
  307. for (var i = 0; i < length; i++) {
  308. el = removedItems[i];
  309. $('#{{ form.identifiers.vars.id }} input:hidden[value="'+el.id+'"]').remove();
  310. }
  311. }
  312. });
  313. // Initialise the autocomplete
  314. var data = [];
  315. {% if multiple -%}
  316. data = [
  317. {%- for key, label_text in value.labels -%}
  318. {id: '{{ value.identifiers[key]|e('js') }}', label:'{{ label_text|e('js') }}'}
  319. {%- if not loop.last -%}, {% endif -%}
  320. {%- endfor -%}
  321. ];
  322. {%- elseif value.labels[0] is defined -%}
  323. data = {id: '{{ value.identifiers[0]|e('js') }}', label:'{{ value.labels[0]|e('js') }}'};
  324. {%- endif %}
  325. if (undefined==data.length || 0<data.length) { // Leave placeholder if no data set
  326. autocompleteInput.select2('data', data);
  327. }
  328. })(jQuery);
  329. </script>
  330. {% endspaceless %}
  331. {% endblock sonata_type_model_autocomplete_widget %}