Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. function sandbox_field_widget_info() {
  2. dsm('sandbox_field_widget_info');
  3. return array(
  4. 'sandbox' => array(
  5. 'behaviors' => array(
  6. 'default value' => FIELD_BEHAVIOR_NONE,
  7. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  8. ),
  9. 'field types' => array('file'),
  10. 'label' => 'xxxxing sandbox',
  11. ),
  12. );
  13. }
  14.  
  15. function sandbox_field_widget_form(&$form, &$form_state, $field, $instance, $language, $items, $delta, $element) {
  16. dsm('sandbox_field_widget_form');
  17.  
  18. $element_info = element_info('managed_file');
  19. $element += array(
  20. '#type' => 'managed_file',
  21. '#process' => array_merge($element_info['#process'], array('sandbox_field_widget_process')),
  22. );
  23.  
  24. dsm($element, ' element');
  25.  
  26. // Add ajax wrapper so that the entire form can be replaced.
  27. // This is the only way to replace multiple fields.
  28. // $form['#prefix'] = '<div id="' . 'sandbox-wrapper-all' . '">';
  29. // $form['#suffix'] = '</div>';
  30.  
  31.  
  32. return $element;
  33. }
  34.  
  35.  
  36. function sandbox_field_widget_form_alter(&$element, &$form_state, $context) {
  37. $field_name = $context['field']['field_name'];
  38. $wrapper_id = 'sandbox-' . $field_name . '-ajax-wrapper';
  39.  
  40. $wrapper_prefix = '<div id="' . $wrapper_id . '">';
  41. $wrapper_suffix = '</div>';
  42.  
  43. dsm($field_name, 'sandbox_field_widget_form_alter');
  44. // dsm($element, ' element');
  45.  
  46. // Adds a wrapper around each field.
  47. if (isset($element['#prefix'])) {
  48. $element['#prefix'] .= $wrapper_prefix;
  49. }
  50. else {
  51. $element['#prefix'] = $wrapper_prefix;
  52. }
  53.  
  54. if (isset($element['#suffix'])) {
  55. $element['#suffix'] = $wrapper_suffix . $element['#suffix'];
  56. }
  57. else {
  58. $element['#suffix'] = $wrapper_suffix;
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. function sandbox_field_widget_process($element, &$form_state, $form) {
  66. dsm('sandbox_field_widget_process');
  67.  
  68. // Replace ajax calls.
  69. $ajax_replacement = $element['upload_button']['#ajax'];
  70. $ajax_replacement['path'] = 'sandbox/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id]['#value'];
  71. $ajax_replacement['wrapper'] = 'sandbox-wrapper-header';
  72.  
  73. $element['upload_button']['#ajax'] = $ajax_replacement;
  74.  
  75. // Toggle to see some changes!!!
  76. if (!empty($form['field_text_widget']['und'][0]['value']['#title'])) {
  77. $form['field_text_widget']['und'][0]['value']['#title'] = 'xxxx';
  78. }
  79. else {
  80. $form['field_text_widget']['und'][0]['value']['#title'] = '';
  81. }
  82.  
  83. dsm($ajax_replacement);
  84. return $element;
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. function sandbox_ajax_callback() {
  92. dsm('sandbox_ajax_callback');
  93. $args = func_get_args();
  94.  
  95. list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
  96.  
  97. drupal_process_form($form['#form_id'], $form, $form_state);
  98. $form_state['rebuild'] = TRUE;
  99.  
  100. $ajax = array(
  101. '#type' => 'ajax',
  102. '#commands' => array(),
  103. );
  104.  
  105.  
  106.  
  107. $commands = array();
  108. $commands[] = ajax_command_replace(NULL, theme('status_messages'));
  109. $commands[] = ajax_command_replace('#sandbox-field_sandbox_widget-ajax-wrapper', render($form['field_sandbox_widget']));
  110. $commands[] = ajax_command_replace('#sandbox-field_text_widget-ajax-wrapper', render($form['field_text_widget']));
  111.  
  112. $ajax['#commands'] = array_merge($ajax['#commands'], $commands);
  113.  
  114. return $ajax;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. function sandbox_form_alter(&$form, &$form_state, $form_id) {
  121. if (!isset($form['#prefix'])) {
  122. $form['#prefix'] = '';
  123. }
  124. $form['#prefix'] = '<div id="sandbox-wrapper-header"></div>';
  125. }
  126.  
  127. function sandbox_help($path, $arg) {
  128.  
  129. switch ($path) {
  130. case 'admin/help#sandbox':
  131. $output = '';
  132. $output .= 'xxxx';
  133. return $output;
  134. }
  135. }
  136.  
  137.  
  138.  
  139. function sandbox_menu() {
  140. dsm('sandbox_menu');
  141.  
  142. // Copied from file_menu
  143. $items = array();
  144. $items['sandbox/ajax'] = array(
  145. 'page callback' => 'sandbox_ajax_callback',
  146. // 'page callback' => 'file_ajax_upload',
  147. 'delivery callback' => 'ajax_deliver',
  148. 'access arguments' => array('access content'),
  149. 'theme callback' => 'ajax_base_page_theme',
  150. 'type' => MENU_CALLBACK,
  151. );
  152.  
  153. return $items;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement