Problem/Motivation
As explained in the parent META issue #2353867: [META] Expose Title and other base fields in Manage Display, the node template directly includes markup for the title and submitted information. This is not compatible with enabling these base fields to be configurable via the field UI. The same is true for various other entity type base fields.
As part of the solution for fixing that, we need a mechanism to reproduce a comparable and equivalent appearance. Plus we need this mechanism to be very easy to enable/configure without adding any extra dependencies. There are two cases that together can solve all core entity types looking pretty close to how they do already (excluding the aggregator module due to #3267458: Deprecate aggregator module in Drupal 9.4).
- Title: with configurable tag and option to link to the entity. Used on many entities.
- "Submitted": based on the "Author" formatter with additions. There is an optional user picture, and a wrapping sentence "Submitted by UUU on DDD", which includes the creation date. Used on node, comment, and could also apply to media.
Proposed resolution
There are many possible ways to solve this (see below under remaining tasks for other possibilities). For this issue, we need something simple, generating close to equivalent markup, and without forcing extra dependencies (such as Layout Builder). This unblocks the path forward for #3036862: Expose Title and other base fields in Manage Display in Drupal Core and #3176673: Deprecate non-standard display of title and other base fields - we can only deprecate the old way once we have created a new equivalent.
Custom formatters seem like the right answer.
- They can be enabled/configured quickly in the UI.
- It's possible to use them as the default in
baseFieldDefinitions(),entity_form_display.*.yml, or custom hooks. - If a site doesn't want to use them, they don't get in the way.
- They support powerful customisation with templates and hooks.
- The code is simple and already written/tested in the contributed module Manage Display.
Copy the Title and Submitted formatters into core. Not much needs changing except to add tests.
Remaining tasks
We can cover some more advanced and sophisticated options for "Submitted" in follow-on issues. People can add many other possibilities in contrib modules. The [italic bracketed text] explains why each one wasn't chosen for this issue.
- Create a custom Layout Builder block. [Forces use of Layout Builder]
- Use
hook_entity_extra_field_info(). [See below] - Create a general mechanism for combining fields?? [Longer-term initiative that will take time]
The extra field option could work, but when examined in detail, it seems less suitable for "Submitted". It could be a good option in a Contrib module.
- Extra fields don't currently allow settings (in particular to configure the user picture).
- The extra field would combine/wrap the output of the underlying fields (uid and created). It works best if they don't have configurable display - which would not be acceptable for Core. (Reasons are: the extra field relies on the underlying fields having specific display settings; having the extra field and the separate fields in Manage Display is confusing.)
Comments
Comment #2
panchoJust to make sure nothing gets lost.
In #2923701-283: Mechanism to disable preprocessing of node base fields so they can be configured via the field UI, @plach proposed A: Layout builder
In #287, @AdamPS proposed B: manage_display
In #289, @plach answered re manage_display
In #292, myself answered re Layout builder:
and re manage_display:
and instead proposed C: config extra fields:
In #293, @AdamPS replied:
In the end, I guess, whatever gets most traction and is doable within due time (ahead of D8 LTS) should be the way to go.
Comment #3
panchoFollowing #2953656: No ability to control "extra fields" with Layout Builder, layout builder can cope with extra fields now, so a one-off extra field would be among the easiest solutions.
However, what I was thinking about is a way to create and edit extra fields in the UI (Core case would be: to combine two fields into one with some extra text). My idea was that they would live in a particular form resp. view mode, just as fieldgroups do.
Let's figure out if that's doable in a very lightweight yet flexible way.
Comment #6
adamps commentedComment #10
aaronmchaleI like the idea of a "submitted" computed field, or even just a new field type for "submitted", something that other entity types can easily reuse.
Comment #12
catchhook_entity_extra_field_info()/computed seems like it might be a decent option, and could be used for any entity that both has published and author fields via respective interfaces? Does that get us a layout builder block 'for free', not sure how layout builder handles extra fields..
Another issue is the author picture on comments which is also in preprocess.
Comment #13
aaronmchaleI think it does, LB seems to expose all of the available fields if I remember correctly.
Comment #14
adamps commentedThere are very many possibilities. Some almost work already in Core (Layout Builder?); some are available in contrib modules (Manage Display); some are more likely a longer term initiative (general mechanism for combining fields?). This issue could easily get stuck in an enthusiastic debate about the merits of each one - so how can we avoid that?
Well we could look at this issue as the minimum simple outcome that would allow the initiative #2353867: [META] Expose Title and other base fields in Manage Display to move forward. People can add more sophisticated mechanisms in other issues, or in contrib.
My feeling is that the answer is the two custom formatters: title and submitted. I've updated the IS in that direction. Please can people take a look and make comments?
Comment #15
geek-merlinThanks @AdamPS for elaborating that.
+1 for dont-use-extra-fields. They are a very odd child, they don't even have labels builtin.
The reasoning in the IS makes perfectly sense to me, and unless we get a better contra-reasoning, i fully support the proposed approach.
Comment #16
catchThe only issue with the submitted formatter for uid is that while it applies to the uid field, it's pulling information from other fields it has to assume exist. I installed manage_display to try things out and this caught me out for a second (was looking for an 'extra field').
However, we can make it available only on entity types that have those fields (in core it'd be nodes and eventually comments too), it's much less weird than the hard-coded preprocessing + template variable.
So I think overall it's a big net UX and API improvement, even if we might want to open follow-ups to add another mechanism later. We definitely shouldn't postpone it on some kind of 'combine fields' system.
Comment #17
adamps commentedThank you both for the comments.
It should work on Core node, comment and media (not even "eventually" - it should work right now).
If the created field is not present then the formatter still works, it just misses out the "on " part.
I am currently planning to make two enhancements to the code in SubmittedFormatter:
Comment #21
catchComment #22
adamps commented