Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. public function buildForm(FormBuilderInterface $build, array $options)
  2. {
  3. $build->add('ID', 'hidden', [
  4. 'required' => false
  5. ]);
  6.  
  7. parent::buildForm($build, $options);
  8.  
  9. $build->add('poiImages', 'collection', [
  10. 'label' => false,
  11. 'type' => new ImageType(),
  12. 'allow_add' => true,
  13. 'allow_delete' => true
  14. ]);
  15. }
  16.  
  17. public function editAction($id, Application $app, Request $r)
  18. {
  19. $row = $app['main']->getById($id);
  20. $imagesRow = $app['main_media']->getByParentId($id, 'image');
  21. $poiRow = $app['main_poi']->getByParentId($id);
  22.  
  23. $collection = array_merge($row, [
  24. 'images' => $imagesRow,
  25. 'pois' => $poiRow,
  26. ]);
  27.  
  28. $form = $app['form.factory']->create(new MainEditForm(), $collection);
  29. $form->handleRequest($r);
  30.  
  31. <div class="form-group destination-poi collection">
  32. <h3>Add POI</h3>
  33. <ul id="poi_collection" class="list-unstyled row" data-prototype="{{ form_widget(form.pois.vars.prototype)|e }}">
  34. {% for poi in form.pois %}
  35. {% set key = loop.index0 %}
  36. <script id="key" type="text/template">
  37. {{ key }}
  38. </script>
  39. <li class="col-md-12">
  40. {{ form_row(poi) }}
  41.  
  42. <ul id="poi_images_collection_{{ key }}" class="collection list-unstyped row" data-prototype="{{ form_widget(poi.poiImages.vars.prototype)|e }}">
  43. {% for poiImage in poi.poiImages %}
  44. <li class="col-md-3">
  45.  
  46. <div class="hidden">{{ form_row(image.fullUrl) }}</div>
  47. {{ form_row(poiImage.ID) }}
  48. {{ form_row(poiImage.sequence) }}
  49. {{ form_row(poiImage.caption) }}
  50. {{ form_row(poiImage.altText) }}
  51. {{ form_row(poiImage.credit) }}
  52.  
  53. <a href="#" class="btn-remove">Remove Image</a>
  54. </li>
  55. {% endfor %}
  56. </ul>
  57. <a class="btn-add btn btn-default" data-target="poi_images_collection_{{ key }}">Add Image</a>
  58.  
  59. <a href="#" class="btn-remove">Remove POI</a>
  60. </li>
  61. {% endfor %}
  62. </ul>
  63. <a class="btn-add btn btn-default" data-target="poi_collection">Add POI</a>
  64. </div>
  65.  
  66. <ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
  67. ...
  68. </ul>
  69.  
  70. var $collectionHolder;
  71.  
  72. // setup an "add a tag" link
  73. var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
  74. var $newLinkLi = $('<li></li>').append($addTagLink);
  75.  
  76. jQuery(document).ready(function() {
  77. // Get the ul that holds the collection of tags
  78. $collectionHolder = $('ul.tags');
  79.  
  80. // add the "add a tag" anchor and li to the tags ul
  81. $collectionHolder.append($newLinkLi);
  82.  
  83. // count the current form inputs we have (e.g. 2), use that as the new
  84. // index when inserting a new item (e.g. 2)
  85. $collectionHolder.data('index', $collectionHolder.find(':input').length);
  86.  
  87. $addTagLink.on('click', function(e) {
  88. // prevent the link from creating a "#" on the URL
  89. e.preventDefault();
  90.  
  91. // add a new tag form (see next code block)
  92. addTagForm($collectionHolder, $newLinkLi);
  93. });
  94. });
  95.  
  96. function addTagForm($collectionHolder, $newLinkLi) {
  97. // Get the data-prototype explained earlier
  98. var prototype = $collectionHolder.data('prototype');
  99.  
  100. // get the new index
  101. var index = $collectionHolder.data('index');
  102.  
  103. // Replace '__name__' in the prototype's HTML to
  104. // instead be a number based on how many items we have
  105. var newForm = prototype.replace(/__name__/g, index);
  106.  
  107. // increase the index with one for the next item
  108. $collectionHolder.data('index', index + 1);
  109.  
  110. // Display the form in the page in an li, before the "Add a tag" link li
  111. var $newFormLi = $('<li></li>').append(newForm);
  112. $newLinkLi.before($newFormLi);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement