2

I need to set a value for a date field with type datetime in hook_form_alter. I tried with below codes. But it is not working.

$form["field_event_date"]["und"][0]['#default_value']['value'] = $new_date;
$form_state['values']['field_event_date']['und'][0]['value']['date'] = $new_date;
$form['field_event_date'][LANGUAGE_NONE][0]['#default_value']['value'] = $new_date;

This $new_date will be a value from another field in this form and it should be in '2014-09-23 00:00:00' format. And 'field_event_date' will be a hidden field.

I can save the value to this field using the node_presave function. Please see the below code. If anyone trying to save a value to a date field with a static value, you can use this method.

function MY_MODULE_node_presave($node) {
     $node->field_event_date['und'][0]['value'] =  '2014-09-23 00:00:00';
}

But in my case, I need to get the value from a field that defined in the form alter. And need to set this value to the field_event_date. Can anyone help me on this? Is there any way to get a custom field value in node_presave function?

  • 1
    What is the function name of the hook you're using? Is it a node edit/create form that you're trying to modify? Did you clear cache and enable the hook module? – Johnathan Elmore Sep 20 '14 at 5:21
  • 1
    @JohnathanElmore Function name is MY_Module_form_alter and this is a node edit/create form. – Aswini K Sep 20 '14 at 5:35
  • 1
    try the date format like "YYYY-MM-DD" only. – Sumit Madan Sep 20 '14 at 6:37
2

To set a value to a date field follow below codes.

Using hook_form_alter

$form["field_event_date"]["und"][0]['#default_value']['value'] = $new_date;

Using hook_node_submit or hook_node_presave (Saving the value to the database)

 function MY_MODULE_node_presave($node) 
  {
      $node->field_event_date['und'][0]['value'] =  '2014-09-23 00:00:00';
  }

Or if you need to save value of one custom field(defined in hook_form_alter) to another, see below codes.

function MY_MODULE_node_submit($node, $form, &$form_state) {
  if($node->type == 'my_node_type') {
    $test_date =  $form['test_date ']['#value'];
    //Ensure that $test_date format is '2014-09-23 00:00:00'
    $node->field_event_date['und'][0]['value'] =  $test_date ;
   }
}

I think it may help anyone who is searching for the solution.

1

It depends upon the format you are providing to save the date, whether it is unix or formatted date. Pick one already saved node using node_load($nid) in your form_alter function or in some other testing page and check the format the data is saved.

    $nodesaved = node_load($nid); //e.g. $nid = 10
    echo "<pre>";
    print_r($nodesaved);
die;

Now pass the same format statically and then make it dynamic. I think issue is with the format you are providing. Usually it is in unix, it converts automatically in the display

Hope it will help!

  • I had checked the format of date and it is correct. And I tried with a static date also. But not working. – Aswini K Sep 20 '14 at 5:55
0

I got it worked using hook_node_submit function. Let test_date is a custom select field that list dates and this is defined in hook_form_alter. And to set and save the value from this date field to another field field_event_date(defined in field base and instance). Please follow below codes.

function MY_MODULE_node_submit($node, $form, &$form_state) {
  if($node->type == 'my_node_type') {
    $test_date =  $form['test_date ']['#value'];
    //Ensure that $test_date format is '2014-09-23 00:00:00'
    $node->field_event_date['und'][0]['value'] =  $test_date ;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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