简体   繁体   中英

add/edit form in custom module Drupal 7?

Hello I make custom form for add content in custom module. I make another form for edit custom nodes. I want to merge two forms in one form for add and edit.

function example_add_form($form, &$form_state) {


  $form['name']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
  );

  $form['name']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
  );


  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Create Order',
  );

  return $form;
}

function example_add_form_submit($form, &$form_state) {


  $node = new stdClass();
  node_object_prepare($node);
  $node->language = LANGUAGE_NONE;
  $node->title = $form_state['values']['title'];
  $node->body[LANGUAGE_NONE][0]['value'] = $form_state['values']['description'];
  node_save($node);

}

this is my edit formthis is my edit formthis is my edit form this is my edit form this is my edit form this is my edit form

function example_edit_form($form, &$form_state, $nid) {

  global $node;
  $node = node_load($nid);

  $form['title'] = array(
    '#title' => 'Title',
    '#type' => 'textfield',
    '#default_value' => $node->title,
  );

  $form['name']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body[LANGUAGE_NONE][0]['value'],
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Update Order',
  );

  return $form;
}

Try like this way, it might be useful for you

Pass argument nid in both the condition

// $nid = ''; //by default NULL
// $nid = YOUR_NODE_ID; pass nid when you edit the node

function example_add_form($form, &$form_state, $nid) {

  global $node;
  if(!empty($nid)){
    $node = node_load($nid);
    $body = $node->body[LANGUAGE_NONE][0]['value'];
  }

  // Create Hidden field    
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $nid, // pass node id in form data
  );

  $form['name']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => isset($node->title) ? $node->title : NULL;
  );


  $form['name']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($body) ? $body : NULL;
  );


  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Create Order',
  );

  return $form;
}

function example_add_form_submit($form, &$form_state) {

  // check if nid value is empty then create a node otherwise update it
  if(empty($form_state['values']['nid']){
  $node = new stdClass();
  node_object_prepare($node);
  $node->language = LANGUAGE_NONE;
  $node->title = $form_state['values']['title'];
  $node->body[LANGUAGE_NONE][0]['value'] = $form_state['values']['description'];
  node_save($node);
  }else{
    // update node data
  }

}

Also one thing, If you don't want to create nid hidden field then you can get nid value in form submit directly through this way

$form_state['build_info']['args']; is an array containing at index 0 the value of argument nid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM