I like to hide a block for one specific content type. What I do to achieve this is the following:

  1. restrict block access by content type (in the block pereferences).
  2. negate the condition so that block access is given for any content type except the selected one

This works fine as long as the node has a content type. If the node does not have any content type (like for example a Views page) the block will also be hidden.

I tried to debug the whole evaluation process of block access. I ended up in the class \Drupal\Core\Plugin\Context\ContextHandler in the method applyContextMapping at the position where it gets checked that the context has a context value. But a Views page has no context value so the condition of the content type restriction never gets evaluated but instead of that the method throws an exception "Required contexts without a value:".

I don't know if this issue is actually a core issue (either Views or Conditions) because of the class mentioned above. Or if the chaos tool suite can somehow fix this behavior because the condition plugin is coming from this module.

Issue fork ctools-2823356

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TiMESPLiNTER created an issue. See original summary.

seanB’s picture

Status: Active » Needs review
FileSize
1.64 KB

Ok went down the rabbit hole. This should fix it! This patch makes the entity context non-required and always provides access when the context is not available.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

It works! Not sure it needs tests but cool to have

seanB’s picture

Sorry, I'm going to mark this as needs work. Was just talking to chx who found an important issue with it. There is also this core issue: #1932810: Add entity bundles condition plugin for entities with bundles

When you make the condition not required, apparently Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait::addContextAssignmentElement() adds a new select box to pick a context. For non-node entity types the select box is empty and this probably doesn't work.

andypost’s picture

Status: Reviewed & tested by the community » Needs work

Makes sense, missed that

star-szr’s picture

Version: 8.x-3.0-alpha27 » 8.x-3.x-dev
szeidler’s picture

FileSize
1.67 KB

Here's a reroll of #2 for the current 8.x-3.x-dev if someone needs it.

Leaving it as "Needs work", because of #4.

maursilveira’s picture

baikho’s picture

FileSize
1.67 KB

Rolling a patch against current stable 8.x-3.4

baikho’s picture

FileSize
1.67 KB

Fixed minor issue in #10 on EntityContextDefinition

szeidler’s picture

FileSize
1.79 KB

Here's a reroll for current dev + 3.5.0

seanB’s picture

Status: Needs work » Needs review
FileSize
1.63 KB

There was a problem with the reroll. This patch should fix it.

seanB’s picture

FileSize
1.54 KB

Reverted a change that is no longer needed in the patch.

tgauges’s picture

Patch #14 fixes the problem for me. Thank you!

dxvargas’s picture

Fix seems just fine and it works. Not sure it needs tests but cool to have.

dxvargas’s picture

Status: Needs review » Reviewed & tested by the community
seanB’s picture

This no longer works in Drupal 9.3 since #1932810: Add entity bundles condition plugin for entities with bundles added a condition plugin to core with the same ID. Which makes me think we should probably open a similar issue for core.

seanB’s picture

joelpittet’s picture

@seanB and @dxvargas is this still RTBC from your perspective?

Berdir’s picture

I suppose this would still work for now combined with #2857279: Duplicate node type visibility condition in block settings, but that's temporary step as the plan will be to remove the duplicated code once 9.3 is required.

As commented in the core issue, I'm not sure it's a black/white bug and the current behavior is always wrong, it depends on the use case.

japerry’s picture

Status: Reviewed & tested by the community » Needs work

Marked needs work since the patch conflicts with the work in core now.

ricovandevin made their first commit to this issue’s fork.

ricovandevin’s picture

Status: Needs work » Needs review
FileSize
833 bytes

Rerolled #14. The changes to src/Plugin/Condition/EntityBundle.php are not relevant anymore so only kept the change in src/Plugin/Deriver/EntityBundle.php.

Berdir’s picture

This doesn't do anything anymore. The reason they are no longer relevant is because that code moved to core, you will need to look at the core issue now.

vasike’s picture

Status: Needs review » Needs work
Related issues: +#2535896: ConditionManager::evaluate() should not negate results itself

Core issue with patches that covers/fix this (for core)
https://www.drupal.org/project/drupal/issues/2535896

Let's add it as related issue.

Also updated the status.