Problem/Motivation
In my opinion, the alterFormField method of the ViewsReferenceSettingInterface interface should have the $element and $form_state parameters of the widget's formElement method.
In this way, plugins of type @ViewsReferenceSetting would have the widget form element information when the view selector is updated by ajax and thus could modify its behavior depending on the selected view.

Proposed resolution
Modify alterFormField method definition in the interface ViewsReferenceSettingInterface and all plugins of this type, and add the variable $element and $form_state. Finally, modify the alterFormField method call and add these variables.

Comments

aarnau created an issue. See original summary.

seanb’s picture

+++ b/src/Plugin/ViewsReferenceSettingInterface.php
@@ -16,7 +17,7 @@ interface ViewsReferenceSettingInterface extends PluginInspectionInterface {
+  public function alterFormField(&$form_field, &$element, FormStateInterface $form_state);

This is unfortunately breaking the API so we can not do this without breaking existing sites.

The reason we didn't implement this from the beginning is because every setting should technically stand on it's own. I do however think we can pass some data in a non breaking way to fetch the selected view. in #3004636: Add View filter plugin to improve experience for content editor something similar is done.

The only way to do this without breaking the API is adding a new interface extending ViewsReferenceSettingInterface. In that new interface we could add a new method with the new method signature. Not sure what to do with naming though. Any suggestions?

seanb’s picture

I just found #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin which has a very creative solution to work around the BC issues. I think this is a duplicate/won't fix.

gambry’s picture

Yeah, I believe too this is a duplicate/won't fix.

In a revision on #3004636: Add View filter plugin to improve experience for content editor I tried with passing the element and the form_state, but that only complicates the logic.
You can get the current plugin element state as well as the current selected values from the provided $form_field, the info to load the view can be provided with #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin.. so I don't think anything else is needed?

aarnau’s picture

Status: Active » Closed (duplicate)

I agree, both issues try to solve the same thing. And probably with the name of the view and the display id is enough.