Search code examples
datedrupal-7data-migrationdrupal-field-api

adding data to a date field attached to a node in drupal 7


I need to add data to a datetime field in drupal 7. I am tryin to use

$node->field_test_a_updated[0]['value'] = $val;
$node->field_test_a_updated[0]['delta'] = 0;
$node->field_test_a_updated[0]['timezone'] = 'UTC';
$node->field_test_a_updated[0]['timezone_db'] = 'UTC';
$node->field_test_a_updated[0]['date_type'] = 'datetime';

where $val has the value "2010-06-15T00:00:00-00:00".

When i try to import the content, all the other fields attached to the node get migrated properly, except the date field.I have also tried using [LANGUAGE_NONE] option.

I am sure i am missing out something that is related to drupal7 field api.

Please help.


Solution

  • The structure of fields in Drupal 7 (in this context) is:

    array(
      'language_code' => array(
        0 => array(
          'value => $val,
          'other_column_value' => $other_val
        )
      )
    );
    

    The delta is handled by the key of each of the arrays inside $array['language_code'] so you don't need to include it. In your case you want the code to look like this (assuming of course you're passing the node through node_save() afterwards):

    $node->field_test_a_updated[LANGUAGE_NONE] = array(
      0 => array(
        'value' => $val,
        'timezone' => 'UTC',
        'timezone_db' => 'UTC',
        'date_type' => 'datetime'
      )
    );
    

    Hope that helps