I like to hide a block for one specific content type. What I do to achieve this is the following:
- restrict block access by content type (in the block pereferences).
- 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.
Comment | File | Size | Author |
---|---|---|---|
#25 | 2823356-25.patch | 833 bytes | ricovandevin |
#14 | 2823356-14.patch | 1.54 KB | seanB |
#13 | 2823356-13.patch | 1.63 KB | seanB |
#12 | ctools-2823356-12.patch | 1.79 KB | szeidler |
#11 | ctools-2823356-11.patch | 1.67 KB | baikho |
Issue fork ctools-2823356
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:
Comments
Comment #2
seanBOk 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.
Comment #3
andypostIt works! Not sure it needs tests but cool to have
Comment #4
seanBSorry, 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.Comment #5
andypostMakes sense, missed that
Comment #6
seanBLet's relate this #2695955: When using the entity bundle condition, block is not shown even when condition is met.
Comment #7
star-szrComment #8
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedHere'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.
Comment #9
maursilveira CreditAttribution: maursilveira at Northern Commerce commentedHello,
Re-rolling this patch after #2785499: Fix Entity Bundle Condition Plugin does not work in all situations has been committed.
Comment #10
baikhoRolling a patch against current stable
8.x-3.4
Comment #11
baikhoFixed minor issue in #10 on
EntityContextDefinition
Comment #12
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedHere's a reroll for current dev + 3.5.0
Comment #13
seanBThere was a problem with the reroll. This patch should fix it.
Comment #14
seanBReverted a change that is no longer needed in the patch.
Comment #15
tgauges CreditAttribution: tgauges commentedPatch #14 fixes the problem for me. Thank you!
Comment #16
dxvargas CreditAttribution: dxvargas at Webzina for European Commission and European Union Institutions, Agencies and Bodies commentedFix seems just fine and it works. Not sure it needs tests but cool to have.
Comment #17
dxvargas CreditAttribution: dxvargas at Webzina for European Commission and European Union Institutions, Agencies and Bodies commentedComment #18
seanBThis 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.
Comment #19
seanBAdded #3255496: Negated EntityBundle conditions are always false for non-node pages as a core issue and will add the patch there as well.
Comment #20
joelpittet@seanB and @dxvargas is this still RTBC from your perspective?
Comment #21
BerdirI 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.
Comment #22
japerryMarked needs work since the patch conflicts with the work in core now.
Comment #25
ricovandevin CreditAttribution: ricovandevin at Finlet for iO commentedRerolled #14. The changes to src/Plugin/Condition/EntityBundle.php are not relevant anymore so only kept the change in src/Plugin/Deriver/EntityBundle.php.
Comment #26
BerdirThis 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.
Comment #27
vasikeCore 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.