Status quo (v1.2)
I've got a node with two paragraph fields:
- field_banner
- field_pagebuilder
I've got quite a lot of paragraph types, but basically I can divide them in three groups:
- paragraph types only used in field_banner
- paragraph types only used in field_pagebuilder
- special paragraph types not used in the two fields above
Until version 1.2 I used the field setting "Include the selected below ... (my manual selection of paragraph types)" to limit available paragraph types per parent paragraph field.
E.g. in field_banner, my editors only could add paragraph types "Hero image" and "Hero video". In field_pagebuilder I have dozen other paragraphs like "Text", "Image", "Image gallery", "Google Maps", etc... and those OTOH must not be available as banners.
Adding library feature (v1.3)
I updated to version 1.3 because my editors always wanted to resue paragraphs accross nodes. I still use the default configuration of the Paragraphs library submodule. All paragraph types selectable either in field_banner or in field_pagebuilder are now marked as "Allow adding to library". In the field settings of both, field_banner and field_pagebuilder, I added the "From library" paragraph type to the list of allowed types. The basic functionality is working, I now can promote and reuse paragraphs.
Question: How do I limit available paragraph types in the library per parent field?
The problem is that editors now can bypass the allowed paragraph types for each field using the library feature.
E.g. an editor first creates a hero image paragraph, and promotes it to the library. Then he continues to field_pagebuilder and chooses "Add from library". There he can select the hero image from the library, and could place a hero image anywhere on the page (-builder).
Can this be done by configuration (contextual view filter? entity browser?) or does the library provide a hook for this?
TiA,
Reinhard
Comments
Comment #2
miro_dietikerYeah, that's the fun problem...
We currently don't support any constrains.
Beside the issue of allowed Paragraph types, you also will experience extra levels of nesting if you use Library items which makes validation of nesting depth hard or easy to break.
We already had discussions for a while if this should have been a multivalue field.
Because we didn't find the golden hammer yet that will solve all the problems, we decided not to make use of the bundles at all.
At least there is room to define if we will use bundles for that use case...
One idea was also that templates (new concept) will allow us to easier manage standardised structure and thus could also be used to manage constrains.
Open for inputs. .-)
Comment #3
miro_dietiker:-)
Comment #4
hudriAfter digging a bit through code, and looking at the concepts used, I wonder if I need a library module at all. Paragraphs library might not be what I need, but it is a perfect recipe to create my own solution. I think I can create a library without any code, just through configuration. For my use case as described above:
block_library_XXXblock type per node paragraph field (...that needs it's own limitations/configuration)from_library_XXXparagraph type per node paragraph field (...that needs it's own limitations/configuration).The only thing I'm missing is the entity usage and the promote/unlink feature, but as long as IEF allows in-page creation/editing of library blocks, the most important UX parts should be intuitive enough.
As of now it is only a concept, but I'm going to test it now. Any thoughts?
Comment #5
hudriIn response to #2, how about using the concept of #4 for a more generic solution: Providing multiple different libraries by using multiple different bundles of
paragraph_library_items?By default the library module still ships with one
paragraph_library_itemand onefrom_libraryparagraph type. Any user that needs advanced configuration could create additional paragraph_library_item bundles, and use custom configuration for the view/entity browser/paragraph type/whatever used by that additional bundle.In the UI replace the unique "Promote to library" with multiple "Promote to
paragraph_library_item--bundlenamelibrary" links.If there is only one paragraph_library_item bundle, skip the
paragraph_library_item--bundlename.Comment #6
miro_dietikerThe downside here is that for every Paragraph Type you want to put into the library, you need a library bundle and a paragraph type bundle. We would want to avoid that with our library.
Additionally, we want to ship preconfigured views / also for entity browser and offer improved UX.
The Library supports promotion into the library and unlinking again.
(And yes, usage integration and more..)
It's only a first step where we are, so there is likely more to come once we learn more about real world requirements.
Comment #7
berdirAlso, custom_blocks are *not* designed to be used outside of blocks, they result in blocks being exposed for each and don't have their own template.
What we need to do is expose the entity reference handler settings of the paragraphs field defined in \Drupal\paragraphs_library\Entity\LibraryItem::baseFieldDefinitions(). So basically build a settings UI that does the same as \Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection::buildConfigurationForm() (by creating a plugin instance and calling that method, like \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm()), then we can save that and use it there.
Comment #8
supermoos commentedWould love this too!
Comment #9
tushar tekchandani commented1. Create a View listing of Paragraph Library Items.
2. Add relationship to Paragraph Entity
3. Add contextual filter of field "Paragraph Entity type"
4. Set View type as "Entity Reference"
5. Add Multiple Paragraph types with library item reference fields and use "Views: Filter from an Entity Reference View" in the "Reference Method" setting of the field.
6. Add respective Paragraph type names in the argument field of above field setting.
Following above steps should limit the Paragraph items as per type when creating content.
Hope this helps,
Cheers!
-T
Comment #10
lukusHi - I've encountered this problem myself.
I'm currently investigating some changes that would allow a solution based on multiple library paragraph bundles.
I then aim to define a solution and work on providing the necessary features.
Adding a link here in case this interests anyone: https://www.drupal.org/project/paragraphs/issues/3149904
Comment #11
liquidcms commentedPossibly too simplistic an approach for this but as the library already has a filter for paragraph type; would it not be possible to filter the items which can be added, by the (other) types which are allowed for the field which is being populated?
When the modal with the library view opens; is it possibly not aware of the field which it is filling?
Comment #12
liquidcms commentedNOTE: this is currently only a UI improvement, correct? It isn't actually possible to add the "wrong" type to a field, even though the library allows you to select items of the "wrong" type. When i select a type which doesnt match the other para types allowed for this field, i get a generic error msg (html5 validation is disabled). It doesn't tell me what the issue is; but does prevent me from saving the parent entity (which is good).
Comment #13
sergiurSuggestion in #9 is very useful and it covers our use case (thank you!), though it would still be nice to have a prefiltered entity browser for a more visual experience, especially if the library is extensive. A select will do fine for now though.
RE #12: I believe the issue is you can add any allowed paragraph type to the library, then you create a paragraph reference field on your node to allow, say, 2 different paragraph types to be referenced + the From library paragraph. Since you are allowing the From library paragraph in your parent field, there is no way to add validation to stop the user from referencing any paragraph type via the From library entity browser. No matter what paragraph type you select from the library, it will let you save it because you're allowing the From library paragraph in the parent field. Not sure what you mean by selecting items of the "wrong" type. Where do you define the wrong type is?
Comment #14
nicolasgraph