Search code examples
formsdrupaldrupal-7content-type

How to add content (post) of content type using form by user


I would like to make one form that allow to user to add post for my content-type. form will have all fields that I have created in content type. once user will submit the from then entry will automatically add in content-type.


Solution

  • You can add creation the new node in the form submit function

    function custom_page_submit($form, &$form_state) {
      global $user;
    
      //Create new object and fill the fields;
      $node = new stdClass();
      $node->title = "SOME TITLE";
      $node->type = "YOUR_NODE_TYPE";
      node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
      $node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
      $node->uid = $user->uid; 
      $node->status = 1; //(1 or 0): published or not
      $node->promote = 0; //(1 or 0): promoted to front page
      $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write
    
      $node = node_submit($node); // Prepare node for saving
      node_save($node);
      // Display success message
      drupal_set_message("Node was saved!");
      // And you can specify where user shoul be redirected
      $form_state['redirect']  = 'SOME WHERE'; // 'front' - if redirect to front page
    }