-1
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'];

0

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!'));
  }
}
| improve this answer | |
  • It is working for the add image but while replacing the image it is showing the error message in alert box PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'public://firoz_uploads/dog2_7.jpeg' for key 'uri': INSERT INTO {file_managed} – Firoz Tennali May 3 '18 at 5:19
  • The file used in the Image field may not be referenced. Are you getting this error also while editing and click on save? – Ajay Reddy May 3 '18 at 6:14
  • Actually the problem with the hidden id and the other parameter which should be input instead of values, it's working now. Thank you for your help Ajay, it's really helpful. – Firoz Tennali May 3 '18 at 6:30
  • Ajay one more issue I am facing is that when I make changes in other fields and submit the edit then it is throwing me the following error: "The file used in the Image field may not be referenced". – Firoz Tennali May 3 '18 at 11:03
  • Can you check drupal.stackexchange.com/questions/4433/… – Ajay Reddy May 3 '18 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.