6

How can I set the default date for a date field in hook_form_alter().?

I tried:

$form["field_price_date"]["und"][0]['value']['#default_value'] = $curDate;
$form["field_price_date"]["und"][0]['value']['date']['#default_value'] = $curDate;
$form["field_price_date"]["und"][0]['value']['date'] = $curDate;           
$form["field_price_date"]["und"][0]['value'] = $curDate;

None of the above work.

1

5 Answers 5

5

The date field's a funny one, normally the field column name is the last in the array (like the first example you tried), and the default value is set on that.

For a date field though, the default value is an array, and it's one level further up, e.g.:

$form["field_price_date"]["und"][0]['#default_value']['value'] = $curDate;

For future reference, install the Devel module and use the dpm() function to print a breakdown of the form array to the screen (dpm($form);). It makes debugging this kind of thing so much easier.

1
  • Thanks a lot. It works. I'll install devel module. thanks again for the advice..
    – rongok
    Commented Aug 31, 2012 at 18:30
2

On Drupal 8, here's an example to set a specific date time field value in a hook form alter :

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;  

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
  $date = strtotime(date('Y-m-d 00:00:00'));
  $form['field_datetime']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp($date);
}
1

I was inspired by this response to try the Devel module to solve a similar problem. I tried it and it worked. I posted detailed instructions on how to obtain the field array structure for a field in order to modify it using hook_form_alter here: https://drupal.org/node/1243628#comment-7777329

I hope this helps someone the way the Devel module suggestion helped me.

0

This is the function I use to print the structure:

// Custom print for testing purposes
function prnt($element){
  print "<pre>";
  print_r($element);
  print "</pre>";
}
0

Please see below methods to set a value to a date field

Using hook_form_alter

$form["field_price_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_price_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_price_date['und'][0]['value'] =  $test_date ;
   }
}

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

1
  • can call the hook_form_submit value in hook_node_presave ? Commented Sep 28, 2017 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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