form_admin_fields.html.twig 18 KB

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