paragraphs.ajax.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @file
  4. * Provides callbacks for ajax endpoints.
  5. */
  6. /**
  7. * Page callback to handle AJAX for editing a paragraphs item.
  8. *
  9. * This is a direct page callback. The actual job of deleting the item is
  10. * done in the submit handler for the button, so all we really need to
  11. * do is process the form and then generate output. We generate this
  12. * output by doing a replace command on the id of the entire form element.
  13. */
  14. function paragraphs_edit_js() {
  15. // drupal_html_id() very helpfully ensures that all html IDS are unique
  16. // on a page. Unfortunately what it doesn't realize is that the IDs
  17. // we are generating are going to replace IDs that already exist, so
  18. // this actually works against us.
  19. if (isset($_POST['ajax_html_ids'])) {
  20. unset($_POST['ajax_html_ids']);
  21. }
  22. list($form, $form_state) = ajax_get_form();
  23. drupal_process_form($form['#form_id'], $form, $form_state);
  24. // Get the information on what we're removing.
  25. $button = $form_state['triggering_element'];
  26. // Go two levels up in the form, to the whole widget.
  27. $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -4));
  28. // Now send back the proper AJAX command to replace it.
  29. $return = array(
  30. '#type' => 'ajax',
  31. '#commands' => array(
  32. ajax_command_replace('#' . $element['#id'], drupal_render($element))
  33. ),
  34. );
  35. // Because we're doing this ourselves, messages aren't automatic. We have
  36. // to add them.
  37. $messages = theme('status_messages');
  38. if ($messages) {
  39. $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  40. }
  41. return $return;
  42. }
  43. /**
  44. * Page callback to handle AJAX for collapse a paragraphs item.
  45. *
  46. * This is a direct page callback. The actual job of deleting the item is
  47. * done in the submit handler for the button, so all we really need to
  48. * do is process the form and then generate output. We generate this
  49. * output by doing a replace command on the id of the entire form element.
  50. */
  51. function paragraphs_collapse_js() {
  52. // drupal_html_id() very helpfully ensures that all html IDS are unique
  53. // on a page. Unfortunately what it doesn't realize is that the IDs
  54. // we are generating are going to replace IDs that already exist, so
  55. // this actually works against us.
  56. if (isset($_POST['ajax_html_ids'])) {
  57. unset($_POST['ajax_html_ids']);
  58. }
  59. list($form, $form_state) = ajax_get_form();
  60. drupal_process_form($form['#form_id'], $form, $form_state);
  61. // Get the information on what we're removing.
  62. $button = $form_state['triggering_element'];
  63. // Go two levels up in the form, to the whole widget.
  64. $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -4));
  65. // Now send back the proper AJAX command to replace it.
  66. $return = array(
  67. '#type' => 'ajax',
  68. '#commands' => array(
  69. ajax_command_replace('#' . $element['#id'], drupal_render($element))
  70. ),
  71. );
  72. // Because we're doing this ourselves, messages aren't automatic. We have
  73. // to add them.
  74. $messages = theme('status_messages');
  75. if ($messages) {
  76. $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  77. }
  78. return $return;
  79. }
  80. /**
  81. * Page callback to handle AJAX for removing a paragraphs item.
  82. *
  83. * This is a direct page callback. The actual job of deleting the item is
  84. * done in the submit handler for the button, so all we really need to
  85. * do is process the form and then generate output. We generate this
  86. * output by doing a replace command on the id of the entire form element.
  87. */
  88. function paragraphs_remove_js() {
  89. // drupal_html_id() very helpfully ensures that all html IDS are unique
  90. // on a page. Unfortunately what it doesn't realize is that the IDs
  91. // we are generating are going to replace IDs that already exist, so
  92. // this actually works against us.
  93. if (isset($_POST['ajax_html_ids'])) {
  94. unset($_POST['ajax_html_ids']);
  95. }
  96. list($form, $form_state) = ajax_get_form();
  97. drupal_process_form($form['#form_id'], $form, $form_state);
  98. // Get the information on what we're removing.
  99. $button = $form_state['triggering_element'];
  100. // Go two levels up in the form, to the whole widget.
  101. $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -4));
  102. // Now send back the proper AJAX command to replace it.
  103. $return = array(
  104. '#type' => 'ajax',
  105. '#commands' => array(
  106. ajax_command_replace('#' . $element['#id'], drupal_render($element))
  107. ),
  108. );
  109. // Because we're doing this ourselves, messages aren't automatic. We have
  110. // to add them.
  111. $messages = theme('status_messages');
  112. if ($messages) {
  113. $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  114. }
  115. return $return;
  116. }
  117. /**
  118. * Page callback to handle AJAX for removing a paragraphs item.
  119. *
  120. * This is a direct page callback. The actual job of deleting the item is
  121. * done in the submit handler for the button, so all we really need to
  122. * do is process the form and then generate output. We generate this
  123. * output by doing a replace command on the id of the entire form element.
  124. */
  125. function paragraphs_deleteconfirm_js() {
  126. // drupal_html_id() very helpfully ensures that all html IDS are unique
  127. // on a page. Unfortunately what it doesn't realize is that the IDs
  128. // we are generating are going to replace IDs that already exist, so
  129. // this actually works against us.
  130. if (isset($_POST['ajax_html_ids'])) {
  131. unset($_POST['ajax_html_ids']);
  132. }
  133. list($form, $form_state) = ajax_get_form();
  134. drupal_process_form($form['#form_id'], $form, $form_state);
  135. // Get the information on what we're removing.
  136. $button = $form_state['triggering_element'];
  137. // Go two levels up in the form, to the whole widget.
  138. $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -4));
  139. // Now send back the proper AJAX command to replace it.
  140. $return = array(
  141. '#type' => 'ajax',
  142. '#commands' => array(
  143. ajax_command_replace('#' . $element['#id'], drupal_render($element))
  144. ),
  145. );
  146. // Because we're doing this ourselves, messages aren't automatic. We have
  147. // to add them.
  148. $messages = theme('status_messages');
  149. if ($messages) {
  150. $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  151. }
  152. return $return;
  153. }
  154. /**
  155. * Page callback to handle AJAX for restoring a paragraphs item.
  156. *
  157. * This is a direct page callback. The actual job of deleting the item is
  158. * done in the submit handler for the button, so all we really need to
  159. * do is process the form and then generate output. We generate this
  160. * output by doing a replace command on the id of the entire form element.
  161. */
  162. function paragraphs_restore_js() {
  163. // drupal_html_id() very helpfully ensures that all html IDS are unique
  164. // on a page. Unfortunately what it doesn't realize is that the IDs
  165. // we are generating are going to replace IDs that already exist, so
  166. // this actually works against us.
  167. if (isset($_POST['ajax_html_ids'])) {
  168. unset($_POST['ajax_html_ids']);
  169. }
  170. list($form, $form_state) = ajax_get_form();
  171. drupal_process_form($form['#form_id'], $form, $form_state);
  172. // Get the information on what we're removing.
  173. $button = $form_state['triggering_element'];
  174. // Go two levels up in the form, to the whole widget.
  175. $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -4));
  176. // Now send back the proper AJAX command to replace it.
  177. $return = array(
  178. '#type' => 'ajax',
  179. '#commands' => array(
  180. ajax_command_replace('#' . $element['#id'], drupal_render($element))
  181. ),
  182. );
  183. // Because we're doing this ourselves, messages aren't automatic. We have
  184. // to add them.
  185. $messages = theme('status_messages');
  186. if ($messages) {
  187. $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  188. }
  189. return $return;
  190. }