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;
}