Problem/Motivation

Due Paragraphs are not re usable, we cannot set a default value for a Paragraphs field thus default values are being removed in #2769857: Disable default value of ERR field but user should be able to set a default value to a Paragraphs field.

Proposed resolution

Allow to select a default paragraph type that will be shown for an empty field.
Upload a patch.
Provide test coverage.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#41 interdiff-2770507-37-39.txt3.3 KBtoncic
#41 allow_to_select_a-2770507-39.patch15.13 KBtoncic
#37 interdiff-2770507-36-37.txt1.73 KBtoncic
#37 allow_to_select_a-2770507-37.patch13.91 KBtoncic
#36 interdiff-2770507-32-36.txt7.51 KBtoncic
#36 allow_to_select_a-2770507-36.patch13.42 KBtoncic
#32 interdiff-2770507-28-32.txt3.97 KBtoncic
#32 allow_to_select_a-2770507-32.patch10.2 KBtoncic
#28 interdiff-2770507-23-28.txt5.99 KBtoncic
#28 allow_to_select_a-2770507-28.patch8.33 KBtoncic
#23 interdiff-2770507-19-22.txt3.82 KBtoncic
#23 allow_to_select_a-2770507-23.patch8.57 KBtoncic
#19 interdiff-2770507-18-19.txt1.55 KBtoncic
#19 allow_to_select_a-2770507-19.patch7.83 KBtoncic
#18 interdiff-2770507-15-18.txt5.12 KBtoncic
#18 allow_to_select_a-2770507-18.patch8.36 KBtoncic
#14 interdiff-2770507-9-14.txt4.84 KBtoncic
#14 allow_to_select_a-2770507-14.patch6.77 KBtoncic
#9 interdiff-2770507-6-9.txt4.87 KBtoncic
#9 allow_to_select_a-2770507-9.patch6.6 KBtoncic
#6 interdiff-2770507-4-6.txt3.37 KBtoncic
#6 allow_to_select_a-2770507-6.patch7.29 KBtoncic
#4 allow_to_select_a-2770507-4.patch4.19 KBtoncic
#4 default_paragraph_type_summary.png15.88 KBtoncic
#4 default_paragraph_type.png54.93 KBtoncic
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yongt9412 created an issue. See original summary.

miro_dietiker’s picture

Priority: Normal » Major
Issue tags: +Usability

Yeah, that would be a nice UX improvement!

John Pitcairn’s picture

Not sure if it is possible, but it would be a great UX improvement to be able to set a multiple-item default value, so that (for example) a standard "create page" form can be pre-populated with ready-to-edit paragraph:image and paragraph:text forms.

toncic’s picture

Added select options and raw in summary.

Stopped to work on this because the HEAD are failing.

miro_dietiker’s picture

Status: Needs review » Needs work
Related issues: +#2414865: [META] Reuse paragraphs / Library, +#2826687: [META] Introduce Templates

The widget label and description are repeating and lacking and don't help users understand about its purpose.

Also can't see any test coverage.

BTW for more complex cases such as prepopulating multiple paragraphs, we will have a library and a template, see references.

toncic’s picture

Tried to add paragraph but not success. @yongt9412 should apply patch to debug.

Status: Needs review » Needs work

The last submitted patch, 6: allow_to_select_a-2770507-6.patch, failed testing.

The last submitted patch, 6: allow_to_select_a-2770507-6.patch, failed testing.

toncic’s picture

Showing default paragraph type and wrote test coverage.
But still we need first to fix HEAD fails before this issue.

Status: Needs review » Needs work

The last submitted patch, 9: allow_to_select_a-2770507-9.patch, failed testing.

The last submitted patch, 9: allow_to_select_a-2770507-9.patch, failed testing.

miro_dietiker’s picture

We need to careful: Changing the allowed paragraph type could cause this setting to be out of sync. How are we dealing with this situation?

Ginovski’s picture

+++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
@@ -4,7 +4,6 @@ namespace Drupal\paragraphs\Plugin\Field\FieldWidget;
-use Drupal\Core\Entity\Entity;

@@ -658,8 +678,29 @@ class InlineParagraphsWidget extends WidgetBase {
-

Unrelated change

+++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
@@ -112,6 +112,20 @@ class InlineParagraphsWidget extends WidgetBase {
+      '#description' => 'Default paragraph type '

Use $this->t()

Changing the allowed paragraph type could cause this setting to be out of sync. How are we dealing with this situation?

Had a discussion with @toncic, there are more options to fix this:
When trying to change allowed type (which is set as default)
1. Update the default settings and set default paragraph type to 'None'.
2. Warn the user that the paragraph type is set as default and to change it there first.

toncic’s picture

Fixed test fails and added check if type is allow before showing paragraph.

toncic’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: allow_to_select_a-2770507-14.patch, failed testing.

The last submitted patch, 14: allow_to_select_a-2770507-14.patch, failed testing.

toncic’s picture

Fixing test failing and extract code for testing in new method in ParagrapsAddModesTest class.

toncic’s picture

drobnjak’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

toncic’s picture

Assigned: Unassigned » toncic
johnchque’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -149,9 +164,15 @@ class InlineParagraphsWidget extends WidgetBase {
    +    $summary[] = $this->t('Default paragraph type: @default_paragraph_type', ['@default_paragraph_type' => $default_paragraph_type]);
    

    Everything fine but here would be nicer to show the label of the Paragraph type and not its machine name.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -675,6 +696,30 @@ class InlineParagraphsWidget extends WidgetBase {
    +    $entity_manager = \Drupal::entityTypeManager();
    

    Change this variable to be $entity_type_manager

After that looks fine to RTBC it. ;)

toncic’s picture

Ok. now we want to remove button if is single type allowed and remove description from summary if we don't have default_paragraph_type.
And of course test coverage.

Status: Needs review » Needs work

The last submitted patch, 23: allow_to_select_a-2770507-23.patch, failed testing.

The last submitted patch, 23: allow_to_select_a-2770507-23.patch, failed testing.

miro_dietiker’s picture

  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -675,6 +693,30 @@ class InlineParagraphsWidget extends WidgetBase {
    +    if ($max == 0 ) {
    ...
    +      if ($default_type !== '' && $this->isTypeAllowed($default_type)) {
    

    This logic should only be triggered for new host entities, not for updates.

    Make sure to test cover this, so also do an edit and verify no extra paragraph is added.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1206,4 +1248,57 @@ class InlineParagraphsWidget extends WidgetBase {
    +  public function isTypeAllowed($default_type) {
    

    This should not be needed. The getDefaultParagraphTypeMachineName should already check for allowed ones and never return invalid values.

  3. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1206,4 +1248,57 @@ class InlineParagraphsWidget extends WidgetBase {
    +  public function getDefaultParagraphTypeLabelName(){
    ...
    +  public function getDefaultParagraphTypeMachineName() {
    

    No public, protected if we need it for our widget only.

  4. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1206,4 +1248,57 @@ class InlineParagraphsWidget extends WidgetBase {
    +    if ($this->getDefaultParagraphTypeMachineName() !== '') {
    

    We should avoid '' and instead refer to NULL for emptyness.

  5. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1206,4 +1248,57 @@ class InlineParagraphsWidget extends WidgetBase {
    +      return key($allowed_types);
    

    So even if the selection is "- None -" we propose a default machine name...

miro_dietiker’s picture

There are some UX problems.

I first thought: Yeah, let's always add a default paragraph it only one type is selected and even remove the setting.
But for some optional rarely used paragraphs, this is bad UX. Also we have some other problem (see below).
So we could make auto-add a default setting. But if explicitly set to none, we need to respect that too.

Now the harder part:
If a paragraph is prepared and added by default and the user doesn't fill out any of the fields, we now still save it with empty values.
This is an unexpectedly complex problem. We would need to flag auto-added instances and check the visible fields for emptyness. If all are empty (and it has no children), we could skip creating it. But this check needs to be limited to the auto-added since other paragraphs could have no field at all. For some intentionally added paragraphs, this might not be true and all fields could be empty.
We can put this whole problem into a follow-up. :-)

toncic’s picture

Added items_count instead to check if is new host entity, but still not sure what we want to do with setting. I discussed shortly wit @Berdit and we should continue tomorrow. Test is still failing.

Status: Needs review » Needs work

The last submitted patch, 28: allow_to_select_a-2770507-28.patch, failed testing.

The last submitted patch, 28: allow_to_select_a-2770507-28.patch, failed testing.

Berdir’s picture

Yes, the special case for single type is tricky. Might be a good reason to keep that to the separate issue for this that was created for it. I can see that there might be a use case to not have a default even for single type use cases. But it prevents us from hiding the setting completely for that case and also means we either need to force users to explicitly select the default always (then we can drop the code for this completely but it is more work and gets us further away from the field_collection default behavior) or we need to be able to differentiate between "user didn't decide anything explicitly, so apply single-type default" and "user explicitly opts out of default type". So we need a _default and _none special options.

Best option might really be to drop that part of the logic here and discuss in #2829572: Allow default opt-out for single paragraph type.

5. The way it was implemented (didn't check last patch) was that we would default-select the type for single case and show that in the summary. and wouldn't show anything if none is selected for multi-type.

Now the harder part:
If a paragraph is prepared and added by default and the user doesn't fill out any of the fields, we now still save it with empty values.
This is an unexpectedly complex problem. We would need to flag auto-added instances and check the visible fields for emptyness. If all are empty (and it has no children), we could skip creating it. But this check needs to be limited to the auto-added since other paragraphs could have no field at all. For some intentionally added paragraphs, this might not be true and all fields could be empty.
We can put this whole problem into a follow-up. :-)

I don't think that this is a problem we need to solve. What we need to solve is to be able to expllicitly *remove* the default, so there is nothing shown, which I tested with @toncic and it doesn't work yet. This is very easy to solve on site building level. Make the field in your paragraph type required, then you force the user to decide between having a field and not, instead of saving something that is empty?

toncic’s picture

Added new function for testing that we can set default paragraph type to none. Test are failing because of limited number.

Status: Needs review » Needs work

The last submitted patch, 32: allow_to_select_a-2770507-32.patch, failed testing.

The last submitted patch, 32: allow_to_select_a-2770507-32.patch, failed testing.

johnchque’s picture

Please use that new created method to fix the failing tests setting default_paragraphs as none.

toncic’s picture

Fixed a lot of test failing, there is one more in ParagraphsFieldGroupTest function. Uploading patch that @yong9412 can test it localy.

I'm continue to write test coverage for other functionality.

toncic’s picture

Added test for testing remove default paragraph type.
Logic for single type is not good here, but we deal to discuss that in related issue Add default paragraph for single paragraph type.

miro_dietiker’s picture

Status: Needs review » Needs work

No, we can discuss the single logic in the other issue.

  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -112,6 +113,21 @@ class InlineParagraphsWidget extends WidgetBase {
    +//      '#default_value' => $this->getSetting('default_paragraph_type'),
    

    remove commented lines.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -112,6 +113,21 @@ class InlineParagraphsWidget extends WidgetBase {
    +      '#description' => 'Default paragraph type '
    

    missing t() and skip it as it's repetitive.

  3. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -675,6 +697,33 @@ class InlineParagraphsWidget extends WidgetBase {
    +    if ($max == 0 ) {
    ...
    +      if ($default_type) {
    

    Still only add a new paragraph when creating a new host entity. We never want to have them added when editing existing ones.

DamienMcKenna’s picture

Any chance of this being backported to D7?

Berdir’s picture

Issue tags: +Needs backport to D7

I don't think we will work on a D7, but I would assume that it shouldn't be too complicated to do.

Tagging, so we can keep the issue open after commit.

toncic’s picture

Implemented comm #38 and add test case for new host entity.

Status: Needs review » Needs work

The last submitted patch, 41: allow_to_select_a-2770507-39.patch, failed testing.

The last submitted patch, 41: allow_to_select_a-2770507-39.patch, failed testing.

toncic’s picture

Status: Needs work » Needs review
drobnjak’s picture

johnchque’s picture

Status: Needs review » Reviewed & tested by the community

Just tested again. Looks solid, I tested with two paragraphs fields in the same node, each one with its own default type. Works good. About the single case, we will need to define the behavior in #2829572: Allow default opt-out for single paragraph type. So...

  • miro_dietiker committed 1c91608 on 8.x-1.x authored by toncic
    Issue #2770507 by toncic, miro_dietiker, yongt9412: Allow to select a...
miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed

Committed with a bunch of clean-ups.

Taran2L’s picture

This patch is creating other issue - I have an optional paragraph with a single possible type.

Now, on the edit form - I see a new empty paragraph added, even so I've selected not to add a default one.

Taran2L’s picture

@@ -1216,6 +1263,41 @@ class InlineParagraphsWidget extends WidgetBase {
+    if (count($allowed_types) === 1) {
+      return key($allowed_types);
+    }

Issue lives here. This behavior is undesirable.

johnchque’s picture

I just tested and @Taran2L is right, even if none is selected the default paragraph types is still added. Let's fix that in the followup.

Status: Fixed » Closed (fixed)

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