Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retain and retrieve image details in Drupal 7 Ajax Form with Add More option?

My custom module code:

This is the screenshot of my form

  <?php
  function my_module_menu() {
  $items = array();

  $items['form-example'] = array( 
    'title' => 'My Module Form', 
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('my_module_form'), 
    'access callback' => TRUE
  );
      
  return $items;
}

function my_module_form($form, &$form_state, $no_js_use = FALSE) {  
  
  $form['file'] = array(
        '#type' => 'file',
        '#title' => t('Image'),
        '#description' => t('Upload an image'),
  );
  
    $form['menu'] = array(
    '#markup' => '<b>Add More:</b>'
    );
    
      $form['#tree'] = TRUE;
      $form['names_fieldset'] = array(
        '#type' => 'fieldset',
        '#title' => t('Add more images'),
        '#prefix' => '<div id="names-fieldset-wrapper">',
        '#suffix' => '</div>',
      );

      if (empty($form_state['num_names'])) {
        $form_state['num_names'] = 1;
      }
      
      for ($i = 0; $i < $form_state['num_names']; $i++) {       
        $form['names_fieldset']['name'][$i][0]= array(
             '#title' => t('Image'),
             '#type' => 'file',
             '#weight' => '5',
             '#description' => t('Upload an image'),
        );
      }
          
      $form['names_fieldset']['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Add one more'),
        '#submit' => array('my_module_add_more_add_one'),
        '#ajax' => array(
          'callback' => 'my_module_add_more_callback',
          'wrapper' => 'names-fieldset-wrapper',
        ),
      );
      if ($form_state['num_names'] > 1) {
        $form['names_fieldset']['remove_name'] = array(
          '#type' => 'submit',
          '#value' => t('Remove one'),
          '#submit' => array('my_module_add_more_remove_one'),
          '#ajax' => array(
            'callback' => 'my_module_add_more_callback',
            'wrapper' => 'names-fieldset-wrapper',
          ),
        );
      }
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),    
  );
  
  $form['#submit'][] = 'my_module_add_more_submit';
    if ($no_js_use) {
    if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
      unset($form['names_fieldset']['remove_name']['#ajax']);
    }
    unset($form['names_fieldset']['add_name']['#ajax']);
  }    
  return $form;
} 


function my_module_add_more_callback($form, $form_state) {
  return $form['names_fieldset'];
}

function my_module_add_more_add_one($form, &$form_state) {
  $form_state['num_names']++;
  $form_state['rebuild'] = TRUE;
   //$form_state['no_redirect'] = TRUE;
}

function my_module_add_more_remove_one($form, &$form_state) {
  if ($form_state['num_names'] > 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}

function my_module_add_more_submit($form, &$form_state) {
    $file = $form_state['values']['file']."<br \>";
    $validators = array();
    $file = file_save_upload('file', $validators, 'public://uploads');  
    print_r($file);     
    exit();
}

 

When i submit the form, i am trying to get the details of images, that are added through Add More option. But i am not able to get them. However i am only able to get the details of the first image (and able to upload it).

I want to know two things here:

  1. How can i retrieve the details of the images that are added with Add More option (fieldset), and how can i upload them ?
  2. When i browse and select an image in the fieldset, it is not retained in the form, after adding another image field. How to retain the selected images in fieldset ?
like image 918
shasi kanth Avatar asked Mar 29 '13 08:03

shasi kanth


1 Answers

Look at this article -- http://evolvingweb.ca/story/poutine-maker-introduction-field-api-drupal-7-part-1 -- as it has some information on something missing in your code. $delta. $delta is the id assigned to values of fields, even if your field only has 1 item.

like image 72
Shawn Avatar answered Sep 30 '22 15:09

Shawn