drupaldrupal-7drupal-modulesdrupal-formsdrupal-form-submission

Drupal 7: Insert into content type from custom module function


I created a form inside a custom module, in a drupal 7 project, and I need to insert the values into a custom content type called 'players'

Here is what I have for a form:

function custom_module_reg_form($form, $form_state){
  $form['first_name'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('First Name')),
  );

  $form['last_name'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Last Name')),
  );
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Email Address')),
  );
  $form['state'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('State')),
  );
  $form['zip_code'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Zip Code')),
  );
  $form['phone_number'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Phone Number')),
  );
  $form['password'] = array(
    '#type' => 'password',
    '#attributes' => array('placeholder' => t('Password')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Register',
  );

  return $form;
}

Here is the submit function but I am getting an error:

function custom_module_reg_form_submit($form, $form_state){
     $first_name = $form_state['values']['first_name'];
      $last_name = $form_state['values']['last_name'];
      $email_address = $form_state['values']['email_address'];
      $state = $form_state['values']['state'];
      $zip_code = $form_state['values']['zip_code'];
      $phone_number = $form_state['values']['phone_number'];
      $password = encrypt($form_state['values']['password']);

      $nid = db_insert('players')->fields(array(
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email_address' => $email_address,
        'state' => $state,
        'zip_code' => $zip_code,
        'phone_number' => $phone_number,
        'password' => $password,
        'created' => REQUEST_TIME
      ))->execute();

      // Save new node
      $node = new stdClass();
      // Set node title
      $node->title = $email_address;
      // set node type ex: article etc
      $node->type = "players";
      // set node language
      $node->language = LANGUAGE_NONE;
      //(1 or 0): published or not
      $node->status = 0;
      //(1 or 0): promoted to front page
      $node->promote = 0;
      node_object_prepare($node);
      node_save($node);
}

I was following an example and I see in my logs that the table is not correct but I can't find anywhere else that gives an example. What am I doing wrong? Is it better to create a custom table for programmatically inserting from forms? Thanks and please let me know.


Solution

  • There are two common ways of programmatically creating nodes in Drupal 7: without or with the Entity API contributed module.

    Without Entity API

    $node = new stdClass();
    $node->type = "players";
    $node->uid = $user->uid; // makes sense to have the global $user the owner of the node
    $node->language = LANGUAGE_NONE;
    $node->status = 0;
    $node->promote = 0;
    
    $node->title = $email_address;
    // NB: if you created the field in Drupal's UI -- it will be named "field_first_name", not "first_name"
    $node->field_first_name[$node->language][]['value'] = $first_name;
    // ...
    $node = node_submit($node);
    node_save($node);
    

    With Entity API (more common nowadays)

    // Use the Entity API to create a new object
    $values = array(
      'type' => 'players',
      'uid' => $user->uid,
      'status' => 1,
      'promote' => 0,
    );
    $entity = entity_create('node', $values);
    
    // Then create an entity_metadata_wrapper around the new entity.
    $wrapper = entity_metadata_wrapper('node', $entity);
    
    // Now assign values through the wrapper.
    $wrapper->title->set($email_address);
    $wrapper->field_first_name->set($first_name);
    // ...
    
    // Finally save the node.
    $wrapper->save();
    
    

    Whichever way you choose, you do not need db_insert('players'). It actually won't work, because Drupal 7 does not store the entity in a single database table.

    More information about creating nodes in code can be found here.