Create a node with date field (node_save) in Drupal 7

<?php
/**
* Implements hook_user_insert().
*/
function mymod_user_insert(&$edit, $account, $category) {
  
$rid = 2; // optionally, you can check for a specific role
  
if (in_array($rid, array_keys($account->roles))) {
    for (
$x = 1; $x <= 365; $x++) {
     
$datetime = date('Y-m-d', (time() + ($x * (60 * 60 * 24))));
     
mymod_new_node($account->uid, $datetime);
    }
   
drupal_set_message('Created ' . $x . ' new nodes with date field for this user!');
  }
}

/**
* function to create a node with date field for this user
*/
function mymod_new_node($uid, $datetime) {
 
$node = new StdClass();
 
$node->type = 'calender_item'// machine name content type
 
$node->title = $datetime;
 
$node->language = 'nl'; .
 
$node->status = 1;
 
$node->uid = $uid;
 
$datetime .= " 00:00:00"; // I didn't care for the time so added this
 
$node->field_datum = array(
   
LANGUAGE_NONE => array (
     
0 => array(
       
'value' => $datetime,
       
'timezone' => 'Europe/Amsterdam',
       
'timezone_db' => 'Europe/Amsterdam',
       
'date_type' => 'datetime',
        ),
      ),
    );
 
node_save($node);
}
?>

About

This is the company blog of
Drupal specialist Merge.nl

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments

Social