I'm writing a custom module, and I am able to upload an image but unable to update image in edit

drupal.stackexchange https://drupal.stackexchange.com/questions/260958

  •  21-01-2021
  •  | 
  •  

Question

function my_custom_table_edit_form($form, &$form_state){

  $id = arg(3);
  $result = db_query('SELECT * FROM {custom_table} WHERE id = :tid', array(':tid' => $id));
  /* foreach($result as $val){
       $record = $val;
     }
  */
  $record = $result->fetchObject();
  // now I add a text field to the form
  // with a label and fixed dimensions (you never know…)
  $form['my_custom_table_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#value' => t($record->title),
    '#description' => t('The Title of the My Custom Table.'),
    '#size' => 40,
    '#maxlength' => 120,
    '#required' => TRUE,
  );
  // Textarea for the body
  $form['my_custom_table_description'] = array(
    '#type' => 'textarea',
    '#rows' => 10,
    '#columns' => 40,
    '#title' => t('Description'),
    '#value' => t($record->description),
    '#required' => TRUE,
    '#description'=> t('The text of My Custom Table .'),
  );
  $form['my_custom_table_image'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image'),
    '#default_value' => t($record->image),
    '#size' => 40,
    '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
    '#upload_location' => 'public://firoz_uploads/'
  );
  // hidden for the body
  $form['id'] = array(
    '#type' => 'hidden',
    '#value' => t($id),
  );
  // Checkbox to indicate.
  $form['my_custom_active'] = array(
    '#type' => 'checkbox',
    '#title' => t('Status'),
    '#description' => t("Indicates whether the active or inactive."),
  );
  // now I add also a button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  // and now I assign a my function as handler of the submit event
// $form['#validate'][] = 'my_custom_table_submit_handler';
  $form['#submit'][] = 'my_custom_table_edit_submit_handler';
  return $form;
}

function my_custom_table_edit_submit_handler($form, &$form_state){
  // this function will be executed after the click
  // event of the user on the "submit" button.
  // here I only print a message
  // you can access a database, redirect, or whatever you want, obviously
  $error = 1;
  if ( !isset($form_state['values']['my_custom_table_title']) || !isset($form_state['values']['my_custom_table_title'])) {
    $error = 0 ;
  }
  if($error){
    $id = $form_state['values']['id'];
    $my_custom_table_title = $form_state['input']['my_custom_table_title'];
    $my_custom_table_description = $form_state['input']['my_custom_table_description'];
    $my_custom_table_image = $form_state['input']['my_custom_table_image'];
    if (isset($form_state['input']['my_custom_table_image'])) {
        $file = file_load($form_state['input']['my_custom_table_image']);
        $file->status = FILE_STATUS_PERMANENT;
        $my_image_file_name = $file->filename;
        file_save($file);
    }
    $data = array(
        'title' => $my_custom_table_title,
        'description' => $my_custom_table_description,
        'image' => $my_image_file_name,
    );
    $num_updated = db_update('custom_table')
    ->fields($data)
    ->condition('id', $id, '=')
    ->execute();
    drupal_set_message(t('Record has been Updated!'));
  }
}

During submit I have checked that File element returns the empty/NULL.

Here $form_state['values']['my_custom_table_image'];

Was it helpful?

Solution

There are some corrections in your code.

1) $id in submit handler. use $id from $form_state['values'] rather than input. As upload happens in ajax, $id will be replaced by "ajax".

2) Save FID in the table rather than filename

3) use default values only if there is any data in your table for that TID.

4) Use db_merge rather than db_updat. Initially, you don't have any values to update using ID as the condition.

Made some changes in your code hope this helps you.

  function my_custom_table_edit_form($form, &$form_state){

    $id = arg(3);
    $result = db_query('SELECT * FROM {custom_table} WHERE id = :tid', array(':tid' => $id));
    /* foreach($result as $val){
      $record = $val;
      } */
    $record = $result->fetchObject();
    // here set default values.
    if ($record) {
      $title = t($record->title);
      $description = t($record->description);
      $image = t($record->image);
    }
// now I add a text field to the form
// with a label and fixed dimensions (you never know…)
    $form['my_custom_table_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#value' => isset($title) ? $title : '',
      '#description' => t('The Title of the My Custom Table.'),
      '#size' => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );
// Textarea for the body
    $form['my_custom_table_description'] = array(
      '#type' => 'textarea',
      '#rows' => 10,
      '#columns' => 40,
      '#title' => t('Description'),
      '#value' => isset($description) ? $description : '',
      '#required' => TRUE,
      '#description' => t('The text of My Custom Table .'),
    );
    $form['my_custom_table_image'] = array(
      '#type' => 'managed_file',
      '#title' => t('Image'),
      '#default_value' => isset($image) ? $image : '',
      '#size' => 40,
      '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
      '#upload_location' => 'public://firoz_uploads/'
    );
// hidden for the body
    $form['id'] = array(
      '#type' => 'hidden',
      '#value' => t($id),
    );
     // Checkbox to indicate.
   $form['my_custom_active'] = array(
      '#type' => 'checkbox',
      '#title' => t('Status'),
      '#description' => t("Indicates whether the active or inactive."),
   );
   // now I add also a button
   $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
   );
   // and now I assign a my function as handler of the submit event
   // $form['#validate'][] = 'my_custom_table_submit_handler';
   $form['#submit'][] = 'my_custom_table_edit_submit_handler';
   return $form;
  }
}

function my_custom_table_edit_submit_handler($form, &$form_state) {

// this function will be executed after the click
// event of the user on the "submit" button.
// here I only print a message
// you can access a database, redirect, or whatever you want, obviously
  $error = 1;
 // use your ID by getting it from dorm state values, as your image upload is in ajax, $id will be replaced.
  $id = $form_state['values']['tid'];
  if (!isset($form_state['input']['my_custom_table_title']) || !isset($form_state['input']['my_custom_table_title'])) {
    $error = 0;
  }
  if ($error) {
    $my_custom_table_title = $form_state['input']['my_custom_table_title'];
    $my_custom_table_description = $form_state['input']['my_custom_table_description'];
    $my_custom_table_image = $form_state['input']['my_custom_table_image'];
    if (isset($my_custom_table_image)) {
      $file = file_load($my_custom_table_image['fid']);
      $file->status = FILE_STATUS_PERMANENT;
      // **Use FID here rather than filename.**
      $my_image_file_name = $file->fid;
      file_save($file);
    }
    $data = array(
      'title' => $my_custom_table_title,
      'description' => $my_custom_table_description,
      'image' => $my_image_file_name,
    );
    // use DB merge as you dint have any data to update initially
    $num_updated = db_merge('custom_table')
      ->key(array('id' => $id))
      ->fields($data)
      ->execute();
    drupal_set_message(t('Record has been Updated!'));
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top