form_admin_fields.html.twig 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. {% set wrapper_class = "" %}
  132. {% else %}
  133. {% set label_class = "control-label" %}
  134. {% set wrapper_class = "form-group" %}
  135. {% endif%}
  136. {% if sonata_admin is not defined or not sonata_admin_enabled or not sonata_admin.field_description %}
  137. <div class="{{ wrapper_class }}{% if errors|length > 0%} has-error{% endif %}">
  138. {{ form_label(form, label|default(null)) }}
  139. <div class="{% if label is sameas(false) %}sonata-collection-row-without-label{% endif %}">
  140. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  141. {% if errors|length > 0 %}
  142. <div class="help-block sonata-ba-field-error-messages">
  143. {{ form_errors(form) }}
  144. </div>
  145. {% endif %}
  146. </div>
  147. </div>
  148. {% else %}
  149. <div class="form-group{% if errors|length > 0%} has-error{%endif%}" id="sonata-ba-field-container-{{ id }}">
  150. {% block label %}
  151. {% if sonata_admin.field_description.options.name is defined %}
  152. {{ form_label(form, sonata_admin.field_description.options.name, { 'attr' : {'class' : label_class} }) }}
  153. {% else %}
  154. {{ form_label(form, label|default(null), { 'attr' : {'class' : label_class} }) }}
  155. {% endif %}
  156. {% endblock %}
  157. {% set has_label = sonata_admin.field_description.options.name is defined or label is not sameas(false) %}
  158. <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 %}">
  159. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  160. {% if errors|length > 0 %}
  161. <div class="help-block sonata-ba-field-error-messages">
  162. {{ form_errors(form) }}
  163. </div>
  164. {% endif %}
  165. {% if sonata_admin.field_description.help %}
  166. <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
  167. {% endif %}
  168. </div>
  169. </div>
  170. {% endif %}
  171. {% endblock form_row %}
  172. {% block sonata_type_native_collection_widget_row %}
  173. {% spaceless %}
  174. <div class="sonata-collection-row">
  175. {% if allow_delete %}
  176. <a href="#" class="btn sonata-collection-delete"><i class="fa fa-minus-circle"></i></a>
  177. {% endif %}
  178. {{ form_row(child) }}
  179. </div>
  180. {% endspaceless %}
  181. {% endblock sonata_type_native_collection_widget_row %}
  182. {% block sonata_type_native_collection_widget %}
  183. {% spaceless %}
  184. {% if prototype is defined %}
  185. {% set child = prototype %}
  186. {% set attr = attr|merge({'data-prototype': block('sonata_type_native_collection_widget_row'), 'data-prototype-name': prototype.vars.name, 'class': attr.class|default('') }) %}
  187. {% endif %}
  188. <div {{ block('widget_container_attributes') }}>
  189. {{ form_errors(form) }}
  190. {% for child in form %}
  191. {{ block('sonata_type_native_collection_widget_row') }}
  192. {% endfor %}
  193. {{ form_rest(form) }}
  194. {% if allow_add %}
  195. <div><a href="#" class="btn sonata-collection-add"><i class="fa fa-plus-circle"></i></a></div>
  196. {% endif %}
  197. </div>
  198. {% endspaceless %}
  199. {% endblock sonata_type_native_collection_widget %}
  200. {% block sonata_type_immutable_array_widget %}
  201. {% spaceless %}
  202. <div {{ block('widget_container_attributes') }}>
  203. {{ form_errors(form) }}
  204. {% for key, child in form %}
  205. {{ block('sonata_type_immutable_array_widget_row') }}
  206. {% endfor %}
  207. {{ form_rest(form) }}
  208. </div>
  209. {% endspaceless %}
  210. {% endblock sonata_type_immutable_array_widget %}
  211. {% block sonata_type_immutable_array_widget_row %}
  212. {% spaceless %}
  213. <div class="form-group{% if errors|length > 0%} error{%endif%}" id="sonata-ba-field-container-{{ id }}-{{ key }}">
  214. {{ form_label(child) }}
  215. {% set div_class = "" %}
  216. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  217. {% set div_class = "col-sm-9 col-md-9" %}
  218. {% endif%}
  219. <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 %}">
  220. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  221. </div>
  222. {% if errors|length > 0 %}
  223. <div class="help-block sonata-ba-field-error-messages">
  224. {{ form_errors(child) }}
  225. </div>
  226. {% endif %}
  227. </div>
  228. {% endspaceless %}
  229. {% endblock %}
  230. {% block sonata_type_model_autocomplete_widget %}
  231. {% spaceless %}
  232. {{ form_widget(form.title) }}
  233. {% for child in form %}
  234. {% if not child.rendered %}
  235. {{ form_widget(child) }}
  236. {% endif %}
  237. {% endfor %}
  238. <script>
  239. (function ($) {
  240. var autocompleteInput = $("#{{ form.title.vars.id }}");
  241. autocompleteInput.select2({
  242. placeholder: "{{ placeholder }}",
  243. allowClear: {{ required ? 'false' : 'true' }},
  244. enable: {{ disabled ? 'false' : 'true' }},
  245. readonly: {{ read_only ? 'true' : 'false' }},
  246. minimumInputLength: {{ minimum_input_length }},
  247. multiple: {{ multiple ? 'true' : 'false' }},
  248. ajax: {
  249. url: "{{ url ?: url(route.name, route.parameters|default([])) }}",
  250. dataType: 'json',
  251. quietMillis: 100,
  252. data: function (term, page) { // page is the one-based page number tracked by Select2
  253. return {
  254. //search term
  255. "{{ req_param_name_search }}": term,
  256. // page size
  257. "{{ req_param_name_items_per_page }}": {{ items_per_page }},
  258. // page number
  259. "{{ req_param_name_page_number }}": page,
  260. // admin
  261. 'uniqid': "{{ sonata_admin.admin.uniqid }}",
  262. 'code': "{{ sonata_admin.admin.code }}",
  263. 'field': "{{ name }}"
  264. // other parameters
  265. {% if req_params is not empty %},
  266. {%- for key, value in req_params -%}
  267. "{{- key|e('js') -}}": "{{- value|e('js') -}}"
  268. {%- if not loop.last -%}, {% endif -%}
  269. {%- endfor -%}
  270. {% endif %}
  271. };
  272. },
  273. results: function (data, page) {
  274. // notice we return the value of more so Select2 knows if more results can be loaded
  275. return {results: data.items, more: data.more};
  276. }
  277. },
  278. formatResult: function (item) {
  279. return {% block sonata_type_model_autocomplete_dropdown_item_format %}'<div class="sonata-autocomplete-dropdown-item">'+item.label+'</div>'{% endblock %};// format of one dropdown item
  280. },
  281. formatSelection: function (item) {
  282. return {% block sonata_type_model_autocomplete_selection_format %}item.label{% endblock %};// format selected item '<b>'+item.label+'</b>';
  283. },
  284. dropdownCssClass: "{{ dropdown_css_class }}",
  285. escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
  286. });
  287. autocompleteInput.on("change", function(e) {
  288. // console.log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));
  289. // add new input
  290. var el = null;
  291. if (undefined !== e.added) {
  292. var addedItems = e.added;
  293. if(!$.isArray(addedItems)) {
  294. addedItems = [addedItems];
  295. }
  296. var length = addedItems.length;
  297. for (var i = 0; i < length; i++) {
  298. el = addedItems[i];
  299. $("#{{ form.identifiers.vars.id }}").append('<input type="hidden" name="{{ form.identifiers.vars.full_name }}[]" value="'+el.id+'" />');
  300. }
  301. }
  302. // remove input
  303. if (undefined !== e.removed && null !== e.removed) {
  304. var removedItems = e.removed;
  305. if(!$.isArray(removedItems)) {
  306. removedItems = [removedItems];
  307. }
  308. var length = removedItems.length;
  309. for (var i = 0; i < length; i++) {
  310. el = removedItems[i];
  311. $('#{{ form.identifiers.vars.id }} input:hidden[value="'+el.id+'"]').remove();
  312. }
  313. }
  314. });
  315. // Initialise the autocomplete
  316. var data = [];
  317. {% if multiple -%}
  318. data = [
  319. {%- for key, label_text in value.labels -%}
  320. {id: '{{ value.identifiers[key]|e('js') }}', label:'{{ label_text|e('js') }}'}
  321. {%- if not loop.last -%}, {% endif -%}
  322. {%- endfor -%}
  323. ];
  324. {%- elseif value.labels[0] is defined -%}
  325. data = {id: '{{ value.identifiers[0]|e('js') }}', label:'{{ value.labels[0]|e('js') }}'};
  326. {%- endif %}
  327. if (undefined==data.length || 0<data.length) { // Leave placeholder if no data set
  328. autocompleteInput.select2('data', data);
  329. }
  330. })(jQuery);
  331. </script>
  332. {% endspaceless %}
  333. {% endblock sonata_type_model_autocomplete_widget %}