andreapavan
andreapavan

Reputation: 697

How to create programmatically data event node and set data fields

I followed the step by step guide to implement the module event calendar feature. This is the link to the module:

http://drupal.org/project/events_calendar_feature

I then created a custom modulethat downloads information from an external website every day (by implementing hook_cron). This module automatically creates nodes programmatically of type "data_event" that i created (following the guide mentioned above) with all the new events on the site every day. These are all the fields in the new content type "data_event" created:

LABEL: Title MACHINE NAME: title FIELD TYPE: Node module element

LABEL: Body MACHINE NAME: body FIELD TYPE: Node module element

LABEL: Date(s) MACHINE NAME: field_event_dates FIELD TYPE: Date WIDGET: Text field

enter image description here

The problem is set up properly the various fields of the node. How do I know the names of the fields for the date to be set?

This is my current implementation in my module. Commented lines are some tests without success.

Date format: dd/mm/yyyy Time format: hh:mm (24h)

function create_data_event_node($title, $id, $data_event_body, $startDate, $startDateTime, $endDate, $endDateTime) {
    watchdog("Indico Downloader", "Creating new data event node.");
    $node = new stdClass();
    $node->type = "data_event";
    $node->title = $title;
    $node->language = LANGUAGE_NONE;
    $node->id = $id;
    node_object_prepare($node);

    $node->body[$node->language][0]['value'] = $data_event_body;
    $node->body[$node->language][0]['format'] = 'full_html';

    // $node->field_event_dates[$node->language][0]['value']['date'] = $startDate." - ".$startDateTime;
    // $node->field_event_dates = array(
        //     0 => array(
        //         'value' => format_date($om__result->date, 'short'),
        //     ),
        // );
    // $node->field_event_dates[$node->language][0]['value']['time'] = $startDateTime;
    // $node->field_event_dates[$node->language][0]['value2']['date'] = $endDate;
    // $node->field_event_dates[$node->language][0]['value2']['time'] = $endDateTime;

    $node = node_submit($node);
    node_save($node);
}

Thanks in advance for your answers.

Upvotes: 0

Views: 1790

Answers (1)

Dromendokter
Dromendokter

Reputation: 35

Let's first make sure that you have configured your field to contain multiple values? -- click "edit" behind your field. -- Verify that "number Of Values" is configured to have more than 1.

Once that is done, you should be able to set the first value by assigning a value to: $node->field_event_dates[$node->language][0]['value'] = "whatever you want it to be". and a second date: $node->field_event_dates[$node->language][1]['value'] = "This is a second value".

etc...

Up until: $node->field_event_dates[$node->language][9]['value'] = "This is the last possible" (assuming you've set max values to 10).

I hope this helps?

Upvotes: 0

Related Questions