Hi,
Thanks for this nice module.
It would be a very nice addition if there is support for Views arguments, just like these modules viewfield and viewsreference
they both work well also, but they only work for Views blocks.
Whereas I like the idea that your module can reference any block including the wonderful webform block.
Thanks for your work.

| Comment | File | Size | Author |
|---|---|---|---|
| #22 | interdiff-20-22.txt | 3.46 KB | marcoscano |
| #22 | 2862204-22.patch | 11.7 KB | marcoscano |
| #20 | interdiff-18-20.txt | 3.1 KB | marcoscano |
| #20 | 2862204-20.patch | 11.46 KB | marcoscano |
| #18 | interdiff-17-18.txt | 4.44 KB | marcoscano |
Comments
Comment #2
berdirI think the way to do this is plugin contexts. Views in Drupal 8.3 now exposes arguments in many but not all cases as contexts. Contexts that this module can display and pass to the block.
Here's a first patch that's working nicely for me.
Views arguments and context require some trickery, for example by using validation to accept node objects.
Steps to reproduce:
1. Create a new views block, list content. Add a contextual filter, content: ID, validate the argument to be Content, optionally of a certain bundle.
2. Add a block note that without this patch, the views block will not show up at all, with this patch it will.
3. Create content, it should now display
Note that this will not allow to pass in values that do not exist as context. For example for simple ids, strings or so. Similar to Page Manager, it would be possible for this module to allow to define user-provided, hardcoded context values, for example in the field settings, that would then be available for users to chose form.
Core currently only defines contexts for the current node and current user. The current node does allow quite a few use cases, you simply have to add another (reverse) relationship back to the node. For example, to show content that has the same tag in a certain field as the current node, you need to add a relationship to that field and then from there another relationship using that to content, then add a contextual filter on the Content: ID argument using the second relationship. Obviously the resulting views query will be.. suboptimal but it works. Improving that requires context relationships, e.g. being able to use the term id of the current node directly as context, ctools has some logic for that that we could optionally integrate?
Another issue is that views blocks are currently all optional, even if they were defined as required. That means the UI shows up and as a user, you have to explicitly select the context or it won't work.
PS:
While working on this, I noticed some things that might be worth looking at in other issues:
* Cacheability metadata of the access result is lost. This could be a security issue if blocks have strange access checks, resulting in blocks being visible when they shouldn't.
* BlockViewBuilder uses a lazy builder to actually render the block. If you have blocks that are not cacheable at all or are per-user, then the whole entity is uncacheable/per-user, a lazy builder would automatically be placeholdered and could be rendered later.
Comment #3
joelsteidl commentedI ran into this issue as well (Views with contextual filters were missing as available options) and this patch is working well with 8.x-1.0-alpha4. I tried with the dev version and got the WSOD.
Comment #4
cbeier commentedI have created a view block with a contextual filter based on a taxonomy term reference field. So I can get all nodes of a specific category.
My problem is, that the contextual filter form element has no options in the block field form.
Maybe it is better to use at this point a simple textfield to enter the contextual value manually (like Views preview).
Comment #5
yobottehg commentedI can confirm problems from #4. Does not work for an filter based on "News category" for example
Comment #6
berdirThat is not a problem with this patch. This just allows to use existing contexts.
You can for example add another join to the view to join on the current node and then pass that as context, or write custom code to expose the category of the current node as a separate context.
There are tons of additional features possible on top of this, but those should be done in separate issues IMHO.
Still needs tests though.
Comment #7
joelsteidl commentedFor clarity, this does not provide an open-ended input field for providing contextual info on the fly? I believe Views Reference Field works that way and I just wanted to check and make sure. It would be awesome to allow that sort of flexibility. An example of making a new context plugin would be cool to. Are there any good examples on D.O.?
Thanks!
Comment #8
ericb1021 commentedAfter the last dev update, block-field-contexts-2862204-2.patch does not apply cleanly.
Updated patch attached.
Comment #9
opiNot tested deeply, but patch from #8 works as expected in my use case (simple views block with contextual arguement). Thanks.
Comment #10
svdhout commentedThis works great when using the patch in #8 combined with the oveo sandbox (https://www.drupal.org/project/oveo)
Comment #11
fenstratLooks good, thanks everyone! Would be great to get tests for this pretty please :)
Comment #13
jody lynnI rerolled the patch, but it didn't end up being useful to me due to the limitations described in 4 and 5
Comment #14
berdirSee #10, maybe oveo is useful for your use case?
Comment #15
berdirWell, it doesn't help that the last two patches were rerolled incorrectly and the context is set too late :)
Comment #16
berdirComment #17
marcoscanoThe patch in #15 removes the sorting of the definitions, so I'm adding it back here.
Comment #18
marcoscanoNow with some test coverage added.
Comment #19
berdirNot quite sure why we have submitForm() and drupalPostForm().
I guess alternatively we could also use the mink API directly with things like $page->fillField() + pressbutton(), that would allow us to use the actual labels of the form elements and assert that things are displayed correctly?
Negative tests are always tricky, lets make sure there's at least one additional node in the view being displayed?
Comment #20
marcoscano@Berdir thanks for reviewing!
When modifying an existing test, I normally try to not modify it too much, so I just repeated what was already there... :)
But I agree that using mink's methods gives us more flexibility and actually makes it more readable. So I did that in this patch.
Thanks!
Comment #21
berdiraccording to \Behat\Mink\Element\NodeElement::selectOption(), this should also work by selecting it with the label/text of the option.
I guess we can drop that @todo, still seems useful with the sorting.
We should actually move this above access() as that might need the injected context as well.
See \Drupal\ctools_block\Plugin\Block\EntityField::blockAccess() for example, which expects the context to be present when checking access.
It indeed doesn't but without any reference, the @todo isn't of much use :)
I'd say change it from a WTF-todo a normal comment that just states this as a fact.
Comment #22
marcoscano@Berdir thanks for the feedback!
In the last point, I was honestly unable to decipher the meaning of that comment..., so I just dropped the @todo altogether. Please let me know if you think something else should be added instead.
Comment #24
fenstratThis looks great, thanks everyone, committed!
Tests are good, I also manually tested. Agree with the removal of the `@todo`, if you feel it should be a comment @Berdir can you reopen with details on that.
Comment #26
stefvanlooveren commentedCan't get this to work in Drupal 10.1. Am I missing something?
1) view > add contextual filter e.g. term reference
2) no options to manipulate/set the contextual filter in the block field
Goal is to manually set my contextual filter e.g. "Show teasers of content with tag X" where X is a value the webmaster can set.
Comment #27
berdirYou need to use Argument validation and have something that provides context.
This is not a filter to set manually. This is for context like current node/term.