Problem/Motivation
There is no easy way to alter the view before render from the entity browser. While there are various view alter hooks, they do not have the contextual information from the entity browser.
Proposed resolution
Add an alter hook to alter the view executable before rendering the view within /src/Plugin/EntityBrowser/Widget/View.php.
Remaining tasks
- Review
- Changes?
- Add test
User interface changes
- None
API changes
- adds optional alter hook (api information to entity_browser.api.php)
Data model changes
- None
| Comment | File | Size | Author |
|---|
Issue fork entity_browser-3038854
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
oknateMoving this here from comment #47 on #2790951: Provide for contextual filter argument option on field widget
Comment #3
oknateComment #4
oknateComment #5
oknatewhile changes to the $view->arg are persistent from the hook,
changes to filters are not. When you start to interact with the exposed filters, the changes disappear.
I was able to get changes to existing type filter to work using patch #33 on #2865928: Provide method for views widget to filter based on context and the following code:
So I think what I'm trying to do is still possible, but maybe should be moved out of the View widget.
Comment #6
geek-merlinOK Nate, nice to meet you here! In fact i have done similar custom things when i wanted to inherit a node's group to media (and alter the views filter). I see you did similar original_path guessing.
Hopefully soon we'll migrate to OG and i'll re-do this then with more knowledge. I think this approach to alter the view makes sense.
I think passing the context we can do better though. The entity browser maintains a persistent storage.
What if when we in \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::getPersistentData or \Drupal\entity_browser\DisplayBase::displayEntityBrowser simply add $form_state to persistent data, then we can access all form values, the form, the entity, and more in the alter context.
(Bigger story: This is all about a cross-request equivalent of form_state, which is an issue for bojanz' IEF-2.x too.)
Comment #7
edysmpHi guys! Thanks for working on this.
I needed upcasted parameters on my project for dynamic paramater type checking.
e.g:
I'm attaching a patch to support it. Code taked from:
https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...
Comment #8
edysmpImplemented the same logic for support when the Entity Browser is used as an embed button on the WYSIWYG editor wich works only with the iFrame display.
Comment #12
heddnRebased patch into an MR.
Comment #13
heddnTests are mostly passing now on the MR. At least the same tests that aren't failing on HEAD are now passing. Hiding patches.
Comment #15
pfrenssenThis is looking good to me. I tried it out and it works brilliantly. The context data provided is very complete and helpful. One gotcha was that the Views display is not retained, so switching to another display cannot be done in the normal way (by calling
ViewsExecutable::setDisplay('my_display')).This is not a problem in practice since the widget configuration is also passed by reference in the alter hook, so it can be done by setting
$configuration['view_display'] = 'my_display'.Comment #17
anybodySome very clever people involved here, all comments resolved. So let's merge this in finally.
Comment #18
anybody