When the EntityBundle Condition Plugin evaluates the context, it assumes that the entity within is an actual entity object. But in some situations it seems to be only the entities id. My situation is:
- Created a page for taxonomy terms, having a variant that selects by using the EntityBundle Condition Plugin
- Added a custom view for reordering articles within that taxonomy term, that is displayed as an additional tab next to "view / edit / devel.." of a given taxonomy term
- When switching to the custom view, the system tries to render the breadcrums, which use the PathBasedBreadcrumbBuilder
- This Builder tries to evaluate the path to the "view" route of the taxonomy term
- That triggers the page / page variant and the EntityBundle Condition Plugin for selection to check access
- In this context of the condition plugin, the "taxonomy_term" is only the id, not the actual entity

=> Error: Call to a member function bundle() on string

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

perprogramming created an issue. See original summary.

perprogramming’s picture

andypost’s picture

nguyenphan’s picture

I tested okay. Thanks.

voleger’s picture

Status: Needs review » Reviewed & tested by the community

this patch works and help with the issue of General Data Protection Regulation (GDPR) - Fields submodule of General Data Protection Regulation (GDPR) module.
+1 for RTBC

Berdir’s picture

That seems like a workaround, whoever provides that context is doing it wrong, the value must be a loaded entity object.

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work

Setting to needs work for #6 because I agree, that seems like a workaround.

phjou’s picture

I agree with @berdir and @joelpittet This patch is just a workaround.
I've encountered the same problem once on a project. It was some custom code that was unsetting the entity in the parameters of the route.
You probably should debug the error and find what module is causing your problems.
The patch should not stay too long on your project, only the time to find what is the real reason of your issue.

Dirst’s picture

Hello, I found another issue related to this piece of code. When I have custom 404 page, and try to load taxonomy term page that doesn't exist (view overrided in page manager) it throws an error because $entity is empty. Something is related to the topic?

maxdev’s picture

Priority: Normal » Major

This is quite important issue.
If you are using Page manager's node_view this issue can be easily reproduced.
For example go to not existing node. If one of your node_view variants has selection criteria by EntityBundle or Content Type, then ctools plugin always throws fatal error:

Error: Call to a member function bundle() on string in Drupal\ctools\Plugin\Condition\EntityBundle->evaluate() (line 107 of modules/contrib/ctools/src/Plugin/Condition/EntityBundle.php).

This happens, because $this->getContextValue($this->bundleOf->id()) return string of nid (not existing node) instead of entity object.

$entity = $this->getContextValue($this->bundleOf->id());
return !empty($this->configuration['bundles'][$entity->bundle()]);
maxdev’s picture

I found also core issue a bit related to current 1932810
There is initiative to move EntityBundle condition plugin to core. I tried patches from 1932810, but after this ctools plugin became not working.
So if you had functionality, where was used ctools plugin it will not work anymore.

I created transitional patch for ctools, which should fix fatal error, when context is not an entity object.

maxdev’s picture

Oops, removed empty lines

michfuer’s picture

I'm seeing the same issue described in #10.

page_manager page for /node/{nid} with selection criteria to check for entity_bundle:node is a certain type.

Navigating to a non-existent nid, expecting a 404 response, but getting server error instead.

The patch in #12 helps, but than I'm seeing a similar error coming from /core/modules/node/src/Plugin/Condition/NodeType.php

$node = $this->getContextValue('node');
return !empty($this->configuration['bundles'][$node->getType()]);

because the /node/{nid} page has a variant using node_type plugin id for selection criteria.

shashank5563’s picture

Hello All,

After adding the given code my issue is resolved.

@@ -104,6 +105,11 @@ class EntityBundle extends ConditionPluginBase implements ConstraintConditionInt
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getContextValue($this->bundleOf->id());
+
+ if (!$entity instanceof ContentEntityInterface) {
+ return true;
+ }
+
return !empty($this->configuration['bundles'][$entity->bundle()]);
}

BD3’s picture

I can confirm that the patch in #12 fixed the issue for me when trying to duplicate Commerce Variations.

dewolfe001’s picture

shashank5563 - that fixed my issue! Thanks!

loze’s picture

I was getting this with a view that uses a commerce_product id in the path as an argment.
Patch in #12 fixes this for me.

loze’s picture

I spoke to soon.
While the patch in #12 does resolve this issue, it causes the pathauto checkbox not to appear on entity forms.

shashank5563'a comment in #14 looks like it got it working correctly.

shashank5563’s picture

Hi @loze,

I have tested my code with pathauto module and it is working fine can you create a patch for it.

Because, I am unable to create patch due to the some issue in my laptop.

Thanks in Advance

loze’s picture

Status: Needs work » Needs review
FileSize
613 bytes

here's a patch that got it working for me with the latest dev verison.

rockzhang’s picture

FileSize
1020 bytes

Fix missing namespace for patch in #20

Rajab Natshah’s picture

Title: Entity Bundle Condition Plugin does not work in all situations » Fix Entity Bundle Condition Plugin does not work in all situations
Rajab Natshah’s picture

Status: Needs review » Reviewed & tested by the community
  • Tested #21 on the 8.x-3.4 version
  • It fixes the issue.
  • Needs to have the patch to apply to the latest DEV too.
joelpittet’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all! I've committed this to the latest dev release.

  • joelpittet committed e2bc0c3 on 8.x-3.x authored by rockzhang
    Issue #2785499 by maxdev, perprogramming, loze, rockzhang, shashank5563...

Status: Fixed » Closed (fixed)

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

dubgeiser’s picture

Between #12 and #14 the return value has gone from FALSE to TRUE... Is this intentional?
Because returning TRUE doesn't fix this at all on my end...

--- a/docroot/modules/contrib/ctools/src/Plugin/Condition/EntityBundle.php
+++ b/docroot/modules/contrib/ctools/src/Plugin/Condition/EntityBundle.php
@@ -107,7 +107,7 @@ public function evaluate() {
     $entity = $this->getContextValue($this->bundleOf->id());

     if (!$entity instanceof ContentEntityInterface) {
-      return TRUE;
+      return FALSE;
     }

     return !empty($this->configuration['bundles'][$entity->bundle()]);
Oscaner’s picture

FileSize
1.28 KB

No require for context, this condition will effect no-content pages.