Provide "Date only" widget in form display for date type fields

Created on 28 August 2023, 9 months ago
Updated 20 March 2024, 2 months ago

Problem/Motivation

It is currently impossible for "Date" type fields to have regular <label> + <input type="date"/> in the render of form display.

Steps to reproduce

First of all steps to reproduce expected behavior:
1. Standard profile installation
2. Enable debug mode here /admin/config/development/settings
2. Go to CT Article fields
3. Add new field with type Text (plain)
4. Then go on this page/node/add/article
5. Inspect final render of your newly created field
6. There is regular form-element.html.twig with form-element-label.html.twig and input.html.twig inside.

Now steps to reproduce problem with date type field:

1. Standard profile installation
2. Go to CT Article fields
3. Add new field with type Date
4. In the field settings Date type choose Date only
5. Then go on Form display page of the CT Article
6. Here is the first problem. Inspect widgets of the date field -> there is (at least one of the available widgets) Date and time widget. Already strange no? Because in field settings we picked Date only, so we don't have / don't need time.
7. You can try this widget (or other widgets available if you have), but the thing is next: there is no way to reproduce same render as we reproduced with Text (plain) field type for example. There is no way to reproduce regular pair <label><input>

Proposed resolution

1. I'm thinking that maybe showing widget Date and time for the field with Date only is a bug, so probably we should do something with it.
2. For the widget which can display regular label + input -> need to create new widget which allows to do it.
3. Another option is to stop using datetime_wrapper theme hook and instead use standard form_element hook.

Feature request
Status

Needs work

Version

11.0 🔥

Component
Render 

Last updated about 17 hours ago

Created by

🇷🇺Russia kostyashupenko Omsk

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

  • Issue created by @kostyashupenko
  • 🇷🇺Russia kostyashupenko Omsk

    I discussed with @sorlov a lot regarding this subject, here are several our thoughts:

    1. From my personal point of view it was initially wrong idea to combine time and date into field type Date. It would be make sense to have Datetime field type for such cases.

    2. Moreover - we have field type called Timestamp. Try to add new field with such field type and find 10 differences from field type Date (in terms of render).

    3. If you will inspect form display render of Date type field with Date only field setting enabled - you will see datetime_wrapper with title and form_element. Title is in <h4> tag and this tag is hardcoded in twig template. It points me to the thought that there can be potentially SEO risks. But that's not all -> if we have Date only field setting enabled, why we need datetime-wrapper in that case? The easiest (and obvious) way is to just display label + input. Right now we have datetime-wrapper always applied, no matter if setting Date only enabled or not. Moreover - visually maybe h4 title is looking like a label, but actually it's not. It doesn't have label html tag and it's not clickable (but it should be clickable to get focus on the input element).

    Now what we could do with all that. Several proposals:

    1. We could rewrite field types and have 3 different field types for different needs: Date (showing date input only, always label + input html tags), Time (for showing input time only + label), Datetime (when we need both, and keep datetime-wrapper for that).

    2. We could kick somehow datetime-wrapper wrapper for existing Date field if setting Date only enabled. And show only regular label + input html tags.

    3. We could keep everything as it is, but only remove Title from datetime-wrapper and unhide original label html tag (which is hidden right now).

  • 🇷🇺Russia kostyashupenko Omsk

    Tried webform module with Date element as a reference.

    This is exactly render i want to have for the Date only field:

  • 🇷🇺Russia kostyashupenko Omsk

    Also they are splitting Date and Date/time elements

  • Status changed to Needs review 9 months ago
  • last update 9 months ago
    30,056 pass, 2 fail
  • 🇷🇺Russia sorlov

    Several proposals:
    ....
    3. We could keep everything as it is, but only remove Title from datetime-wrapper and unhide original label html tag (which is hidden right now).

    This can be done as quick fix on Drupal\Core\Datetime\Element\Datetime to show as Title for Date sub-element instead of rendering title in datetime-wrapper theme template

    Attached patch with this solution

  • Status changed to Needs work 9 months ago
  • 🇬🇪Georgia maximkashuba Batumi

    Labels for inputs are always present, but can be visually hidden (accessible to screen readers) if the field has multiple cardinality.

    There are 2 scenarios: single and multiple cardinality field. For multiple fields the labels are visually hidden by the WidgetBase class, but for fields with a single element, the label is displayed as required by the task.

    The Datetime field is flexible and can have 2 variants: date (1 input) or date and time (2 inputs), additionally it has own custom form render element that results in a slightly different rendering order, adds #title_display => 'invisible' property and misses logic of WidgetBase class hiding labels for multiple fields.

    So there are options:

    1. Single date field/show label
    2. Multiple date fields/hide label
    3. Single date and time field/hide labels
    4. Multiple date and time field/hide labels

    An additional example of a multiple simple text field is attached.

  • last update 9 months ago
    Patch Failed to Apply
  • 🇬🇪Georgia maximkashuba Batumi

    Labels for inputs are always present, but can be visually hidden (accessible to screen readers) if the field has multiple cardinality.

    There are 2 scenarios: single and multiple cardinality field. For multiple fields the labels are visually hidden by the WidgetBase class, but for fields with a single element, the label is displayed as required by the task.

    The Datetime field is flexible and can have 2 variants: date (1 input) or date and time (2 inputs), additionally it has own custom form render element that results in a slightly different rendering order, adds #title_display => 'invisible' property and misses logic of WidgetBase class hiding labels for multiple fields.

    So there are states:

    1. Single date field/show label
    2. Multiple date fields/hide labels
    3. Single date and time field/hide label
    4. Multiple date and time field/hide labels

    An additional example of a multiple simple text field is attached.

  • last update 9 months ago
    30,056 pass, 2 fail
  • Status changed to Needs review 9 months ago
  • Status changed to Needs work 9 months ago
  • 🇷🇺Russia sorlov

    Changes in datatime-wrapper template also affect datelist form element and make ElementsLabelsTest::testTitleEscaping() test fail for it

  • 🇷🇺Russia ilya.no

    Problem is that if we use date fields on custom form, then they don't have titles with current patch. For testing I've copied FormTestLabelForm into custom module and saw, that only `date` form element has title. That's why test fails.

    My solution is to use `form_element` as theme wrapper instead of `datetime_wrapper`, because we need usual label and input HTML structure. In my patch I remove occurencies of `datetime_wrapper`. Failed test runs fine locally after my update. I've also checked all 4 cases from #11 comment on node add form and titles are fine.

  • Status changed to Needs review 7 months ago
  • last update 7 months ago
    30,438 pass
  • 🇺🇸United States smustgrave

    If the solution is going to change the issue summary will have to be updated.

    Also a CR will probably be needed for other themes to delete their templates.

  • Status changed to Needs work 7 months ago
  • 🇺🇸United States smustgrave

    Oops think I meant to set to NW.

  • Status changed to Needs review 7 months ago
  • 🇷🇺Russia ilya.no

    I've been pointed by @sorlov to the issue with duplicating title for date only case, so attaching another patch with the fix for datetime field widget. It adds logic to use title_display setting from the element, if there is no such setting already.
    I've updated issue summary, but for the change record, I think, that we need to agree on one of ours solutions first and then add CR if necessary.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 7 months ago
    30,483 pass
  • Status changed to Needs work 7 months ago
  • 🇺🇸United States smustgrave

    Looking at the IS there are 3 solutions, could it be highlighted which approach is being proposed.

    Also with template changes will need a CR.

  • 🇮🇹Italy FiNeX

    Hi, I've found a couple of related issues which could be considered when fixing this request.

Production build 0.69.0 2024