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
Fatal PHP Fatal error: Call to a member function getEntity() on a non-object in /var/www/drupal-8/core/modules/comment/src/CommentAccessControlHandler.php on line 61
Steps to reproduce.
- Create a comments view with a rest display on path /comments
- Add relation to it's node with node id from url
- Is preview working?
- Is page in browser working?
- Is
curl --user admin:admin --header 'Accept: application/json' --request GET http://drupal.d8/comments
- Change Row style from Entity to Fields
- Repeat the checks above
Proposed resolution
Remaining tasks
User interface changes
API changes
Original report by @MartijnBraam
Got this error on a view with rest export for comments filtered by node. The view works if format is "Entity" but not with "Fields".
[Wed Sep 24 14:12:31.021783 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP Fatal error: Call to a member function getEntity() on a non-object in /var/www/drupal-8/core/modules/comment/src/CommentAccessControlHandler.php on line 61
[Wed Sep 24 14:12:31.021880 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP Stack trace:
[Wed Sep 24 14:12:31.021918 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 1. {main}() /var/www/drupal-8/index.php:0
[Wed Sep 24 14:12:31.021943 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 2. Drupal\\Core\\DrupalKernel->handle() /var/www/drupal-8/index.php:22
[Wed Sep 24 14:12:31.021977 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 3. Stack\\StackedHttpKernel->handle() /var/www/drupal-8/core/lib/Drupal/Core/DrupalKernel.php:567
[Wed Sep 24 14:12:31.021990 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 4. Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle() /var/www/drupal-8/core/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23
[Wed Sep 24 14:12:31.021998 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 5. Drupal\\Core\\StackMiddleware\\PageCache->handle() /var/www/drupal-8/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php:58
[Wed Sep 24 14:12:31.022010 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 6. Drupal\\Core\\StackMiddleware\\KernelPreHandle->handle() /var/www/drupal-8/core/lib/Drupal/Core/StackMiddleware/PageCache.php:52
[Wed Sep 24 14:12:31.022016 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 7. Symfony\\Component\\HttpKernel\\HttpKernel->handle() /var/www/drupal-8/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php:53
[Wed Sep 24 14:12:31.022032 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 8. Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw() /var/www/drupal-8/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php:66
[Wed Sep 24 14:12:31.022046 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 9. call_user_func_array() /var/www/drupal-8/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php:126
[Wed Sep 24 14:12:31.022059 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 10. Drupal\\views\\Routing\\ViewPageController->handle() /var/www/drupal-8/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php:126
[Wed Sep 24 14:12:31.022071 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 11. Drupal\\views\\ViewExecutable->initHandlers() /var/www/drupal-8/core/modules/views/src/Routing/ViewPageController.php:82
[Wed Sep 24 14:12:31.022083 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 12. Drupal\\views\\ViewExecutable->_initHandler() /var/www/drupal-8/core/modules/views/src/ViewExecutable.php:787
[Wed Sep 24 14:12:31.022095 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 13. Drupal\\field\\Plugin\\views\\field\\Field->access() /var/www/drupal-8/core/modules/views/src/ViewExecutable.php:903
[Wed Sep 24 14:12:31.022107 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 14. Drupal\\Core\\Entity\\EntityAccessControlHandler->fieldAccess() /var/www/drupal-8/core/modules/field/src/Plugin/views/field/Field.php:217
[Wed Sep 24 14:12:31.022120 2014] [:error] [pid 13325] [client 127.0.0.1:46271] PHP 15. Drupal\\comment\\CommentAccessControlHandler->checkFieldAccess() /var/www/drupal-8/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php:297
Beta phase evaluation
Issue category | Bug because fatals |
---|---|
Issue priority | Critical because blocks other criticals |
Disruption | None |
Comment | File | Size | Author |
---|---|---|---|
#43 | comment-rest-fatal-2344151.43.patch | 17.26 KB | larowlan |
#41 | comment-rest-fatal-2344151.39.patch | 17.75 KB | larowlan |
#41 | interdiff.txt | 876 bytes | larowlan |
#38 | comment-rest-fatal-2344151.38.patch | 17.75 KB | larowlan |
#38 | interdiff.txt | 2.67 KB | larowlan |
Comments
Comment #1
clemens.tolboomI vaguely remember this encounter this field related error.
Comment #2
larowlanAny chance you could export the view and paste here?
Comment #3
clemens.tolboom@larowlan iirc @MartijnBraam added the comment body field which made it crash.
Maybe reviewing #2340471: Rest export views preview should show some output instead of 500 would help too.
Comment #4
larowlanSo is the error on preview or on visiting the page.
If on preview, @clemens.tolboom's issue should fix it.
If not, then it sounds like a different issue so a sample view (which could be used in a test) will be helpful.
Lee
Comment #5
clemens.tolboomBoth preview and REST request from angular app crashed. Probably for different reasons.
Comment #6
MartijnBraam CreditAttribution: MartijnBraam commentedThis is a export of the view. The json request crashes as soon as I add the comment body field.
Comment #7
larowlanThanks, will have a look when I get a chance
Comment #8
larowlanFail patch, working on pass
Comment #9
larowlanHere's the pass test, issue is with views passing null $items to field access during init of handler, so deferred that to the parent (As comment field access relies on having some $items) but added some checks to ensure that field-level access is still respected once $items exists.
Comment #10
larowlanComment #11
larowlanAlso @MartijnBraam and @clemens.tolboom note that I've changed the rest path in the view to be node/%node/comments because that is more correct.
Comment #12
andypostplease, add inline comment why $items is null, that's really strange
Comment #14
larowlanAdded comment
Comment #15
BerdirYou should probably check $items && $items->getEntity(). I had a similar problem yesterday, but in my case, I had items but no entity when fields were displayed in a view.
This isn't "strange". $items is defined to be an optional argument, there are cases where access is checkout without having items.
This makes all the access checking optional now. Is that really what we want? Wouldn't this allow access to things like hostnames through REST, as it defaults to allowing access? Should we use default settings when we can't get them from the entity?
Comment #16
andypostI'd prefer to use default values before checking.
@Berdir
is that a common case?
Comment #17
larowlan@berdir - it calls access in the handler init, I thought it might also skip access checks too so added a test for both mail and host - as these are protected - neither of these were displayed - so I'm figuring it calls access again later with real items? hoping someone from views camp can confirm
Comment #18
BerdirNot sure, all I know is that I built a field based RSS view and it blew up because I had a $items->getEntity() there that returned NULL.
Comment #19
clemens.tolboomComment #20
damiankloip CreditAttribution: damiankloip commentedSo this is more of a comment/possibly views module issue and not rest?
Comment #21
clemens.tolboomI tried to reproduce the fatal view but got different results.
- comments
- fields: Title,
- relation on Node ID
I expected the preview to fail which did not broke.
Visiting the page on http://drupal.d8/comments/1 is not broken either
Running
curl --user admin:admin --header 'Accept: application/json' --request GET http://drupal.d8/comments/1
is working fine.
What did I miss in reconstructing the view?
I hope to test the patch tomorrow.
Comment #22
dawehnerAt least the API allows so yeah +1 for ensure that you don't fail.
We don't add UUIDs to those test views, sorry
Comment #23
jhedstromBouncing to needs work based on #22.
Comment #24
larowlanRe-roll and refactor to not allow null $items in the edit op.
Comment #25
dawehnerIt would be great to get this issue in, really, it add test coverage, this is quite helpful.
Isn't AccessResult::neutral() the exact tool for it? ...
note: these fields should now have 'entity_type' and 'entity_field'
Comment #26
andypost#25.1 Good point, but comment depends on access to parent(commented) entity access so when no entity there should be no access.
Also there's couple of nitpicks
why not $entity = $items ? $items->getEntity() : NULL;
please, add blank line between
Comment #27
dawehner... Well, I guess things are anded together anyway.
Comment #28
dawehnerNote: This fixes one of the failures of #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency, so its critical for itself.
Bump up to critical.
Comment #29
dawehner.
Comment #30
larowlanComment #31
kim.pepperThis comment seems more about not having any items, rather than access to the comment entity?
Comment #33
larowlanSo it fails on schema issues for the exported test view - but none of the other test views in comment module have a schema - does that mean that's an issue with rest schema?
Comment #34
larowlan#31 if we have items we can use $items->getEntity() - clarified comment
Comment #35
larowlanmmmmmm green
Comment #36
andypostlooks great, rtbc +1
Comment #37
dawehnerEveryime I look at the patch I still stumble upon line #25.1
Comment #38
larowlanThanks @dawehner - we can be smarter about this
Comment #39
BerdirCan we give this a bit more descriptive title and update the issue summary? I guess then it's ready.
Comment #40
kim.pepperSmall nitpick:
s/field/fields
Comment #41
larowlanNew patch, new title, added beta eval
Comment #42
jibranwrong rebase.
Comment #43
larowlanComment #44
dawehnerLooks fine for me.
Comment #45
jibranLet's fix it then.
Comment #46
alexpottCommitted 1c94124 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation.
Comment #48
BerdirSimilar issue: #2404739: language_entity_field_access() doesn't work if $items isn't present