Problem

When adding an existing ERR (paragraphs) field to a node or on a paragraph, the default widget is Autocomplete not Paragraphs.

This is different from when the ERR (paragraphs) field is newly created for that paragraph/node, in which case the widget is correctly the Paragraphs widget.

Cause

(according to #5)
ERR uses the property "default_reference_revision_settings" on the Entity Paragraph Annotations to pass these default settings when creating the field, it uses the function "getPreconfiguredOptions" on the FieldType to add those to the list. However, it does not store the selected preconfiguration when creating the field, so when you add that same field to another bundle, it does not know which preconfiguration you picked, and it also doesn't ask which one you want to pick. So then you end up with the same storage settings (as it does copy those), but with another widget (the default for that field type).

Solution

(according to #5)
1. Open a core issue about this.
2. Implement a quick workaround in some hook (if exists) while the core issue is being addressed.

Original report

Shouldn't the paragraphs widget be the default widget for a new Paragraphs field? Currently it's Autocomplete by default.

Perhaps this cannot be achieved given that #2430209: Create own fieldtype for Paragraphs was not adopted.

Comments

jonathanjfshaw created an issue. See original summary.

jeroen.b’s picture

Status: Active » Postponed (maintainer needs more info)

It should be the default widget. What versions are you using?

Field selection

Results in the following form display:
Resulting form display

jeroen.b’s picture

StatusFileSize
new12.54 KB
new22.94 KB

Picture upload, can't reference external images.

jonathanshaw’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new51.34 KB

I was referring to the form display widget. I had some trouble reproducing, but I've got it now:
the issue only occurs when adding an existing ERR (paragraphs) field (to a node or on a paragraph). It doesn't happen when the ERR (paragraphs) field is newly created for that paragraph/node.

Structure->Manage content type->Basic Page->Manage fields
Add field -> Existing field -> Entity Reference Revisions field_paragraphs_demo
Manage form display

Tested today on simplytestme with D8-1.x with paragraphs demo module.

jeroen.b’s picture

Ah yes. I think I remember that.
This is a very hard one.

ERR uses the property "default_reference_revision_settings" on the Entity Paragraph Annotations to pass these default settings when creating the field, it uses the function "getPreconfiguredOptions" on the FieldType to add those to the list. However, it does not store the selected preconfiguration when creating the field, so when you add that same field to another bundle, it does not know which preconfiguration you picked, and it also doesn't ask which one you want to pick. So then you end up with the same storage settings (as it does copy those), but with another widget (the default for that field type).

I think it's best to open a core issue about this, as it's generally odd behavior to experience.
However, we could implement a quick workaround in some hook (if exists) while the core issue is being addressed:

(warning: this code is kind of pseudo-code)

/**
 * Implements hook_field_create_instance
 */
function paragraphs_field_create_instance($instance) {
  $current_field_instances = field_get_instances($instance['entity'], $instance['field_name']);
  if (count($current_field_instances) > 1) {
    $got_all_paragraphs_targets = TRUE;
    foreach ($current_field_instances as $current_field_instance) {

      // When this isn't our own instance, and the field points to something else than Paragraphs, we should not set the widget.
      if ($current_field_instance['bundle'] != $instance['bundle'] && $current_field_instance['field_storage_config']['settings']['target_type'] != 'paragraph') {
        $got_all_paragraphs_targets = FALSE;
      }
    }
    if ($got_all_paragraphs_targets) {
      // Update $instance to get a Paragraphs widget.
      // Ugly, but it will probably work.
    }
  }
}
jonathanshaw’s picture

Unfortunately I don't understand Entity API and its terminology enough to be able to check if that core issue already exists, and create it if it doesn't. Sorry not to be more help with that!

jeroen.b’s picture

I'll check it out tomorrow.

jeroen.b’s picture

-doublepost-

jonathanshaw’s picture

Issue summary: View changes
berdir’s picture

Priority: Normal » Major

After the composite changes, we should try to not only default to our widget, we should hide the other widgets completely, since they must not be used anymore.

While doing so, we should also disallow creating entity reference fields to paragraphs since that will not work anymore either.

I guess we have to do form alters on the relevant core forms.

johnchque’s picture

Assigned: Unassigned » johnchque
johnchque’s picture

Status: Active » Needs review
StatusFileSize
new1.17 KB
new59.72 KB

Added first try, hiding the other widgets.

But not sure about

While doing so, we should also disallow creating entity reference fields to paragraphs since that will not work anymore either.

It means that we should not be able to add entity_reference fields in a paragraph type? or that we should not be able to reference a paragraph type with a entity_reference field?

berdir’s picture

Status: Needs review » Needs work

Yes, it means that if you click on Other.. for a normal reference field, in the select that you get, Paragraph should not be listed as an option.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB
new909 bytes

Added second hook for hiding Paragraph as an option to be referenced by a entity_reference field. Not really sure how to implement a presave hook for set the default widget. Is it really needed?

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/paragraphs.module
@@ -76,6 +76,38 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
+        unset($form['settings']['target_type']['#options']['Content'][$key]);

From Berdir: Missing t() since the key is translatable...

Note that for me without applying this patch, if i select adding a new ERR field to Paragraphs, i get the Paragraphs widget automatically selected.

johnchque’s picture

@miro_dietiker yes, when you create a new ERR field the widget by default is Paragraphs but when you use it somewhere else it is set to Autocomplete. #4 explains the situation better.

miro_dietiker’s picture

Issue tags: +Needs tests

Finally, what #4 describes is a core bug.

Core in FieldStorageAddForm only merges in submitForm the preconfiguredOptions, specifically of entity_form_display (Paragraphs provide, Paragraph.php, default_reference_revision_settings).

Please open the issue and refer to it in the workaround code comment.
Also workarounds like this need tests.. :-)

berdir’s picture

  1. +++ b/paragraphs.module
    @@ -76,6 +76,38 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
    +function paragraphs_form_entity_form_display_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    +  $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($form['#entity_type'], $form['#bundle']);
    +  foreach ($field_definitions as $key => $value) {
    +    if ($field_definitions[$key]->getType() == 'entity_reference_revisions') {
    +      if ($field_definitions[$key]->getSettings()['target_type'] == 'paragraph') {
    +        foreach ($form['fields'][$key]['plugin']['type']['#options'] as $option => $value) {
    +          if (in_array($option, ['options_buttons', 'options_select', 'entity_reference_revisions_autocomplete'])) {
    +            unset($form['fields'][$key]['plugin']['type']['#options'][$option]);
    +          }
    +        }
    +      }
    

    This needs some comments to explain what we're doing and why we're doing it.

  2. +++ b/paragraphs.module
    @@ -76,6 +76,38 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
    +    foreach ($form['settings']['target_type']['#options']['Content'] as $key => $value) {
    +      if ($key == 'paragraph') {
    +        // Entity Reference fields are no longer supported by Paragraphs.
    +        unset($form['settings']['target_type']['#options']['Content'][$key]);
    +      }
    

    'Content' is a translatable string, you need (string) t('Content') in the key.

    Also, the loop is unnecessary, you can just unset() the key directly.

    This comment isn't very clear yet. What's not supported is entity reference fields *to* paragraphs.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new1.83 KB
new1.74 KB

Changes made based on comments above. :)

johnchque’s picture

The last submitted patch, 20: make_paragraphs_widget-2668678-20-test-only.patch, failed testing.

The last submitted patch, 20: make_paragraphs_widget-2668678-20-test-only.patch, failed testing.

The last submitted patch, 20: make_paragraphs_widget-2668678-20-test-only.patch, failed testing.

berdir’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
+++ b/paragraphs.module
@@ -76,6 +76,39 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
+        foreach ($form['fields'][$key]['plugin']['type']['#options'] as $option => $value) {
+          // Unset the widget display options that are no longer supported for
+          // the Paragraphs field.
+          if (in_array($option, ['options_buttons', 'options_select', 'entity_reference_revisions_autocomplete'])) {
+            unset($form['fields'][$key]['plugin']['type']['#options'][$option]);
+          }

Similar to below, I think we can simplify this, just loop over the keys you want to unset and unset them.

Also, $key is a bad variable name, use $field_name or so instaed. Same for $value, which you don't use, you could use array_keys($field_definitions) to avoid an unnecessary variable.

Also missing the core issue that we identified above.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new1.29 KB
new2.85 KB
new2.93 KB

Changed as it should. :)

The last submitted patch, 25: make_paragraphs_widget-2668678-25-test-only.patch, failed testing.

The last submitted patch, 25: make_paragraphs_widget-2668678-25-test-only.patch, failed testing.

The last submitted patch, 25: make_paragraphs_widget-2668678-25-test-only.patch, failed testing.

johnchque’s picture

tduong’s picture

Status: Needs review » Needs work

Looks good. Maybe just a couple of nitpick:

+++ b/paragraphs.module
@@ -76,6 +76,35 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
+        // Loop over the display options of an err field with paragraph target
+        // type.

// Loop over ERR field's display options with paragraph target type.
Probably better above the first foreach loop.

+++ b/paragraphs.module
@@ -76,6 +76,35 @@ function paragraphs_theme_suggestions_paragraph(array $variables) {
+    // Entity Reference fields are no longer supported for referencing
+    // Paragraphs.

// Entity Reference fields are no longer supported to reference Paragraphs.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new2.89 KB
new1.66 KB

Comments changed. :)

tduong’s picture

Status: Needs review » Reviewed & tested by the community

Yep, looks great! :)

miro_dietiker’s picture

Continuing to nitpick.. :-)
For instance you comment being looping, but not why.

+++ b/src/Tests/ParagraphsAdministrationTest.php
@@ -440,6 +440,22 @@ class ParagraphsAdministrationTest extends WebTestBase {
+    // Test that entity_reference field is not supported to Paragraphs.

We are not really testing that it is "not supported". We are more testing that the unsupported situation can not be selected.

johnchque’s picture

Thanks for the feedback. :)

berdir’s picture

Looks good to me.

miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed

Thx, committed. :-)

jonathanshaw’s picture

Status: Fixed » Active

I created a new issue #2724213: Widget is wrong when adding existing ERR field to Paragraph for this, but @yongt closed it as a duplicate of this, so I'll reopen this instead.

The result of this patch, the current state of the module, is that placing an existing ERR field on a paragraph type produces a result that is badly broken, but not obviously so.

More specifically,
- the widget is actually set to ER autocomplete by default
BUT
- the widget claims (on Manage Form Display) to be set to Paragraphs.

I understand it's easy enough to fix this by setting the widget to hidden and back to Paragraphs, and that there is a core issue pending that may address this one day, but my question is:

are you sure you want to ship the stable release of the module in this condition?

I just wanted to be sure the maintainers understood that the "workarounds" discussed above were only partial.

berdir’s picture

Status: Active » Fixed

No, he didn't close it as a duplicate of this issue.

He closed it as a duplicate of the still open core issue: #2717319: Provide better default configuration when re-using an existing field.

Yes, we are aware of the problem, but the problem is in core, and we don't think this is important enough to add workarounds in paragraphs. We will fix the core issue instead.

johnchque’s picture

And also you don't even need to set it as hidden and back again to Paragraphs, you can just re save it.

jonathanshaw’s picture

Great, thanks for the reply, I just wanted to be sure.

Status: Fixed » Closed (fixed)

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

benjifisher’s picture

benjifisher’s picture

There is now a patch to try out on #2717319: Provide better default configuration when re-using an existing field. Once that issue is resolved, re-using a paragraph field should be a lot easier.

gthibert’s picture

mahtabalam’s picture