I have a date field, but I only want to collect the year and month, not the day. How can I hide the day part?
There are instructions for Drupal 7, but I am using Drupal 8.
I have a date field, but I only want to collect the year and month, not the day. How can I hide the day part?
There are instructions for Drupal 7, but I am using Drupal 8.
Note that the day is required.
This means there are some difficulties:
unset()
or #access: FALSE
because this will cause the form to have no value and fail validation.You can do this in an #after_build
callback.
function MYMODULE_form_MYFORM_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#after_build'][] = 'MYMODULE_date_remove_day_after_build';
}
function MYMODULE_date_remove_day_after_build($form, &$form_state) {
$form['field_MYFIELD']['widget'][0]['value']['day']['#value'] = '15';
$form['field_MYFIELD']['widget'][0]['value']['day']['#attributes']['class'][] = 'element-hidden';
return $form;
}
And then in your theme .css, add this class:
.element-hidden {
display: none !important;
}
Not sure exactly but if I understand your question correctly...
One approach might be through templating. In your template file try printing this:
{{ content.field_name.0.value | date('F j - h:i a') }}
I am not sure what this would actually be, use {{ dump(content | keys) }} in your template file to see what array keys are available.
This method, you are able to apply the date filter to the field time value which allows you to use php date formatting... you can then render the timecode any way you want.