I got a fatal error when trying to view a node revision:
Error: Call to a member function bundle() on string in Drupal\forward\Plugin\Block\ForwardLinkBlock->isAllowed()
This occurred in ForwardLinkBlock.php where it says:
if ($parameters->has('node')) {
$entity = $parameters->get('node');
$bundle = $entity->bundle();
}
The code here wrongly assumes that if one of the route parameters is called "node", it must always contain a node object. This is not true in the case of the entity.node.revision route, and perhaps in other cases too.
Proposed solution: Check if $entity is an instance of EntityInterface, for instance
if ($parameters->has('node')) {
$entity = $parameters->get('node');
if ($entity instanceof EntityInterface) {
$bundle = $entity->bundle();
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | fatel_error-3111639-12.patch | 3.28 KB | yahyaalhamad |
| #10 | fatel_error-3111639-10.patch | 19.41 KB | yahyaalhamad |
| #8 | fatel_error-3111639-8.patch | 18.88 KB | yahyaalhamad |
| #3 | fatel_error_on_revisions_view-3111639-3.patch | 1.26 KB | yahyaalhamad |
Comments
Comment #2
yahyaalhamadGlad someone beat me to it. I will upload a patch soon.
Comment #3
yahyaalhamadComment #4
yahyaalhamadComment #5
marcvangendThanks Yahya!
On closer inspection, I think it would be much better to implement the method described in this blog post. That way we lose the hard-coded references to 4 specific entity types, and it's less code :-) I guess the function could look like this (warning, untested code):
PS. You don't need to do
$bundle = $entity->bundle() == $entity->getEntityTypeId() ? "" : $entity->bundle();, because the documentation of EntityInterface::bundle() says: "@return string - The bundle of the entity. Defaults to the entity type ID if the entity type does not make use of different bundles."Comment #6
yahyaalhamad@marcvangend, Yeah I agree. I guess I'm still not that confident to change other's code.
Comment #7
marcvangendPlease don't worry too much about that. Other developers may look confident (some are :-)) but often we're just putting ideas out there to collect feedback. It's the fastest way to move the Drupal project and your own learning forward.
Comment #8
yahyaalhamadAnother attempt tried to clean up some stuff, and the entity is now loaded dynamically from the controller.
Comment #9
yahyaalhamadComment #10
yahyaalhamadApplied code standards also
Comment #11
marcvangendThanks for your contribution! That's a much bigger patch than the first one. While that is not necessarily a problem, it seems that you're trying to solve more issues than just the fatal error this issue started with. Creating a separate issue for each improvement (fix fatal error / create base class / coding standards / etc.) may make it easier for the maintainer to review and commit your patches.
Comment #12
yahyaalhamadSorry, @marcvangend, this patch attempts to fix the problem without changing unnecessary things, I guess I felt a little overwhelmed since the code appears to be repeated two times.
Comment #13
marcvangendThanks @YahyaAlHamad, that patch looks very good, although I must add I haven't tested it yet.
You are right that some code is duplicated and abstracting it into a base class would solve that. You don't need to apologize! However in my experience, patches are more likely to be accepted when they only try to solve one problem. That way, if a discussion arises about the newly introduced base class, it will not stop the fatal error from being fixed.
Comment #14
yahyaalhamadI tested it on Node + Taxonomy controller, this needs more testing though in other entity types.
Comment #16
john.oltman commented