Using Drupal's node form in a new page -


in custom module want have page defined in hook_menu, shows add form specific content type, modifications form.

so far it's working, , saving new node, default values i'm setting in code, i.e. it's not picking user types form. checked , $form_state['input'] contains inputted values, $form_state['values'] doesn't, new node gets saved wrong.

here's relevant code:

function mymodule_menu() {   return array(     'admin/content/myadd/%' => array(       'title' => 'my custom add page',       'page callback' => 'mymodule_node_add',       'page arguments' => array(3),       'access callback' => true,       'type' => menu_callback,     ),   ); }  function mymodule_node_add() {   module_load_include('inc', 'node', 'node.pages');    //i'm doing print here instead of returning because i'm calling page   //in ajax popup, don't want whole page output, form.   print render(drupal_get_form('mymodule_node_add_form')); }  function mymodule_node_add_form($form, &$form_state) {   if (!isset($form_state['node']) {     global $user;     $node = (object) array(       'uid' => $user->uid,       'type' => 'mycontenttype',       'language' => language_none,     );      //this setting default value     $node->myfield = array(language_none => array(array('value' => arg(3))));      $form_state['build_info']['args'] = array($node);      $form = drupal_build_form('mycontenttype_node_form', $form_state);     $form['actions']['submit']['#submit'][0] = 'mymodule_node_add_form_submit';     //there's lot more customization of form here, adding fields, etc.   }    return $form; }  function mymodule_node_add_form_submit($form, &$form_state) {   //here's $form_state['input'] correct $form_state['values'] isn't.    $node = node_form_submit_build_node($form, $form_state);   node_save($node);   $form_state['values']['nid'] = $node->nid;   $form_state['nid'] = $node->nid;   $form_state['redirect'] = 'some/other/page'; } 

so, doing wrong here? should concerned form ids being wrong? (my form's id mymodule_node_add_form, actual form might output mycontenttype_node_form), affect me?

you want hook_form_alter() (see api.drupal.org). try use existing content type's form , alter hook_form_alter(). try first working standard, non-ajax page, can advantages of dpm() , other debugging techniques. when have down solid, modify take advantage of ajax techniques.

mymodule_form_alter(&$form, &$form_state, $form_id) {   // use devel module turned on verify   // $form_id , contents of forms load on given page   dpm($form);   // once verify $form_id, can begin accessing form , altering   switch( $form_id ) {     case 'my_target_form_id' :       // part pseudocode, haven't memorized $form structure,        // can dpm().       if( $form['node']->type == 'my_target_content_type' ) {         $form['actions']['submit']['#submit'][0] = 'mymodule_node_add_form_submit';       }       break;   } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -