Problem/Motivation

There are cases where you'd want to have a default paragraph set when creating new content.

Proposed resolution

Add a new dropdown field in the widget settings where you can choose the default set.

Remaining tasks

Review, tests

User interface changes

Added a new dropdown field in the widget settings:
New dropdown

Comments

vtcore created an issue. See original summary.

vtcore’s picture

StatusFileSize
new2.86 KB
hawkeye.twolf’s picture

Thank you for making this patch, @vtcore! I am about to try it out. The module README says:

"Paragraphs Sets" currently provides one widget in addition to "Paragraphs".

* "Paragraphs Classic (sets)": like "Paragraphs Classic" but with the option
to configure a default set and select the paragraphs set on entity creation.

But that isn't the case. Maybe it was removed since the README was last updated?

hawkeye.twolf’s picture

Works great! A few notes:

  1. The "Default set" setting only works if the "Default paragraph type" setting is unset (set to "None") instead of overriding it, as indicated by the new "Default set" settings form element description.
  2. The selected "Default set" setting does not show in the field widget's settings summary.
  3. Capitalization + punctuation: The new settings form element description should say:

    Choose a default set. This will override the "Default paragraph type" setting above.

  4. The "Default set" setting should not depend on the "Enable Paragraphs sets" setting, as it's desirable to allow default sets without enabling the Paragraphs Sets form elements.

Patch forthcoming to address at least issue #4, if not all. Thanks again!

stborchert’s picture

Thanks for the work and sorry for the confusion. I thought, that I had implemented this feature before but couldn't find it anymore. As you said, it has been removed while rewriting the whole module to not use a custom widget.

It would be great if your patch could cover the necessary changes in the README, too.

hawkeye.twolf’s picture

StatusFileSize
new5.23 KB
new5.8 KB

You got it, @stBorchert. Patch attached to address the items in my previous comment plus the README updates. Note that I just changed the wording of the "Default set" form element description to indicate that the "Default paragraph type" needs to be set to "- None -"... I think it's more clear that way, so people don't get confused looking at the widget settings and wonder why their "Default paragraph type" isn't getting used. I also refactored the Summary settings alteration to output the human-friendly name rather than the machine name for both the "Limit sets to" and "Default set" settings. Patch and interdiff attached.

hawkeye.twolf’s picture

Hold up, didn't properly test that. Although the "Default set" setting is now exposed without the "Enable Paragraphs Sets" box checked, the "Default set" setting still doesn't take effect without it. Will post a new patch to fix that shortly.

hawkeye.twolf’s picture

Status: Active » Needs review
StatusFileSize
new8.22 KB
new3.93 KB

New patch! Note that I pulled out the getDefaultParagraphTypeMachineName() method, as it was only used in one place and, from reviewing the logic, didn't seem to do what it purported (default_paragraph_type refers to a Paragraph type not a Paragraph Sets type).

pcate’s picture

I just applied this patch and it added the desired functionality.

inedyalkova’s picture

Title: Add support for choosing a default paragraph set » Drupal9 Readiness and compatibility
StatusFileSize
new685 bytes
stborchert’s picture

Title: Drupal9 Readiness and compatibility » Add support for choosing a default paragraph set

@inedyalkocva: please don't mix things up. Your patch belongs to #3156961: Cannot install on Drupal 9.0.1 via Composer.

sfuchsbe’s picture

I can confirm that the patch #8 is providing the expected funtionality. But I'm wondering if this integrates perfectly in the idea of paragraphs especially when having restrictions configured in the entity reference revisions field settings.

Let's just imagine the following scenario:

  • The ERR field is configured to just allow to have a single paragraph type A referenced
  • The field cardinality is set to 1
  • There is a Paragraphs Set configured with paragraph type A
  • In the widget configuration I'm adding this set to the field
  • Now I'm able to use the paragraph type twice which is contradictionary to the configured field cardinality

You can just slightly change the scenario to other use cases that will then allow for using paragraphs that are not initially permitted in the ERR field.

Probably you should think of a separate content entity for paragraph sets that can directly be used as ERR field similar to paragraph types. Then the configuration options would appear more reasonable.
This is just a high level thought using an edge case which might not even be relevant but worth considerin. If this is not the intention of this patch in my opinion you can go ahead with merging to the module. I'm going to use this patch anyway because it is filling a gap occuring in one of my projects.
Thank you for your work.


edit: Keeping this comment for consistency.

sfuchsbe’s picture

Just realized that it is wrong what I stated in my previous comment. But still something strage is happening on my site. Maybe I will find out what I did wrong. If my result is worth to be shared I will provide an update...

...otherwise - good job. Go ahead ;)

sfuchsbe’s picture

StatusFileSize
new10.23 KB
new3.53 KB

I was now able to reproduce what I did wrong and why:

  • I have a field that allows to reference paragraph type A with cardinality 2
  • I want to have both paragraphs added by default
  • So I created a paragraph set containing two elements of paragraph type A
  • I have added this set as default set for the field
  • Issue: I was still able to add a third paragraph A to the field because the cardinality was not respected
  • I decided to reduce the field cardinality from 2 to 1
  • Issue: The default paragraph set was still added to the field because the cardinality was not respected
  • My assumption was that I probably should disallow all paragraph types in the field
  • Issue: The paragraph set was still added to the list of available sets in form display settings because empty allowed paragraphs was not processed correctly

For the issues mentioned above I'm adding a rework of the patch in #8 with the following changes:

  • Check cardinality before adding the default set
  • Remove "add more" when default set consumes all available elements
  • respect paragraphs "real_item_count" after default set was added
  • add condition to entity_form_display_edit_form to only enable default set if default paragraph is not defined
  • reorganized getSets to not add any paragraph set if the field doesn't allow for any paragraph type

Unrelated to this issue one could investigate (should be handled in separate issues):

  • Why is the select box for adding paragraph sets displayed when there is no set available?
  • Why is the entire form element altered even when no paragraph set is involved

Happy to receive your feedback :)

  • sfuchsbe authored a21b9ec on 8.x-2.x
    Issue #3057353 by hawkeye.twolf, sfuchsbe, vtcore, inedyalkova: Add...
stborchert’s picture

Status: Needs review » Fixed

Thank you all for your work (and patience).
I've committed the patch from comment #14 (with one minor necessary change for widgets allowing unlimited items).

sfuchsbe’s picture

Hi @stBorchert,
I just checked your changes that were actually committed to the repository.
paragraphs_sets.module@179ff:

  // Stop further processing if this field does not want the Paragraphs Sets
  // form elements or there aren't any allowed sets for this field.
  if (empty($widget_third_party_settings['use_paragraphs_sets']) || empty($sets)) {
    return;
  }

Actually with this change you introduced something that was not intented at least by me. The idea was that you could use default set even if "use_paragraphs_sets" is unchecked. Therefore the checkbox in settings is not controlling the visibility of the defaults dropdown. With your change one has to check the setting which will automatically add the additionall form elements for managing paragraph_sets. The exit point suggested in #14@332ff:

  // Bail now if this field does not want the Paragraphs Sets form elements.
  if (empty($widget_third_party_settings['use_paragraphs_sets'])) {
    return;
  }

I will create a new issue to revert this change - then it is up to you to include this as an additional feature or decline. I'm going to use it ;)

Status: Fixed » Closed (fixed)

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