0

The date module gives the possiblity to collect an end date.
The thing is, in the node creation form, 'Show end date' is checked by default and the date calendar popup field is displayed.
As shown in this screenshot

I would like to have the opposite scenario.
With the End date field hidden by default, and it would only appear if we check the 'Show end date' checkbox.
As shown in this screenshot

Thanks to anyone who can help with this

1 Answer 1

1

We can do this easily using hook_form_alter hook, by setting #checked value of checkbox to FALSE. Since the date module doesn't add both fields at initial form load, we've to add #after_build to form where we can modify #checked value of checkbox (i.e. show_todate field) in form.

Below is the sample code for hiding the end date in date field. For this we've to add a Date Type field (field name as field_date) with ability to add end date in article Content Type. After which we've to implement hook_form_alter and checked if current form is add new article form to make sure we don't override node edit field value. And then added #after_build to add article form. Which then set the #checked value to FALSE.

/**
 * Implements hook_form_alter().
 */
function [MODULE_NAME]_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'article_node_form':
      if (empty($form['nid']['#value'])) {
        $form['#after_build'][] = '_modify_end_date_form';
      }
      break;
  }
}

/**
 * Proxy function to call _modify_end_date_form_after_build().
 * @param type $form
 * @param type $form_state
 * @return void
 */
function _modify_end_date_form($form, &$form_state) {
  return _modify_end_date_form_after_build($form, $form_state);
}

/**
 * After_build callback for Article Content Type.
 * @param type $form
 * @param type $form_state
 * @return void
 */
function _modify_end_date_form_after_build($form, &$form_state) {
  $form['field_date'][LANGUAGE_NONE][0]['show_todate']['#checked'] = FALSE;
  // $form['field_date'][LANGUAGE_NONE][0]['show_todate']['#value'] = FALSE;
  return $form;
}
4
  • When implementing the first form_alter hook what module name would you target ? I tried _modify_end_date_form_alter and since i want this in the event content type, i replaced article_node_form with event_node_form but nothing happens. I'm doing something wrong but i don't know what. I really appreciate you taking time to help @Yogesh !
    – amstram
    Sep 23, 2016 at 13:49
  • Module name is the module file name, i.e. if your module file name is drupal_custom.module then you've to use drupal_custom_form_alter as function name. After implementing this clear site cache. Other things are correct.
    – Yogesh
    Sep 23, 2016 at 16:40
  • Well it's not working. Maybe i should've mentioned that i only know very basic php and I've never opened a module file or delved into drupal core. So it's like i'm discovering a totally new dimension here. Everything looks exciting, scary and mysterious at the same time. Where should i paste those 3 functions ? I tried putting them into date.module and named the first module name date_form_alter but it doesn't seem like a good idea. I tried _modify_end_date_form_alter but it's not good either. Sorry for being this bad. I'm learning :)
    – amstram
    Sep 23, 2016 at 20:27
  • @amstram, No worries, check out this documentation on Creating custom modules. This will give you some insights on basics of module in Drupal. hook_form_alter is hook, check how to implement guide here. Keep _modify_end_date_form and _modify_end_date_form_after_build function as it is, as these are custom functions written by us.
    – Yogesh
    Sep 29, 2016 at 9:06

Your Answer

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

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