edgar.dueñas
Enviado por edgar.dueñas el Mié, 11/05/2016 - 16:55

Insertando un nuevo nodo con la función node_save. A continuacion se muestra un ejemplo de como almacenar un nodo en base de datos utilizando el hook submit.

Creación de un nodo:

 

<?php
function contributing_donor_form_submit($form, &$form_state) {
 
    // Create a node object, and add node properties.
    $newNode = (object) NULL;
    $newNode->type = 'contributing_donors';
    $newNode->uid = 0;
    $newNode->created = strtotime("now");
    $newNode->changed = strtotime("now");
    $newNode->status = 0;
    $newNode->comment = 0;
    $newNode->promote = 0;
    $newNode->moderate = 0;
    $newNode->sticky = 0;
    $newNode->language = 'und';
 
    node_object_prepare($newNode);
 
    $newNode->title = $form_state['values']['name_donor'];
 
    //Add to field Amount
    $amountValue = (object) array(
                "value" => $form_state['values']['amount_donor']
    );
    $newNode->field_amount_life_time['und'][0] = (array) $amountValue;
 
    // Save the node.
    node_save($newNode);
 
    drupal_set_message("Your account has been succcessfully updated.");
 
}
?>
Un ejemplo mas completo:
 
<?php
function transaction_money_field_form_submit($form, &$form_state) {
    global $user;
 
    //Load type transaction for user
    $currendUid = $user->uid; //$user->uid
    $userData = entity_load('user', array($currendUid));
    $userTransactionType = $userData[$currendUid]->field_user_transaction_type['und'][0]['tid'];
 
 
    //Node load por nid
    $node = node_load($form_state['values']['nid']);
    $categoryNode = $node->field_category['und'][0]['tid'];
 
 
    // Create a node object, and add node properties.
    $newNode = (object) NULL;
    $newNode->type = 'project_money';
    $newNode->uid = 0;
    $newNode->created = strtotime("now");
    $newNode->changed = strtotime("now");
    $newNode->status = 1;
    $newNode->comment = 0;
    $newNode->promote = 0;
    $newNode->moderate = 0;
    $newNode->sticky = 0;
    $newNode->language = 'und';
 
    node_object_prepare($newNode);
    //Add to field NID (nodo)
    $projectNid = (object) array(
                "nid" => $form_state['values']['nid']
    );
    $newNode->field_project_nid['und'][0] = (array) $projectNid;
    //Add to field UID (user)
    $projectUid = (object) array(
                "uid" => $user->uid
    );
    $newNode->field_user_uid['und'][0] = (array) $projectUid;
    //Add to field Amount
    $amountValue = (object) array(
                "value" => $form_state['values']['amount']
    );
    $newNode->field_amount['und'][0] = (array) $amountValue;
    //Add to field Category
    $categoryNodeArray = (object) array(
                "tid" => $categoryNode
    );
    $newNode->field_project_category['und'][0] = (array) $categoryNodeArray;
 
    //Add to field Transaction Type
    $categoryTransactionUser = (object) array(
                "tid" => $userTransactionType
    );
    $newNode->field_transaction_type['und'][0] = (array) $categoryTransactionUser;
    //Add to field date
    $fieldDate = (object) array(
                "value" => date('Y-m-d- H:i:s'),
                "timezone " => 'America/New_York',
                "timezone_db" => 'UTC',
                "date_type" => 'datetime'
    );
    $newNode->field_date['und'][0] = (array) $fieldDate;
 
    // Save the node.
    // The node will also get published, if $newNode->status was set to 1.
    node_save($newNode);
 
    drupal_goto('allocation-success', array('query' => array(
            'ref' => $node->title,
            'val' => $form_state['values']['amount']
    )));
}
 
?>
Otra forma de crear un nodo:
 
<?php
/**
* Basic Node Creation Example for Drupal 7
*
* This example:
* - Assumes a standard Drupal 7 installation
* - Does not verify that the field values are correct
*/
  $body_text = 'This is the body text I want entered with the node.';
 
  $node = new stdClass();
  $node->type = 'article';
  node_object_prepare($node);
 
  $node->title    = 'Node Created Programmatically on ' . date('c');
  $node->language = LANGUAGE_NONE;
 
  $node->body[$node->language][0]['value']   = $body_text;
  $node->body[$node->language][0]['summary'] = text_summary($body_text);
  $node->body[$node->language][0]['format']  = 'filtered_html';
 
  $path = 'content/programmatically_created_node_' . date('YmdHis');
  $node->path = array('alias' => $path);
 
  node_save($node);
?>
Actualizacion de un nodo:
 
<?php
/**
* Basic Node Update Example for Drupal 7
*
* This example:
* - Assumes a standard Drupal 7 installation
* - Assumes there is a node with a nid of 1
*/
  $nid = 1;
  $node = node_load($nid);
  $node->title = 'Updated Title Text';
  node_save($node);
?>
Revisiones:
 
<?php
/**
* Basic Node Revision Example for Drupal 7
*
* This example:
* - Assumes a standard Drupal 7 installation
* - Assumes there is a node with a nid of 1
*/
  $nid = 1;
  $node = node_load($nid);
  $node->body[$node->language][0]['value'] .= "\nA line of text added by program.";
 
  // Make this change a new revision
  $node->revision = 1;
  $node->log = 'This node was programmatically updated at ' . date('c');
 
  node_save($node);
?>