Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sandbox_field_widget_info() {
- dsm('sandbox_field_widget_info');
- return array(
- 'sandbox' => array(
- 'behaviors' => array(
- 'default value' => FIELD_BEHAVIOR_NONE,
- 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
- ),
- 'field types' => array('file'),
- 'label' => 'xxxxing sandbox',
- ),
- );
- }
- function sandbox_field_widget_form(&$form, &$form_state, $field, $instance, $language, $items, $delta, $element) {
- dsm('sandbox_field_widget_form');
- $element_info = element_info('managed_file');
- $element += array(
- '#type' => 'managed_file',
- '#process' => array_merge($element_info['#process'], array('sandbox_field_widget_process')),
- );
- dsm($element, ' element');
- // Add ajax wrapper so that the entire form can be replaced.
- // This is the only way to replace multiple fields.
- // $form['#prefix'] = '<div id="' . 'sandbox-wrapper-all' . '">';
- // $form['#suffix'] = '</div>';
- return $element;
- }
- function sandbox_field_widget_form_alter(&$element, &$form_state, $context) {
- $field_name = $context['field']['field_name'];
- $wrapper_id = 'sandbox-' . $field_name . '-ajax-wrapper';
- $wrapper_prefix = '<div id="' . $wrapper_id . '">';
- $wrapper_suffix = '</div>';
- dsm($field_name, 'sandbox_field_widget_form_alter');
- // dsm($element, ' element');
- // Adds a wrapper around each field.
- if (isset($element['#prefix'])) {
- $element['#prefix'] .= $wrapper_prefix;
- }
- else {
- $element['#prefix'] = $wrapper_prefix;
- }
- if (isset($element['#suffix'])) {
- $element['#suffix'] = $wrapper_suffix . $element['#suffix'];
- }
- else {
- $element['#suffix'] = $wrapper_suffix;
- }
- }
- function sandbox_field_widget_process($element, &$form_state, $form) {
- dsm('sandbox_field_widget_process');
- // Replace ajax calls.
- $ajax_replacement = $element['upload_button']['#ajax'];
- $ajax_replacement['path'] = 'sandbox/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id]['#value'];
- $ajax_replacement['wrapper'] = 'sandbox-wrapper-header';
- $element['upload_button']['#ajax'] = $ajax_replacement;
- // Toggle to see some changes!!!
- if (!empty($form['field_text_widget']['und'][0]['value']['#title'])) {
- $form['field_text_widget']['und'][0]['value']['#title'] = 'xxxx';
- }
- else {
- $form['field_text_widget']['und'][0]['value']['#title'] = '';
- }
- dsm($ajax_replacement);
- return $element;
- }
- function sandbox_ajax_callback() {
- dsm('sandbox_ajax_callback');
- $args = func_get_args();
- list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
- drupal_process_form($form['#form_id'], $form, $form_state);
- $form_state['rebuild'] = TRUE;
- $ajax = array(
- '#type' => 'ajax',
- '#commands' => array(),
- );
- $commands = array();
- $commands[] = ajax_command_replace(NULL, theme('status_messages'));
- $commands[] = ajax_command_replace('#sandbox-field_sandbox_widget-ajax-wrapper', render($form['field_sandbox_widget']));
- $commands[] = ajax_command_replace('#sandbox-field_text_widget-ajax-wrapper', render($form['field_text_widget']));
- $ajax['#commands'] = array_merge($ajax['#commands'], $commands);
- return $ajax;
- }
- function sandbox_form_alter(&$form, &$form_state, $form_id) {
- if (!isset($form['#prefix'])) {
- $form['#prefix'] = '';
- }
- $form['#prefix'] = '<div id="sandbox-wrapper-header"></div>';
- }
- function sandbox_help($path, $arg) {
- switch ($path) {
- case 'admin/help#sandbox':
- $output = '';
- $output .= 'xxxx';
- return $output;
- }
- }
- function sandbox_menu() {
- dsm('sandbox_menu');
- // Copied from file_menu
- $items = array();
- $items['sandbox/ajax'] = array(
- 'page callback' => 'sandbox_ajax_callback',
- // 'page callback' => 'file_ajax_upload',
- 'delivery callback' => 'ajax_deliver',
- 'access arguments' => array('access content'),
- 'theme callback' => 'ajax_base_page_theme',
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement