#2537732: PluginFormInterface must have access to the complete $form_state (introduce SubFormState for embedded forms) introduces subform states, to make sure that parent and embedded forms can work together without knowing anything about each other. Many plugins assume they can access the top levels of the form states they receive, for instance to retrieve submitted values from. If the parent form passes on its own form state to plugin forms, this fails, unless the plugin forms are embedded at the top level of the parent form, which generally is a bad idea due to conflicts with other elements.

To solve this, plugin selectors must create subform states and pass those on to plugin forms instead of parent form states.

Issue fork plugin-2647312

Command icon 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

Xano created an issue. See original summary.

xano’s picture

StatusFileSize
new5.56 KB
xano’s picture

StatusFileSize
new1.96 KB
new5.58 KB
xano’s picture

StatusFileSize
new3.78 KB
new6.99 KB
xano’s picture

StatusFileSize
new18.18 KB
new21.18 KB

This requires #2537732-105: PluginFormInterface must have access to the complete $form_state (introduce SubFormState for embedded forms).

It also improves the naming of the different complete and embdded form and form state variables throughout the codebase, to keep it readable.

xano’s picture

Status: Active » Needs review
StatusFileSize
new21.17 KB

Re-roll, and enabling testing now [#11278695] has been fixed.

Status: Needs review » Needs work

The last submitted patch, 6: plugin_2647312_6.patch, failed testing.

The last submitted patch, 6: plugin_2647312_6.patch, failed testing.

The last submitted patch, 6: plugin_2647312_6.patch, failed testing.

The last submitted patch, 6: plugin_2647312_6.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new12.64 KB
new32.48 KB

This fixes all unit test failures.

Status: Needs review » Needs work

The last submitted patch, 11: plugin_2647312_11.patch, failed testing.

The last submitted patch, 11: plugin_2647312_11.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new27.08 KB
new53.35 KB

Status: Needs review » Needs work

The last submitted patch, 14: plugin_2647312_14.patch, failed testing.

The last submitted patch, 14: plugin_2647312_14.patch, failed testing.

xano’s picture

Status: Needs work » Needs review

Triggering new tests, because the previous ones failed because of something Java on Jenkins.

berdir’s picture

Thanks for the ping. Can't check right now, but I will test this in our environment and against our tests asap.

xano’s picture

The last patch introduces E_USER_DEPRECATED errors to the existing plugin selectors' embedded forms, for when they receive a form state that does not implemen SubformStateInterface. It's by design, to help people migrate and debug, but it may show up quite often in your automated tests.

xano’s picture

StatusFileSize
new53.59 KB

Re-roll.

hamrant’s picture

Priority: Normal » Major
Status: Needs review » Needs work

Trying to apply last patch on plugin 8.x-2.4 and seems it need Re-roll.
My problem is that block field from plugin not save block settings, because in blockSubmit $form_state I get parent form state, and I think this patch should solve this problem.
@Xano, you made great job, thanks, I hope that you will finish this patch
I change priority of this issue because I think it quite critical for this module

sanchiz’s picture

Status: Needs work » Reviewed & tested by the community

This patch is applicable for module version 2.5.

hamrant’s picture

Confirm, on 8.x-2.5 works great, thanks @Xano

bradjones1’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new53.81 KB
new1.05 KB

So I hate to effectively re-open this, though it's been "stuck" in RTBC for over a year. I ran into the issue described in #2994724: Trying to add more than one instance of a plugin field (through paragraphs) prevents saving node and initially thought it sounded like the issue in IEF that spawned another long-RTBC'd patch, #2653574: Unable to keep nested IEF data separate with multivalue fields.. However, applying that had no effect. It appeared the issue was indeed in plugin module.

I tracked this down to the interdiff'ed line in the enclosed patch; PluginSelector::getPluginSelector() stored information regarding the selected plugin in the complete form's storage, using a key of the field name and delta. If you have, for instance, a paragraph type with a plugin field attached, and add more than one in the same complete form, these keys collide. I added a hash of the element's parents, since I think that should be one of the only stable ways of generating a key, since new entities don't have an ID. (I used json_encode() to serialize the parents array since I've read this is faster, and gets a similar effect of a hashable string.)

Status: Needs review » Needs work

The last submitted patch, 24: plugin_2647312_24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

dubs’s picture

Sadly this didn't work for me. I had to manually apply the patch and still the config doesn't save when referenced in paragraphs.

dubs’s picture

StatusFileSize
new65.13 KB

Here's a new patch for the latest version. It does work if you delete the field data and re-add. There are still issues - if you change the plugin type you don't immediately see the config form (if you have one) for that plugin - you have to save the entity and re-edit to see the form. Aside from that, it seems to work okay.

This patch is purely a re-roll of the previous patch from @bradjones1 for the latest version of the module, so thanks for the work Brad :-)

bradjones1’s picture

Status: Needs work » Needs review

Thanks, Dubs. The Form API is my nemesis as well. Bumping to needs review for testbot.

fenstrat’s picture

StatusFileSize
new53.12 KB

Attached is a re-roll of #20 for 2.x-dev/2.7. It's passing locally for me, interested to see what the bot makes of it.

It still doesn't fully fix the issue with paragraphs noted in the previous few comments, I'm still digging into those.

fenstrat’s picture

StatusFileSize
new53.34 KB
new1.13 KB

Attached adds the changes from #24 which seem to be the correct approach to handling unique keys for things like multivalued paragraphs, nice catch @bradjones1!

Not quite sure how to go about testing this without introducing a test dependency on paragraphs?

There's still a (relatively) minor issue with the ajax I haven't got to the bottom of. It's fine on first form load (add/edit), but subsequent requests (e.g. by selecting a different plugin type) the config form doesn't get properly replaced and leaves the old stale form in place.

Also worth noting that we had a custom implementation to expose plugins which extended PluginBase. These are referenced via paragraphs and wouldn't save any config for those plugins with the patch here (others have mentioned that before with paragraphs). It was overriding the submitConfigurationForm() with $this->setConfiguration($form_state->getValue($form['#parents'])) and this is what broke with this switch to Subforms. The fix there was to switch that to simple $this->setConfiguration($form_state->getValues()).

bradjones1’s picture

Issue tags: +Needs tests

Not quite sure how to go about testing this without introducing a test dependency on paragraphs?

I don't think you'd have to pull in all of paragraphs, just have a test that replicates (or even borrows code from) the specific piece of paragraphs that causes this.

Thanks for the review - I'm not super active on the project/subsystem that I discovered this on but I'm hopeful that this will help move the issue forward. Form API is so tricky. Test coverage will surely help.

fenstrat’s picture

StatusFileSize
new53.29 KB

Re-roll of #30. Still needs tests as per #31.

podarok’s picture

Priority: Major » Critical
Status: Needs review » Reviewed & tested by the community
Related issues: +#3174110: TypeError: Argument 1 passed to Drupal\\Core\\Cache\\CacheableMetadata::applyTo() must be of the type array, null given
steffenr’s picture

I can confirm, that #32 also works fine for us in drupal 9. Thx.

dubs’s picture

This patch is still essential to work with config forms on plugins. Can it please be committed?

bradjones1’s picture

Let me see about getting this in and rolling a release this week.

steffenr’s picture

Since nearly a year passed since your last comment, i wonder, if there is any progress in the issue.
@bradkones1: Did you find time checking the patch / rolling a new release fixing the problem?

Thx in advance.

froboy’s picture

StatusFileSize
new53.32 KB
new1.2 KB

It's been a minute. Here's a rerolled patch for 8.x-2.12. Interdiff shows only indexes and surrounding text have changed.

ckaotik’s picture

This is a great upgrade and fix at the same time, thank you! The fix itself still applied to the current version (2.13.0). What's left to get this committed?

stborchert made their first commit to this issue’s fork.