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.
More or less as a follow-up to #709892: Complete entity CRUD hook invocations:
While we rightfully have hook_entity_load(), hook_entity_insert(), hook_entity_update(), and hook_entity_delete() now, the last missing piece to complete the stack is hook_entity_view() and hook_entity_view_alter().
Attached patch does just that. Normally a few lines only, but the added documentation makes it larger.
While doing it, I recognized that taxonomy_term_view() does not invoke any hook yet, which could be classified as a bug...
Comment | File | Size | Author |
---|---|---|---|
#7 | drupal.entity-view.7.patch | 10.03 KB | sun |
#2 | drupal.entity-view.2.patch | 9.97 KB | sun |
drupal.entity-view.0.patch | 9.9 KB | sun | |
Comments
Comment #2
sunOnly variables can be passed by reference -- All arguments of drupal_alter() are taken by reference, so $type needs to be prepared separately.
Comment #3
Dries CreditAttribution: Dries commentedIn Drupal 8, I'd love to standardize on
drupal_alter('entity_view')
and to get rid of the domain specificdrupal_alter()
s (e.g.drupal_alter('comment_view', $build)
. We need the generic one for different reasons, and it would allow us to streamline and simplify the code.I'd be in favor of this patch, especially if we added "Drupal 8 @todos" to the domain specific
drupal_alter()
s. It tells people to start using the generic one in Drupal 7.Comment #4
sunHm. I wouldn't want to preemptively recommend any usage. The benefit of the more focused alter hooks is that the implementations are only invoked when actually needed. The generic hook_entity_view() + hook_entity_view_alter() is invoked for all entities, and therefore should only be used if the code really affects all entities. At least for now. We can discuss this and performance implications of removing the entity-specific hooks in detail for D8.
Comment #5
effulgentsia CreditAttribution: effulgentsia commentedYeah, there's definitely use-cases for this. Some modules will want to act on entities generically, rather than on a per-type basis. Just one of about a bazillion examples: contrib really will need a comment module that allows comments on entity types other than nodes. Media entities will need comments. Product entities will need comments, etc. Basically, for all the reasons we made fields work with entities generically, and not just with nodes, many contrib modules will need to do the same: comment, fivestar, flag, etc. Some of these modules will be able to become fields, but not all. So, IMO, this is "major".
It's a shame that we're still finding this now, rather than earlier in the dev cycle. But considering the entity API is brand new to D7, we shouldn't be too surprised to be still finding holes in it. Fortunately, this hole is easy to fix without breaking BC. #830704: Entity forms cannot be properly extended might be more challenging.
What I don't like about #2 though, is that hook_entity_view_alter() runs after hook_TYPE_view_alter(). I would prefer it to look more like:
For all the same reasons that we did this structure for 'form' and 'form_ID'.
But, I also realize that isn't completely clean, because we can't do the same for the non-alter hooks. Not sure how to best deal with that yet.
For D8, I'd like to move what we did for drupal_alter() into module_invoke_all(), so that we could do module_invoke_all(array('GENERIC_HOOK', 'SPECIFIC_HOOK'), ...).
Comment #6
effulgentsia CreditAttribution: effulgentsia commentedActually, maybe that's not a problem. The order in which hook_entity_view() and hook_node_view() implementations run shouldn't matter. Who cares if a low weight hook_entity_view() runs after a high weight hook_node_view(). Those implementations are for adding NEW things, not for altering a different module's decision. But I think we need to avoid a low weight hook_entity_view_alter() overruling a high weight hook_node_view_alter(). So CNW for #5.
Comment #7
sunThanks for reviewing! Good call!
Comment #8
Dries CreditAttribution: Dries commentedThis looks RTBC, except that the test bot doesn't run.
Comment #10
sun#7: drupal.entity-view.7.patch queued for re-testing.
Comment #11
sunBack to RTBC
Comment #12
webchickDries and I talked about this yesterday. This seems to be pretty firmly in the "API oversight" rather than the "new feature" side of API additions, so committing #7 to HEAD.