Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Coming from #1510544: Allow to preview content in an actual live environment
No matter how many view modes I check off under admin/structure/types/manage/article/display for Articles, the preview is still only letting me select between Default and Teaser. By design, or is this a bug?
getDisplayModeOptions() should not look at the status key of the view mode config entity, but that from the entity display ?
Comment | File | Size | Author |
---|---|---|---|
#82 | getdisplaymodeoptions-2322503-82.patch | 18.91 KB | swentel |
#82 | interdiff.txt | 2.75 KB | swentel |
#78 | getdisplaymodeoptions-2322503-78.patch | 16.17 KB | nlisgo |
#78 | interdiff-2322503-75-78.txt | 3.65 KB | nlisgo |
#75 | getdisplaymodeoptions-2322503-75.patch | 12.52 KB | nlisgo |
Comments
Comment #1
swentel CreditAttribution: swentel commentedOk, nevermind, plopesc did some research and this behaves as intented.
Comment #2
olli CreditAttribution: olli commentedI think this is a bug. Currently you can't choose a custom block to render in "Full" mode because we are checking the
status
that is "Whether or not this form or view mode has custom settings by default."Comment #3
plopescAs @olli said in #2, the
EntityManager::getDisplayModeOptions()
method has a $include_disabled parameter, described as:This is not right, given that the meaning of the status property of the entity type is not related to the display mode status. It means "Whether or not this form or view mode has custom settings by default."
The desired status is not available at this level, given that display modes can be enabled or disabled at bundle level.
Here I'm proposing a new approach after reviewing all the places where
EntityManagerInterface::getViewModeOptions()
andEntityManagerInterface::getFormModeOptions()
are invoked.$include_disabled
prameter from both methods above, given that is uselessBlockContentBlock::getViewModeOptions()
where available view modes are displayed in the edit block form. Other places where these methods were called before are now OK with the new implementation where$include_disabled
is removed.Other approach could be add new methods
EntityManagerInterface::getViewModeOptionsByBundle($entity_type_id, $bundle_id, $include_disabled = FALSE)
where we could return this list. We could reuse this method in some places in the core code where this list is loaded like in the Manage Display pages or in the new node preview page select list.Any thoughts?
Regards
Comment #5
swentel CreditAttribution: swentel commentedI think getViewModeOptionsByBundle would make more sense so we can kill code in various places. If I remember correctly, the original goal of this function was to reduce/remove a lot of duplication we already had re: options and view modes. It just didn't to it right :) Introducing new methods again seems overhead and also cumbersome for contrib.
Comment #6
plopescHere is the new patch following the new methods approach.
Regards.
Comment #8
plopescSorry, forgot to include Test file changes and NodePreviewForm specific changes.
Regards.
Comment #9
plopescRenaming variables.
Comment #10
plopescPatch re-rolled
Comment #11
olli CreditAttribution: olli commentedComment #14
plopescNew version of the patch.
Comment #17
yannickooComment #18
DuttonMa CreditAttribution: DuttonMa commentedComment #19
DuttonMa CreditAttribution: DuttonMa commentedComment #20
mrjmd CreditAttribution: mrjmd commentedRe-roll attached. There were a bunch of conflicts but I think they're properly resolved. The entire relevant part of /core/modules/field/src/Entity/FieldConfig.php has been removed in HEAD, so that part of the patch from #14 is gone.
Comment #22
axe312 CreditAttribution: axe312 commentedJust tested the patch with simplytest.me
I can now select all available view modes in the entity reference field formatter and also have them available in views. (getDisplayModeOptions() is used over there)
Also a fresh created view mode appears in the dropdown. Looks good so far :)
The tests for the custom block view mode selection fails. I also could not see any view mode selection fields over there. Does anyone have more infos about that? If the feature is just not implemented yet, we might be RTBC here.
Comment #23
olli CreditAttribution: olli commentedChanging priority to major because this affects views, custom block, entity reference and the node preview.
Comment #24
kerby70 CreditAttribution: kerby70 at Blink Reaction (now part of FFW) commentedPatch wouldn't apply. Here is a quick reroll.
Comment #25
axe312 CreditAttribution: axe312 commentedComment #27
axe312 CreditAttribution: axe312 commentedComment #28
siva_epari CreditAttribution: siva_epari commentedPatch rerolled.
Comment #30
axe312 CreditAttribution: axe312 commentedThank you for that quick reroll epari.siva
We still do not pass the same tests. Does anyone have any informations about the block view mode switch functionality? Imho this patch will not solve this problem, so we might be already RTBC here.
Comment #31
Nick_vhSpacing issues here still
Going to see if I can reroll this so we can move this along. Search API is blocked because this is not available in core. See issue #2471669: Views should show the view mode for each bundle as view modes are configured per bundle
Comment #32
Nick_vhrerolling and rerunning tests
Comment #33
Nick_vhI do confirm that by using this patch, we would be unblocked from our issue in Search API. Awaiting the tests and will try to work through them
Comment #35
Nick_vhThe test had wrong assumptions, but the patch is actually fine. Uploading a fixed version. It took me ages before I realised the tests were in a bad shape instead of the patch...
Comment #36
Nick_vhLooking better here. If someone can review this at Drupal Dev Days and commit this I'd be super happy so it can unblock Search API Views integration.
Comment #37
yched CreditAttribution: yched commentedThat phpdoc needs to be adjusted, this is not about disabled view modes anymore. This is about filtering out modes for which the *display* is disabled.
Comments and var names here are confusing - this is loading entity displays (EntitiyViewDisplay / EntityFormDisplay), not modes (EntityViewMode / EntityFormMode).
Also : you only need to load the displays if $include_disabled is FALSE, right ?
The 'default' display is not supposed to ever have its 'status' set to false, but for safety, you should always inlude that one in the list without checking its status.
It should always be in the list, because at display-time it's always active as a fallback.
Other than that, not a fan of adding two new methods, a new __construct() dependency and yet more code in the EntityManager that's already 1300 lines long, but doing otherwise would require a refactor that's probably outside the scope of this patch. Opened #2472013: Move view_mode / display handilng code out of EntityManager and into a new EntityDisplayManager
Comment #38
swentel CreditAttribution: swentel commentedChanged the comments, and some other small nitpicks, should be better I think.
Well, we need to load them all to check their status, no ?
Comment #39
yched CreditAttribution: yched commentedBut you only actually need to check the status if you don't want disabled ones ☺
Comment #40
swentel CreditAttribution: swentel commentedRight, DUH - but we shouldn't care when it's TRUE :)
Comment #41
Nick_vhI think this is a bit cleaner in how the Default option is being set.
Comment #42
Nick_vhUploading interdiff for patch #41
Comment #43
swentel CreditAttribution: swentel commentedNitpick: missing point at the end
Nitpick, uppercase start of 'unset'
Maybe rephrase to something like 'Add the default mode if we have more than 1 option' ?
Can't we just do t() directly here instead of the next line ?
Comment #44
Nick_vhCleaning up
Comment #45
yched CreditAttribution: yched commentedI think we can still simplify getDisplayModeOptionsByBundle() quite a bit :-)
This code could move in the if (!$include_disabled) { code branch
Also, minor : I'd suggest to simplify/adjust var names a bit :
s/$display_mode_options/$options
s/$display_type_entity_name/$display_entity_type_id
s/$entity_displays/$displays
+ remove $entity_type, inline it within the getConfigPrefix() line ?
Nitpick : "Collect" rather than "Load" ?
Comment needs to be adjusted, the "always include default" part is not done in this code block anymore.
Meaning, that comment line could go away IMO :-)
Actually, this could now directly do $options = $this->getDisplayModeOptions($display_type, $entity_type_id) ?
We are in the EntityManager, so it should be $this->getStorage($display_type_entity_name)->load(...)
could be inlined, no need for the $display_mode_name var ?
Likewise, comment needs updating - the options are already generated, this code block removes some of them if needed.
Comment #46
Nick_vhAll comments were incorporated except for number 4. If we use that one, we have to start fiddling with the default and unset it if there are only 2 in the end. That seems unnecessary and not very clean to do.
Comment #49
yched CreditAttribution: yched commentedNeeds reroll ?
Otherwise looks good, thanks @Nick_vh !
Comment #50
swentel CreditAttribution: swentel commentedrerolled
Comment #51
yched CreditAttribution: yched commentedOops, sorry :
Actually, since the code now collects all possible view modes just above, no need for $this->configFactory->listAll() and the prefix dance anymore, we can just do :
And we can thus revert the added dependency on the @config.factory :-)
Then once we have the $displays, there's a catch I didn't spot earlier : the display for a given $mode might not exist yet, and in our case we have to treat that as if it was disabled.
So the code should be :
Comment #52
Nick_vhCleaned up the code and the default is now added at the top and makes the code even easier and more readable
Comment #53
yched CreditAttribution: yched commentedThanks @Nick_vh - almost there :-)
These changes can be reverted now, we don't use the config factory anymore (yay !)
Then this really is just strictly equivalent to $this->getDisplayModeOptions() now :-)
minor : s/exists/exist
Comment #54
swentel CreditAttribution: swentel commentedrerolled
Comment #55
swentel CreditAttribution: swentel commentedugh, forgot some others, one sec
Comment #57
swentel CreditAttribution: swentel commentedOk, this is better - interdiff against #53
Comment #58
yched CreditAttribution: yched commentedCool - I think we have it :-)
Comment #60
webchickNot marking down from RTBC but...
How does contrib hook into this, if they're providing a non-front-end view mode? For example, https://www.drupal.org/files/project-images/screenshot_17.png shows "Tokens" and "Test bundle."
Comment #61
swentel CreditAttribution: swentel commentedStupid me
@webchick Hmm yeah, good call - are you fine creating a follow up for that, this is blocking search api now ?
Comment #62
yched CreditAttribution: yched commentedSorry to be a pain, but could we keep $options as a name for this ? (consistent with the internal var name in getDisplayModeOptions())
A little less accurate :-) What about :
"If needed, filter out modes for which the entity display is disabled (or inexistent)" ?
Comment #63
yched CreditAttribution: yched commented@webchick : true, but the hardcoded special treatment of 'rss' and 'search_index' is not introduced by this patch, it's just moved around (see the code removed from NodePreviewForm). IIRC, it was already baked in the initial "node preview for realz" patch - I even think there was a followup issue opened for that ?
Comment #64
swentel CreditAttribution: swentel commentedok :)
Comment #65
alexpottThere are no usages of these methods anywhere - can we add tests as I guess these are here for symmetry with the view mode methods.
$include_disabled
parameter is never used (or tested) ongetViewModeOptionsByBundle
andgetFormModeOptionsByBundle
.Comment #66
yched CreditAttribution: yched commentedThis would still need fixing :-)
Anyone up for rerolling / adressing #65 ?
(sorry, no drupal time on my side before another couple days...)
Comment #67
nlisgo CreditAttribution: nlisgo commentedComment #68
nlisgo CreditAttribution: nlisgo commentedComment #69
nlisgo CreditAttribution: nlisgo commentedRe-roll required after #2472147: Standardize getter docblocks in node module was committed.
I will attempt to address the feedback in #65 in a separate patch.
Comment #70
nlisgo CreditAttribution: nlisgo commentedComment #71
nlisgo CreditAttribution: nlisgo commentedWhen the getFormOptions method was introduced in #2154711-63: Move entity_get_(form/view)_mode_options() functions to EntityManager and add get(Form/View)ModeOptions() methods there was one use of it in the code. I will try and track down when it was removed.
Is it possible that the in $include_disabled parameter for getViewModeOptionsByBundle and getFormModeOptionsByBundle is scope creep and we should remove it here and should open a new issue to make a case for it and introduce the tests or implement it in the codebase if appropriate in that new issue?
Comment #72
nlisgo CreditAttribution: nlisgo commentedThe last use of getFormModeOptions was removed in this patch #2416409-65: Delete dependent config entities that don't implement onDependencyRemoval() when a config entity is deleted.
It feels rather strange adding tests for code that is never used, I know of some cases where that might be necessary (such as testing a hook that has not invoked in core) but us this one of those cases? If all use of it in the codebase has been stripped out over time then is it needed at all?
Comment #73
nlisgo CreditAttribution: nlisgo commentedComment #74
swentel CreditAttribution: swentel commentedThanks for the re-roll!
Regarding getFormModeOptions(). Yes, it would be kind of weird to have a method that is not used in core and we usually don't support that, however, I think in this case, it might be valid. Mostly because I think we'll see interesting contribs using/extending (think inline entity form) the form modes concept that is now in core, but only used on a limited level. Having the equivalent method for getting form modes seems useful then.
As for $include_disabled - the 'disabled' state in the patch is shifted to the entity display and not the display mode config entity (which is the bug we're fixing here) and wouldn't make any sense to be used since the entity display will not be loaded either. So I guess we can simply remove the parameter.
Comment #75
nlisgo CreditAttribution: nlisgo commentedGetting rid of redundant parameter $include_disabled.
All that needs doing now is to provide tests for getFormModeOptions and getFormModeOptionsByBundle.
Comment #76
nlisgo CreditAttribution: nlisgo commentedComment #77
nlisgo CreditAttribution: nlisgo commentedI'm going to get the ball rolling on the tests.
Comment #78
nlisgo CreditAttribution: nlisgo commentedI'm not sure how this patch is going to be received but I found a place in the codebase that we should be using the getFormModeOptions and getViewModeOptions methods as the code in the Drupal\field_ui\Form\EntityDisplayFormBase::form was preparing an array just like the output of getFormModeOptions or getViewModeOptions.
If we prepare a method that is useful for contrib but are unwilling to use it where it is appropriate in code then it probably will not get used or at least it doesn't send out the right message.
If this is accepted and removes the need to prepare a test then we would just need a test for getFormModeOptionsByBundle. I'm fairly confident that a test is possible with phpunit which I'm not proficient with but willing to give it a go with some support. A test should belong in the Drupal\Tests\Core\Entity\EntityManagerTest class.
Comment #79
nlisgo CreditAttribution: nlisgo commentedLeaving as 'Needs review' to invite feedback on the latest patch. I will try and prepare tests this weekend otherwise I will unassign.
Comment #80
nlisgo CreditAttribution: nlisgo commentedThe phpunit test is too tricky for me. I will review whatever someone else can come up with so that I can learn from it. So far, I'm getting stuck preparing the mock storage handler. So far I have this:
This is not intended to be complete but I just don't know how to progress from this point.
Comment #82
swentel CreditAttribution: swentel commentedMoved the tests into EntityDisplayTest so they all belong together a bit - we should modernize that test class one day though.
Comment #83
yched CreditAttribution: yched commentedUnlike the rest of the "display methods" here, those additionally filter by "the displays that are enabled for this bundle" - enabled/disabled is by bundle, otherwise all bundles have the same available modes.
IMO we should make that clear in the name [edit : and also in the phpodoc : this looks at the available modes, but additionally looks at the corresponding entity displays for the bundle]. getEnabledDisplayModeOptionsByBundle() ?
minor : by that definition, $display_statuses should be named $enabled_displays.
Wonder if we could use an EntityQuery here to filter on status = 1. This loads the same amount of config records, but avoids instanciating the Entities ? Probably no biggie given this only for a given bundle.
Comment #84
swentel CreditAttribution: swentel commentedDone 1 and 2. Not yet 3 since that would add another param in the contruct and make EntityManager become ubergod ? :)
Comment #86
yched CreditAttribution: yched commentedOh, right, I though the EntityManager had direct access to the EntityQuery builder just like an entity handler, but that's in fact a separate service.
Yeah, nevermind 3, that's a micro-optim at best anyway, we're only loading the displays for a given bundle, shouldn't be that heavy. Sorry about that :-)
Comment #87
swentel CreditAttribution: swentel commentedNew test send - c'mon bot!
Comment #88
yched CreditAttribution: yched commentedLooks good, thanks @swentel !
Comment #89
alexpottCommitted 64d6415 and pushed to 8.0.x. Thanks!
Removed unused use on commit.
Comment #92
axe312 CreditAttribution: axe312 commentedI am missing this function. Cannot find it in core. What was the reason to not commit this when it was in the patch? @alexpott
Same goes for these.
Comment #93
yched CreditAttribution: yched commentedOpps, it looks like the patch that got committed was #82 instead of #84...
So @axe312, the methods are there, but they're called getViewModeOptionsByBundle() / getFormModeOptionsByBundle() (without "Enabled")
Sadly it's too late now to change the method names to the name that was intended. Opened #2590605: Followup for [#2322503] for the rest of interdiff #84.
Comment #94
yched CreditAttribution: yched commentedAnyone up for revieweing the super-trivial folowup for #93 at #2590605: Followup for [#2322503] ? :-)