2

i'm using drupal 7 and in my content type there is a date field has start and end value with unlimited values, i set the default value to "Now".
but the problem is when adding new item of this field there is no default value in it.

is there a way to set a default value for new items?
thanks a lot.

  • Did you ever discover a solution to this? I am experiencing the same issue. – user1055810 Jan 15 '13 at 17:22
  • not yet :( sory ... let me know if you fine some one – abd Jan 16 '13 at 21:08
0

Try something like this:

//this hook or general hook_field_widget_form_alter
function hook_field_widget_date_popup_form_alter(&$element, $form_state, &$context){

  //only when field is specific name and during node creation form
  if ($element['#field_name'] == 'field_date' && !isset($element['#entity']->nid)) {

    //if default value is not set, only for second and later elements, when first item value is set
    if (!is_array($element['#default_value']) && $element['#delta'] > 0 && isset($form_state['values']['field_date']['und'][$element['#delta']-1]['value'])) {

      //set value
      $element['#default_value'] = array(
        //advance by 24 hours
        'value' => $form_state['values']['field_date']['und'][$element['#delta']-1]['value'] + 24 * 3600,
        //same timezone
        'timezone' => $form_state['values']['field_date']['und'][$element['#delta']-1]['timezone'],
        'timezone_db' => 'UTC',
        'date_type' => 'datestamp',
      );

      //if second value (date to) is set (by comparing value and value2)
      if ($form_state['values']['field_date']['und'][$element['#delta']-1]['value2'] != $form_state['values']['field_date']['und'][$element['#delta']-1]['value'])
        //set and advance second value by 24 hours also
        $element['#default_value']['value2'] = $form_state['values']['field_date']['und'][$element['#delta']-1]['value2'] + 24 * 3600;

      //#date_items needs to be same as #default_value
      $element['#date_items'] = $element['#default_value'];
    }
  }
}

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.