Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a content type called Event. I advertise events on my website using this form. I want to advertise automatically using code. I have this code which when I run automatically add content. But at the moment it adds the title and the body.

ini_set('display_errors','1');
ini_set('memory_limit', '512M');
define('DRUPAL_ROOT', getcwd());
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'node', 'node.pages');
$node = new stdClass();
//print_r($node);
$title          =  "test post 3";// html_entity_decode($_POST['title']);
// $summary     =   "summarry";//$_POST['summary'];      
$body           =  "This is testing of Automated Script";// $_POST['body']; 
$node->uid      = '0'; //author
$node->type     = 'event';    //content type
// $node->created  = time() - (rand( 1,240) * 60); 
$node->created  = strtotime("now");
$node->changed  = strtotime("now"); 
$node->field_location = 'Oxford';
$node->title    = $title;
$node->status   = 0;
$node->promote  = 0;
$node->sticky   = 0;
$node->language = 'und';
$node->body['und'][0]['value']   = $body;
// $node->body['und'][0]['summary'] = $summary;
$node->body['format']  = 'full_html';
node_submit($node);
node_save($node);
print "ok";

How do I add remaining inputs like City, Country, Event Date, Duration, Email Address using code?

Event content type showing machine names

share|improve this question
1  
Have those fields been added to the content type? And where data will come? –  xurshid29 Apr 2 at 13:01
    
Welcome to Drupal Answers! On Stack Exchange, questions and answers are kept separated. You can answer your own question, but you need to write an answer, not editing the question. Also, we don't normally change the title to say the issue has been resolved; that is what accepting answers is for. –  kiamlaluno Apr 7 at 20:20
add comment

2 Answers 2

up vote 4 down vote accepted

You need to do something similar to what you do to set the body of the note for each of your additional field (City, Country, Event Date, Duration, Email Address). The exact data structure for each filed depends on its type.

The general pattern is

$node->{$field_machine_name}[$language][0] = array(...);

The hard part if figuring the content of the array(...) which is the value of the field. The value of a field is actually always an associative array. Its keys (also called columns) depend on the type of the field. A text field, for instance, as only one value key/column. A taxonomy term reference field has also only one key/column, but named tid.

For instance your city and duration field are a simple text fields whose machine names are field_location and field_duration you can set their values with

$node->field_location['und'][0] =  array('value' => 'Oxford');
$node->field_duration['und'][0] =  array('value' => '2');

You country field is a Taxonomy Term Reference field, its value can be set with

$node->field_country['und'][0] = array('tid' => 1);

Where 1 is the ID of a term in the proper taxonomy vocabulary.

If you were using an single address field name field_location to store the location, the code should be something like

$node->field_location['und'][0] = array(
  'country' => 'GB',
  'locality' =>  'Oxford',
);

The structure to set a date field depends on the date format. If the field uses the ISO format, you can set it with something like

$node->field_date['und'][0] =  array(
  // The start date.
  'value' => '2014-04-02T10:00:00',
  // The end date.
  'value2' => '2014-04-02T11:00:00',
);

To discover the structure of a field, I suggest to use the Devel module. It provides a devel tab on node that let you explore the structure of the $node object for an existing node. You can also check the hook_field_schema() implementation of the module defining the field type.

share|improve this answer
    
+1 for address field and date examples –  Felix Eve Apr 2 at 13:17
add comment

You can use the following code to add custom fields:

$node->field_city[$node->language][0]['value'] = 'London';
$node->field_county[$node->language][0]['value'] = 'England';

I find the easiest way to make sure you are using the correct structure is to create a node using the back-end UI, and then inspect it with the devel module. This will show you the correct field names to use.

Also not all fields use 'value'.

For example a taxonomy relation field would use:

$node->field_taxonomy_reference[$node->language][0]['tid'] = 189;

Or an entity reference field would use:

$node->field_entity_reference[$node->language][0]['target_id'] = 189;

For some other examples see these blog posts How to programmatically create node in drupal 7 or Creating and Updating Nodes Programmatically in Drupal 7.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.