Problem/Motivation

There is no longer a way to gain the context of the block/entity within a condition plugin. This has made it impossible for me to upgrade Per User Block to the new condition plugins.

This module is a backport of the functionality that was wrote when we removed it from D8.

Relevant usage is here: http://cgit.drupalcode.org/per_user_block/tree/src/Plugin/Condition/PerU...

Proposed resolution

To be discussed.

Remaining tasks

Agree upon a solution, write patch.

User interface changes

n/a

API changes

n/a

CommentFileSizeAuthor
#7 interdiff.txt1.1 KBbenjy
#7 2374295-7.patch1.29 KBbenjy
#5 2374295-5.patch860 bytesbenjy
#2 2374295-1.patch865 bytesbenjy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

EclipseGc’s picture

So, this is really screwy which is why I proposed we accept this regression. If we want to do this for real, you need to document the block as a required context (in the annotation) of this condition. I have a notion of HOW we could do this. After #2339151: Conditions / context system does not allow for multiple configurable contexts, eg. language types lands, the Block entity itself will be passed the context array. We might be able to ad-hoc add the current block to the context stack so that this can be evaluated, but all my efforts are focussed there right now. Once that lands, I'm way more up for trying to address this.

Eclipse

benjy’s picture

FileSize
865 bytes

As discussed with EclipseGc in IRC one approach would be as shown in the attached patch (written by EclipseGc). The patch depends on #2339151: Conditions / context system does not allow for multiple configurable contexts, eg. language types so leaving as active for now.

EclipseGc’s picture

Should use Core's version of Context, but otherwise yeah, this would make the current block available to conditions to evaluate it if necessary.

Eclipse

EclipseGc’s picture

Also, I reserve the right to completely disown this patch if at some point I become convinced this is just insane. We abandoned this particular feature (that this issue is attempting to re-enable) for a purpose, which was that we are pursuing a vastly different block placement model. True, D8 still has a lot of similarities to D7, and we obviously CAN make per-user block visibility work, but I'm not sure we want to. This is totally overridable in contrib, I have yet to be convinced one way or another, but as a technical challenge it was interesting, and there is a solution. I have no clue how we'd migrate this to a more panels-style block rendering style though.

Eclipse

benjy’s picture

Status: Active » Needs review
FileSize
860 bytes

OK, I re-rolled the patch and updated to use the core class now #2339151: Conditions / context system does not allow for multiple configurable contexts, eg. language types is in. We don't have anywhere in core that uses this functionality which makes it hard to test from a web test.

tim.plunkett’s picture

+++ b/core/modules/block/src/Entity/Block.php
@@ -219,6 +221,8 @@ public function getCacheTags() {
+    $contexts['block'] = new Context(new ContextDefinition('entity:block'));

This should probably be namespaced better, maybe block.entity or block.block_entity?

Also this means that two objects are instantiated with every call to this each time, is it worth storing it somewhere?

benjy’s picture

FileSize
1.29 KB
1.1 KB

I know EclipseGc expressed concerns about this issue in general, if we do end up deciding against it, this is how I solved it in contrib: http://cgit.drupalcode.org/per_user_block/commit/?id=654aeb3d05ea0ff2bf3...

Now storing the context in a class property and i namespaced as "block.block_entity". I'm using the tests from per_user_block to test these changes locally but still don't have a test for core.

benjy’s picture

One other thing that is strange because we have the namespace hardcoded is that to use this in your annotation you have to do this:

"block.block_entity" = @ContextDefinition("entity:block", label = @Translation("Block"))

Where the key is always block.block_entity but with any other condition, you can use whatever keys you want, even storing the same context in two different keys?

tim.plunkett’s picture

The same context cannot be used in multiple keys until #2378585: Multiple context requirements cannot be satisfied by a single value.

And no, you don't need to do that in your annotation, that's why we have context_mapping.
You would just need to resave the block in the UI after changing the context ID, it will then map whatever you have in your annotation to "block.block_entity" for you.

EclipseGc’s picture

Yup to everything tim said.

Eclipse

benjy’s picture

The same context cannot be used in multiple keys until #2378585: Multiple context requirements cannot be satisfied by a single value.

I read this in the docs at the top of Drupal\Core\Annotation\ContextDefinition, thanks for pointing me to that issue.

Unfortunately, I can't get the per_user_block tests to pass without that annotation. I get an invalid context error. You could try running the tests from per_user_block yourself locally to see what i mean or I could roll a patch with it included into core?

tim.plunkett’s picture

Ah, @EclipseGc hit this exact problem today:

If you are submitting this through the UI, it will be handled automatically.

But if you are using drupalPlaceBlock, you have to do it manually.
See the bottom of the interdiff in #2339151-165: Conditions / context system does not allow for multiple configurable contexts, eg. language types for an example.

mgifford queued 7: 2374295-7.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 7: 2374295-7.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pritam.tiwari’s picture

I am also facing the same problem. Not able to get the block details in plugin condition evaluate.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.