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();
      }
    }

Comments

marcvangend created an issue. See original summary.

yahyaalhamad’s picture

Glad someone beat me to it. I will upload a patch soon.

yahyaalhamad’s picture

yahyaalhamad’s picture

marcvangend’s picture

Thanks 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):

public function isAllowed() {
  $allowed = FALSE;

  $parameters = $this->routeMatch->getParameters();
  // Find the first parameter that is an entity.
  foreach ($parameters as $parameter) {
    if ($parameter instanceof EntityInterface) {
      $entity = $parameter;
      $this->entity = $entity;
      $allowed = $this->accessChecker->isAllowed($this->settings, $entity, 'full', $entity->getEntityTypeId(), $entity->bundle());
      break;
    }
  }

  return $allowed;
}

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."

yahyaalhamad’s picture

@marcvangend, Yeah I agree. I guess I'm still not that confident to change other's code.

marcvangend’s picture

I guess I'm still not that confident to change other's code.

Please 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.

yahyaalhamad’s picture

StatusFileSize
new18.88 KB

Another attempt tried to clean up some stuff, and the entity is now loaded dynamically from the controller.

yahyaalhamad’s picture

Status: Active » Needs review
yahyaalhamad’s picture

StatusFileSize
new19.41 KB

Applied code standards also

marcvangend’s picture

Thanks 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.

yahyaalhamad’s picture

StatusFileSize
new3.28 KB

Sorry, @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.

marcvangend’s picture

Thanks @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.

yahyaalhamad’s picture

I tested it on Node + Taxonomy controller, this needs more testing though in other entity types.

john.oltman’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.