Commit 37ced362 authored by Lily Yan's avatar Lily Yan
Browse files

ISTWCMS-4007 Remove empty widget of event date field

parent b7293594
......@@ -17,3 +17,34 @@ function uw_ct_event_preprocess_block($variables) {
unset($variables['content']['#view']->exposed_widgets);
}
}
/**
* Implements hook_form_alter().
*
* Remove the extra empty date widget
*/
function uw_ct_event_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Remove the second date widget when add new event date.
if ($form_id == 'node_uw_ct_event_form') {
// Remove the second empty widget.
unset($form['field_uw_event_date']['widget'][1]);
}
// Remove the extra date widget when edit the exist event node date field.
if ($form_id == 'node_uw_ct_event_edit_form') {
// Get all widgets when loading edit event node page.
$widgets = $form['field_uw_event_date']['widget'];
// Go through all widgets
foreach($widgets as $key => $widget) {
// Get the empty widget id.
if (isset($widget['value']['#default_value']) && empty($widget['value']['#default_value'])) {
$extra = $key;
}
}
// Remove the empty widget.
unset($form['field_uw_event_date']['widget'][$extra]);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment