Problem/Motivation

Steps to reproduce:

  1. There is a paragraph type with at least two plugins enabled, one of them should be style
  2. There is only one style enabled in a style group and set as default
  3. Go to node add form and place a paragraph
  4. Place another instance of the same paragraph type
  5. The page fails with:
TypeError: Argument 2 passed to Drupal\paragraphs\Entity\Paragraph::setBehaviorSettings() must be of the type array, null given, called in /drupal/web/modules/contrib/paragraphs_collection/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php on line 124 in Drupal\paragraphs\Entity\Paragraph->setBehaviorSettings() (line 216 of /drupal/web/modules/contrib/paragraphs/src/Entity/Paragraph.php)."

Proposed resolution

Fix \Drupal\paragraphs_collection\Plugin\paragraphs\Behavior\ParagraphsStylePlugin::submitBehaviorForm() and check whether there is style_wrapper before setting it as behavior settings.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

mbovan created an issue. See original summary.

mbovan’s picture

Status: Active » Needs review
StatusFileSize
new824 bytes

Quick fix.

ytsurk’s picture

Seems fine to me and works.

johnchque’s picture

Just tried this out, cannot reproduce the problem. Was there any extra patch added here?

I cannot reproduce since in the ParagraphsWidget file there is a check to see if the behavior_plugins element was added, having only one style set as default wouldn't go through that check and thus we wouldn't get this error.

[EDIT] Just tested again and this happens when having more than one Style enabled. Updating the issue summary.

johnchque’s picture

Issue summary: View changes
johnchque’s picture

Title: Fix style submitBehaviorForm when there are only style plugin and one style enabled » Fix style submitBehaviorForm when the style plugin is enabled and only one style enabled
StatusFileSize
new3.22 KB
new4.02 KB
new3.05 KB

There we go, this should trigger the bug.

Fixing issue title.

The last submitted patch, 6: 3003983-fix-style-submit-form-6-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php
    @@ -121,7 +121,8 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
    -    $paragraph->setBehaviorSettings($this->pluginId, $form_state->getValues()['style_wrapper']);
    +    $settings = isset($form_state->getValues()['style_wrapper']) ? $form_state->getValues()['style_wrapper'] : [];
    +    $paragraph->setBehaviorSettings($this->pluginId, $settings);
       }
     
    

    you can probably simplify this to $form_state->getValue('style_wrapper', [])

  2. +++ b/tests/src/FunctionalJavascript/ParagraphsCollectionStylePluginTest.php
    @@ -0,0 +1,84 @@
    + * Test paragraphs collection style behavior.
    + *
    + * @group paragraphs_collection
    + */
    +class ParagraphsCollectionStylePluginTest extends JavascriptTestBase {
    +
    

    Do we really need a JS test to trigger this? Might be easier and faster with a plain browser test?

    All the fields are there to in a browser test.

    If it requires JS then we should merge it with the ParagraphsCollectionStyleTest that I added in the other issue.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new2.59 KB
new3.39 KB
new6.18 KB

Ha! Took me longer than expected. Somehow when trying to create the paragraph through the UI, the groups config of the Style plugin was null. Not sure what went wrong there, switched to setting config programatically. :)

The error I got was

1) Drupal\Tests\paragraphs_collection\Functional\ParagraphsCollectionStylePluginTest::testStylePlugin
Exception: Warning: array_keys() expects parameter 1 to be array, null given
Drupal\paragraphs_collection\Plugin\paragraphs\Behavior\ParagraphsStylePlugin->buildConfigurationForm()() (Line: 159)

[EDIT] If happens again I will create an issue.

The last submitted patch, 9: 3003983-fix-style-submit-form-9-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php
@@ -121,7 +121,9 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
   public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
-    $paragraph->setBehaviorSettings($this->pluginId, $form_state->getValues()['style_wrapper']);
...
+    $settings = isset($settings) ? $settings : [];

still too complicated?

Just do $paragraph->setBehaviorSettings($this->pluginId, $form_state->getValue('style_wrapper', [])); the second argument is the default and that already ensures that it is always an array.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new2.45 KB
new3.28 KB

Ha! Didn't notice that, thanks! Updating, no interdiff, too big. :)

johnchque’s picture

The last submitted patch, 13: 3003983-fix-style-submit-form-13-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 12: 3003983-fix-style-submit-form-12-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Berdir committed f6e0af2 on 8.x-1.x authored by yongt9412
    Issue #3003983 by yongt9412, mbovan: Fix style submitBehaviorForm when...
berdir’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.