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.
Problem/Motivation
Default EntityViewBuilder tries to render shortcut entities based on assumptions that don't match with shortcut entities.
By default EntityViewBuilder creates an render array'#theme' => 'shortcut'
, which tries to render the entity using non existend theme hook implementation.
Proposed resolution
Create view builder for Shortcut entity type.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#44 | entity-view-builder-2698909-44-interdiff.txt | 631 bytes | Berdir |
#44 | entity-view-builder-2698909-44.patch | 5.79 KB | Berdir |
#42 | entity-view-builder-2698909-42-interdiff.txt | 6.31 KB | Berdir |
#42 | entity-view-builder-2698909-42.patch | 6.4 KB | Berdir |
#39 | entity-view-builder-2698909-29.patch | 2.42 KB | Berdir |
Comments
Comment #2
andypostComment #3
jibran@lauriii can you explain the problem a little?
Comment #4
lauriiiIn #674108: ThemeManager::theme() does not trigger an error when a theme hook is not found we want to trigger an error every time theme hook is not found. EntityCacheTagsTestBase tests by default referencing entities and printing them as rendered entity. Without view builder it tries to render the entity using non-existing theme hook
shortcut
. There's probably bunch of other ways that could be solved in case this issue doesn't make any sense.Comment #5
tim.plunkettAhh, I was very confused at first, because entity types shouldn't be given a view_builder unless they specify.
Except
\Drupal\Core\Entity\ContentEntityType
forcibly adds\Drupal\Core\Entity\EntityViewBuilder
as one!Unfortunately, removing that line is out of the question. It would be the correct fix, since it blindly adds EntityViewBuilder without ensuring there is a corresponding theme hook.
Since AFAIK we don't actually view shortcuts via that code, I think we should add an alter to unset the view builder...
Comment #6
dawehnerCould we check somehow in the view builder whether the theme hook exists and otherwise throw an exception?
Comment #7
andypostLet's see how many thing could be broken
Comment #9
andypostSuppose #2270883: Automatically add theme hook suggestions for all entity types should be fixed first
Comment #10
andypostThe same way "menu_link_content" missing view builder
Comment #11
lauriiiThat is not necessary to be fixed before this. That only adds the theme suggestions but this should work without any theme suggestions.
Comment #12
andypostI see no reason to implement view builder because root of problem in missing theme functions, see #2186653: $build['#attributes'] added in hook_entity_view_alter() are not output for entity types that provide neither a #theme nor #theme_wrappers (breaks Quick Edit module for those entity types)
Ways to solve that
- add missing theme functions (wrappers or real ones) #2270883: Automatically add theme hook suggestions for all entity types
- add preprocessing into
\Drupal\Core\Entity\EntityViewBuilder
#2023571: Support preprocessing in EntityViewBuilderComment #14
andypostMissing builders after applying #7
drush @d8 scr theme-entity.php
Comment #15
andypostThe core content entities without theme function are
script is
Comment #16
dawehnerNone of those really have any meaning, you cannot render out a file entity, nor a contact message etc. I would argue that removing the view builder manually is the right solution still.
Comment #17
andypost@dawehner did you mean that way?
Only alter allows to unset that.
Comment #18
dawehner@andypost
Yeah something like that ...
Comment #21
tstoecklerLooks fine and has tests. Thanks!
Comment #22
tim.plunkettSorry to mess with such a simple and RTBC patch, but that alter was bothering me.
Specifying NULL in the annotation makes more sense to me, so that we keep the values with the rest of the annotation. Additionally, a custom module can now specify a view builder if they so desire, without needing to worry about hook invocation order.
Also since I was worried about the treatment of NULL, I expanded the tests.
Comment #23
BerdirWe also have this problem with other entity types, we just have no tests for them. menu_link_content, file and probably more.
The default entity view builder was added in #2446483: Viewing fields requires a view builder., so with this change, it is now longer possible to add shortcut fields to a view. So, I don't think we can just remove it.
Comment #24
BerdirCounter-proposal.
Comment #27
BerdirThis should be better.
Comment #28
lauriiiThe approach looks good. It would be good to add some test coverage for this (even in this issue).
Comment #29
BerdirUpdated the previous test. It's no longer shortcut specific, so we could make it generic as well and use entity_test or so.
Comment #30
BerdirComment #31
BerdirSee #2808481: Introduce generic entity template with standard preprocess and theme suggestions
Comment #33
tstoecklerComment #34
jibranShould we make this a generic entity test instead?
Comment #35
jibranI meant something like this.
Comment #37
Berdir@jibran: I would prefer to keep those overrides for forward compatibility with #2808481: Introduce generic entity template with standard preprocess and theme suggestions.
I was thinking about making the hook generic as well but actually, I'm not sure if we need to test every single entity type for a generic implementation.
Comment #38
jibranOk then NR for patch in #29 again.
Comment #39
BerdirRe-uploading the patch from #29 to make sure it still passes.
I think this is ready unless someone feels strongly about converting this test to use entity_test or so. Note that entity_test actually gets a template in #2808481: Introduce generic entity template with standard preprocess and theme suggestions, so it would need to be updated there again.
Comment #40
jibranI think it is fine.
Comment #41
alexpottWe should inject the service - no?
I'm not too keen on using shortcut for this. It is easy to imagine someone adding a shortcut theme and removing the test and then we lose coverage of this change.
Comment #42
BerdirOk. This moves the test to EntityViewBuilderTest and uses test entities. I also injected the theme registry but didn't update all the subclasses, core has 3 that inject additional dependencies.
I also removed the #theme removal in EntityTestViewBuilder. This is different from the others, as that will get to use the generic entity template in #2808481: Introduce generic entity template with standard preprocess and theme suggestions and then I will update the test for the new behavior.
Comment #44
BerdirRemoved left-over empty test class.
Comment #45
tstoecklerThank you, that looks good. I agree with not updating the other view builders for now, that would probably double that patch size (or worse...).
That's not used.
Can be fixed on commit.
Comment #48
tstoecklerNot sure what happened here...
Comment #50
BerdirTestbot, be nice again, please :)
Comment #51
alexpottCommitted and pushed 408f00c to 8.3.x and 0290c9c to 8.2.x. Thanks!
Fixed unused use on commit.
This kind of magic naming concerns me. But not changed here...