Problem/Motivation
Currently all the field blocks in layout builder are being rendered in ---custom view modes. Having field templates for user defined view modes is not possible at the moment, and hence not able to get the required markup for the page when using layout builder.
This is due to usage of \Drupal\Core\Entity\EntityDisplayBase::CUSTOM_MODE
in \Drupal\layout_builder\Plugin\Block\FieldBlock::getFormatter()
Proposed resolution
Force the field blocks to render with the view mode template suggestion that is used at the Layout Builder level
Remaining tasks
Decide if Layout Builder should provide an additional suggestion or just expose the info
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#63 | 3001313--63.patch | 1.01 KB | rlmumford |
#57 | 3001313-56.patch | 20.89 KB | lostkangaroo |
#57 | interdiff-3001313-41-56.txt | 910 bytes | lostkangaroo |
#41 | interdiff-3001313-34-41.txt | 846 bytes | yogeshmpawar |
#41 | 3001313-41.patch | 20.87 KB | yogeshmpawar |
Comments
Comment #2
mikey en CreditAttribution: mikey en commentedComment #3
mikey en CreditAttribution: mikey en commentedComment #4
tim.plunkettThanks for opening the issue! I think we can categorize this as a bug, as it was an oversight when writing the module.
Comment #5
tim.plunkettDo you have a custom
hook_theme_suggestions_alter()
in a module or theme?Because when I inspect the template suggestions I do not see one for the view mode at all:
Comment #6
mikey en CreditAttribution: mikey en commentedYes i do !, i uploaded the picture , it got hidden, sorry about that.
Comment #7
tim.plunkett#3007973: Layout builder prevents the rendering of extra fields (like Links) on pages not using Layout Builder might be a duplicate of this issue, linking so we can check back.
Comment #8
hershey.k CreditAttribution: hershey.k commentedI've applied the latest patch from the referenced related issue and I can confirm that this is still an issue.
Field blocks displayed in different view modes in layout builder all reference a `_custom` view mode inside the $vars['element']['#view_mode'] value. Attempting to create a custom hook suggestions for field_blocks per the host entities view mode(s), something like below inside a hook_suggestions_alter() produces the attached results.
For a use case example: I have a field_block_node_title being printed on the page twice with two different view modes. One for the current nodes title (full view mode). A secondary for the `related content` title (secondary view mode). At the moment there is no way to control a separate theming template, from the field or block level per content (entity) view mode.
Comment #9
tim.plunkettStill needs tests.
And a decision on whether to add this suggestion from within Layout Builder, or just stop short at making it possible to add.
As seen in the patch, this adds the correct information at
I don't think it is possible to change the actual
$element['#view_mode']
key._custom
is technically the correct value at that point, it's just that this is a nested rendering situation...This issue needs at least two issues opened as blockers: one for the change to FormatterBase, one for ContextHandler.
Comment #10
tim.plunkettBlocked by #3029627: FormatterBase should pass along third party settings and #3029625: Do not throw an exception when a context is missing while applying context mapping to a plugin if that context was previously set
Comment #12
fenstratHaving the view mode suggestion in there seems like a pretty common use case. So I think this make sense to provide as a suggestion from layout builder, rather than just making it possible by placing it in
#third_party_settings
.Comment #13
tim.plunkettFair point. This is still blocked by (and includes) those two issues (one RTBC, the other needs tests)
Comment #15
tim.plunkettPostponed on #3029627: FormatterBase should pass along third party settings
Comment #16
bobbygryzyngerReroll against 8.7.x-dev. It looks like several things added in #13 were committed elsewhere.
Comment #17
bobbygryzyngerFixes patch paths.
Comment #18
Cyberschorsch#17 sort of works but there is an issue with actually using the suggestions. The reason is that the suggestions get added to the template suggestions before the system suggestions and will be placed at the bottom of all suggestions.
There are two solutions:
1) Change the module weight of layout_builder so the hook will be executed after the system module
2) Change the hook to hook_theme_suggestions_alter
3) Use hook_module_implements_alter to reorder the modules
Comment #19
CyberschorschI discovered another issue with adding the view mode as context. The module "layout_library" can create layouts without having a view mode as display context. Since we do not provide a default view mode when creating a field block and the context is required, this will break.
I went ahead and added changes from #18 and from this comment to patch #17.
Comment #21
CyberschorschAdding missing interdiff
Comment #22
Petr IllekI've give it a try (patch from #19 with interdiff from #21) and it is working for me, providing me with "field--node--title--page--teaser.html.twig" suggestion. That what I was looking after.
Comment #23
Petr IllekStill working fine after update to 8.7.1.
Comment #24
AaronMcHaleCan confirm, worked for me
Comment #25
tim.plunkettWorking on this
Comment #26
bnjmnmComment #27
bnjmnmAbout this patch
Comment #28
tim.plunkettThanks for picking this up!
I think this is an unintentional revert introduced by a bad reroll, may have been my fault
Why doesn't the default value passed here satisfy this?
Also this should switch to
(new ContextDefinition('string'))->setDefaultValue('default'),
to sidestep all those other paramsAh, maybe this is the part that needs to change. This can be
@ContextDefinition("string", default_value = "default"),
Not 100% clear to me why this is needed, don't the template suggestions handle this for you?
Along with the next patch, can you include a failing patch with just the functional test? Thanks!
Comment #29
bnjmnm#28.1 -- fixed
#28 2/3 setting the default value in the annotation for
DefaultsSectionStorage
removed the need to set a default value in the deriver. I also figured out a way to getFieldBlockTest
to work while making the deriver's view_mode required. This was accomplished by adding a ContextProvider service to layout_builder_fieldblock_test#4
. If the template was provided in a theme, it would automatically be discovered based on filename and directly placement. As far as I know, Drupal will not know to look in the templates directory of a module without an implementation of hook_theme(). There may be a more elegant way to do it, but it doesn't automatically check the contents of the templates dir like it would in a theme.
Comment #30
tim.plunkettAwesome, thanks!
Idk if I highlighted this hunk as well above, but this seems like another instance of passing `default` that seems like it's in the wrong place
If there's no method call on the new object, then you don't need the extra ()
Thanks for clarifying this, can you put an inline comment that says "this is only needed because it's a module, not a theme"?
Comment #32
bnjmnm#30.1
I moved this to what seems like a more appropriate place
Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase::getContextsFromPreview()
and revised the content to better explain what is being addressed. Interested to know if there's a better way to go about this - the problem this addresses can be reproduced by installing Layout Library and visiting an "Edit Layout" page. They Layout library SectionStorage has its own getContextsDuringPreview() method that does not return view_mode and there is not a default defined. Perhaps this is something that could be implemented with a deprecation path but I'm not entirely sure how that would be done.#30.2 Unnecessary parentheses removed.
#30.3 added some clarification to the hook_theme() implementation
Comment #34
bnjmnmThe test failure in #32 was due to OverridesSectionStorage having a view mode context without a default value (which surfaced due to adding a check for view mode in
Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase::getContextsFromPreview()
Although this was addressed by updating OverridesSectionStorage to have a default value in the view mode context, I also added a validation condition inside the getContextsFromPreview() conditional that checks for a view mode context, so it does not break contrib modules with SectionStorage plugins that have view mode contexts without default values.
Comment #35
jonraedeke CreditAttribution: jonraedeke commentedThis patch doesn't seem to work for me. Here's what I get for the body field placed via Layout builder:
When I try to add a suggestion for view_mode, like this:
I get these suggestions:
Comment #36
tim.plunkett$element['#view_mode'];
is not changed in this issue. It will function exactly that way with and without the path.As shown in the patch, it is using
$element['#third_party_settings']['layout_builder']['view_mode']
Curious that with the latest patch,
layout_builder_theme_suggestions_field_alter()
isn't handling it for you. It should be.Comment #37
jonraedeke CreditAttribution: jonraedeke commentedApologies, I was not using the latest version of core. The template suggestion with view mode appears now without the need to use a hook. Thanks for all the work here!
If a themer adds suggestions for fields with view mode added without layout builder, there is still an extraneous entry for layout builder added fields. Maybe that could be unset?
When I try to add a suggestion with view_mode for fields added displayed without Layout Builder, like this:
For a field added with Layout builder I then get:
Comment #38
tim.plunkettThat additional one with all the UUIDs is from the Quick Edit module integration. It can be ignored.
Comment #39
borisson_I only found 2 really small nitpicks that are related to the coding standards. Otherwise I don't see anything with this patch that could be improved.
neeeds addtional newline.
I think this blank line can go. But I am not sure if we have a PHPCS rule for it.
Comment #40
yogeshmpawarComment #41
yogeshmpawarComments addressed in #39 & added an interdiff as well.
Comment #42
borisson_My nitpicks have been resolved, this looks good overall.
Comment #43
tim.plunkettI concur that this is ready to go. Thanks everyone!
Comment #44
matt_paz CreditAttribution: matt_paz commentedHmm. It seems I must have misattributed this patch as the source of issues in #3063081.
Comment #45
matt_paz CreditAttribution: matt_paz commentedRestoring status after recognizing my error.
Comment #47
borisson_I think that test failure is unrelated as it was in a media js-test.
Comment #48
larowlanissue credits
Comment #50
larowlanCommitted 90534ac and pushed to 8.8.x. Thanks!
I'm of the opinion that this is not eligble for back-port to 8.7.x because it could have unintended consequences to templates/markup - see allowed changes - but its a grey area so I'll put it to a poll of other committers
Comment #51
larowlanI realise this has already gone into 8.8 - but could I request a posthumous change record, it would help with the decision around backporting.
Comment #52
tim.plunkettThis doesn't work on 8.7 because #3029627: FormatterBase should pass along third party settings wasn't backported. Reopened that one
Comment #53
annisar CreditAttribution: annisar commentedCan confirm when using both the patches , https://www.drupal.org/files/issues/2019-06-17/3001313-41.patch and https://www.drupal.org/files/issues/2019-04-12/3029627-tps-10.patch works in 8.7x for generating the view mode suggestions !!
Comment #54
larowlanCan we get a change notice here, if this is to be backported, we need to let people know - thanks
Comment #55
larowlanFor #54
Comment #56
lostkangaroo CreditAttribution: lostkangaroo at R2integrated commentedUsing the patch in #41 was still allowing failures of view_mode context using 8.7.x. This updates #41 to ensure a default value when getting derivative definitions.
Comment #57
lostkangaroo CreditAttribution: lostkangaroo at R2integrated commentedFixes line endings for files in #56.
Comment #59
AnybodyComment #60
AnybodySORRY - my stupid mistake, I didn't see this was already committed and you were just discussing the backport to 8.7.x!
Now where 8.8.0 is out - should we close this FIXED?
Comment #61
tim.plunkettAgreed.
Comment #62
rlmumfordThis fix is causing problems for users of the mini_layouts module - hopefully someone can offer some advice.
https://www.drupal.org/project/mini_layouts/issues/3110850
https://www.drupal.org/project/mini_layouts/issues/3112090
People often create mini layouts with a required context or two and then use FieldBlocks to render fields. These mini layouts can then be placed as a block anywhere else. This fix has caused all of those mini_layouts to throw fatal errors because the 'view_mode' context is not satisfied.
I can't work out what the appropriate way forward is - do mini layouts users have to define two required contexts for each entity they're using in their mini layout? That seems like an uncomfortable burden to place on the users. Can we make it so that, if there isn't a view_mode context, the default view mode (or even we revert to the 'custom' behaviour as before!) is used rather than throwing an error?
Comment #63
rlmumfordI know this patch will probably need a seperate issue to get committed, but I think this is probably all it needs to avoid breaking old mini_layouts. Just set the default value of the context definition to 'default'.