Problem/Motivation

It is possible to add all Paragraph Type bundles directly via /admin/content/paragraphs/add/default (ie, the Paragraph Library list) even if the 'Allow adding to library' box is unticked.

Eg, Paragraph Type bundle configuration here:
Paragraph type bundle configuration screen

Yet still shows up:
Add paragraph item screen

Proposed resolution

Prevent using Paragraph Type bundles on the /admin/content/paragraphs/add/default page that are not allowed.

Remaining tasks

Provide patch (coming shortly).

User interface changes

The number of options on the "Add paragraphs library item" form will be reduced.

API changes

None.

Data model changes

None.

Release notes snippet

When adding a Paragraph Library item directly via the library, now only Paragraph Type bundles marked as 'Allow adding to library' will be allowed.

Issue fork paragraphs-3090101

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

scott_euser created an issue. See original summary.

scott_euser’s picture

Assigned: scott_euser » Unassigned
Status: Active » Needs review
StatusFileSize
new3.46 KB

First shot at this. If this is in the right direction, happy to add tests to prove the issue and prove this resolves it.

scott_euser’s picture

Updating issue description with screenshots.

berdir’s picture

The setting currently only limits the action on the paragraph edit form dropdown and not this.

I agree that the UI there is bad but I'm not sure we should be using the same setting for this.

Core kind of provides an API for this, we'd just need to expose the field settings form on the settings page and store it as a field config override.

scott_euser’s picture

Thanks for the feedback. I'll see if I can find the UI and update the patch accordingly.

Should we also then rename "Allow adding to library" to something like "Allow adding to library from the paragraph edit form" as part of the scope of this or do you prefer as a separate issue? Ie, so that the label is not misleading as to its functionality.

berdir’s picture

I'd be fine to change that here as well.

Note that there is no existing setting yet, you basically need to simulate what \Drupal\field_ui\Form\FieldConfigEditForm::form() does here:

    $ids = (object) [
      'entity_type' => $this->entity->getTargetEntityTypeId(),
      'bundle' => $this->entity->getTargetBundle(),
      'entity_id' => NULL,
    ];
    $form['#entity'] = _field_create_entity_from_ids($ids);
    $items = $form['#entity']->get($this->entity->getName());
    $item = $items->first() ?: $items->appendItem();

    // Add field settings for the field type and a container for third party
    // settings that modules can add to via hook_form_FORM_ID_alter().
    $form['settings'] = [
      '#tree' => TRUE,
      '#weight' => 10,
    ];
    $form['settings'] += $item->fieldSettingsForm($form, $form_state);

And $this->entity is a field definition, so we need to get that from the base field definition, similar to content_translation_form_language_content_settings_submit(), $base_field_definition->getConfig(), then use that to build the form, apply the settings to it and then save it.

Not trivial...

scott_euser’s picture

Thanks for the details. I am not fully sure if I understood, so just to double check the expected final outcome.

If you add an Entity Reference Revisions field type to a Node you get this configuration:
Paragraph reference field configuration

But at `/admin/config/content/paragraphs_library_item/fields` the reference field is not available to be configured.

So the goal here is to reproduce the above to allow the above URL to have an interface to be able to override

    $fields['paragraphs'] = BaseFieldDefinition::create('entity_reference_revisions')
      ->setLabel(t('Paragraphs'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'paragraph')
      ->setSetting('handler', 'default')
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'entity_reference_revisions_entity_view',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
        'type' => 'paragraphs',
        'weight' => 0,
      ])
      ->setRequired(TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

from paragraphs/modules/paragraphs_library/src/Entity/LibraryItem.php then:

  1. store that in paragraphs_library config as custom config
  2. create a service tagged with config.factory.override to override the base field configuration
  3. apply the config override at the entity.paragraphs_library_item add and edit routes

Does that sound right or am I going down the wrong path?

berdir’s picture

Sounds right until point 1., 2., 3. :)

That functionality already exists, it's called base field overrides, it's a core config entity type, that is for example used by NodeTypeForm::save() to persist the default value for promote/sticky/status fields per bundle. And content_translation (as mentioned above) to store the translatability flag. you just get the field definition, call getConfig(), set the settings and you're good to go.

It's automatically loaded in and overrides the base field for a specific bundle (paragraphs_library currently doesn't have bundles, so it's just the default pseudo-bundle).

scott_euser’s picture

Status: Needs review » Needs work

Okay great will follow those examples, thanks for all the pointers! This sounds like something I won't manage over a breakfast so I'll need to wait for my next free day on a weekend - might be a bit.

scott_euser’s picture

Status: Needs work » Needs review
StatusFileSize
new6.06 KB

Okay I think I am getting there. The base field overrides don't seem to be affecting the library item add form though, but they are correctly exporting to sites/default/files/sync/core.base_field_override.paragraphs_library_item.default.paragraphs.yml. I think I am clearing the correct cache but also tried a full cache rebuild in case with no joy.

uuid: 0d9f20c7-5005-4bf1-b4a3-6186fbc078ca
langcode: en
status: true
dependencies:
  config:
    - paragraphs.paragraphs_type.from_library
  module:
    - entity_reference_revisions
    - paragraphs_library
id: paragraphs_library_item.default.paragraphs
field_name: paragraphs
entity_type: paragraphs_library_item
bundle: default
label: Paragraphs
description: ''
required: true
translatable: true
default_value: {  }
default_value_callback: ''
settings:
  handler: default
  handler_settings:
    negate: '0'
    target_bundles:
      from_library: from_library
    target_bundles_drag_drop:
      from_library:
        enabled: '1'
        weight: '3'
      test_not_allowed_in_library:
        weight: '4'
        enabled: 0
field_type: entity_reference_revisions

I have checked and the plugin ID I am overriding does appear to be an exact match to the selection handler on the library item add form, so I am not quite sure where I am going wrong.

(no interdiff as it is a completely different approach so 100% is different)

bvoynick’s picture

Thanks Scott, #10 was almost there.

The issue was using the bundle string 'default'. It looks like for bundle-less entities like paragraphs_library_item the entity type id must be reused again as the bundle id. Here's a modified version of the patch that addresses that and is working for me.

Additionally, vs patch #10 I fixed the call to the save() method so that it is called on the field configuration object rather than the field definition object. (I'm guessing you may have had that locally, but trying to save the form was hitting a fatal error for me with #10.)

I also added a task item for the main Manage Fields route, so that the task menu with the link to the Paragraphs field override will show up.

tostinni’s picture

#11 works great, thank you.

I wanted to add a little more visibility to this feature so I added some documentation and took the liberty to change the links to the paragraph library setting screen (which is empty) to this new settings which is a little bit hidden right now.

inversed’s picture

Looks like the #12 patch no longer applies cleanly to the 1.12 version of the module. The #11 version still does, however.

stefan.korn’s picture

I can also confirm that #11 works.

I would find it helpful if the views type filter for the paragraphs library would also leverage this new setting to limit the available paragraph types in the views filter. In order not to clutter this issue, I have created a new one for this at #3207764: Only show allowed paragraph types in the views filter.

mudassar774’s picture

Hi

The patch #11 applied successfully on D9.1.5 (theme Claro) but Allow Adding to Library setting inside Paragraph settings no more effective. The Only place to handle these setting will be /admin/config/content/paragraphs_library_item/fields/paragraph-field-override with some visual issues too.

Not sure if this is the intended behavior or this patch is not meant to be for D9 Yet

Thanks

stefan.korn’s picture

It is intended to have this now configured via /admin/config/content/paragraphs_library_item/fields/paragraph-field-override and not taken from the paragraph type library setting. See @berdir pointing that way in #4

Regarding visual issues, maybe explain or show a screenshot. Might be "Claro" related?

mudassar774’s picture

StatusFileSize
new146.18 KB

hi @stefan.korn

about visuals After Ticking the checkboxes and save them, They remain unchecked visually, though settings applied and works, not sure if Claro issue or something else,

stefan.korn’s picture

hi @mudassar774, not sure why you are seeing these special styled checkboxes. I tried with D9.1.6 and Claro and I got regular checkboxes that work like expected.

Do you have some customization or addons for the Claro theme?

esch’s picture

StatusFileSize
new72.38 KB

I tried applying patch #11 and it still does not limit selection of available paragraph types in list. I've only approved one paragraph type to be added to library but all the options are showing.

Enviro: Drupal 9.2.7, Claro 9.2.7, Paragraphs 1.12.0

Screenshot

pasqualle’s picture

I can confirm #11 works, with the small "visual issue" on the /admin/config/content/paragraphs_library_item/fields/paragraph-field-override page, that the checkboxes remain unchecked after save.

There are some issues in the config
core.base_field_override.paragraphs_library_item.paragraphs_library_item.paragraphs.yml

settings:
  handler: default
  handler_settings:
    negate: '0'
    target_bundles:
      faq: faq
    target_bundles_drag_drop:
      faq:
        weight: '24'
        enabled: '1'
      slide:
        weight: '39'
        enabled: 0

negate and enabled should be boolean (true/false)
weight should be integer

seems like a missing config schema.

Tried to fix the config manually (and import), but the checkboxes are still unchecked on the admin page.

bvoynick’s picture

Status: Needs review » Needs work
bvoynick’s picture

Here's an updated patch that fixes the issues with saved config not being loaded back in properly for form building. It also fixes the issue pasqualle noted with config schema for this selection type not being used, by setting the handler for the overridden field config to be explicitly "default:paragraph" instead of just "default."

I also pulled in elements of #12, particularly the link to the field override route from the main item settings form. Other elements of #12 however did not seem appropriate to include. I don't know what plans there may be to add settings to the settings screen so I removed the replacement of links to that screen since it could still be important some day. I also removed the README text addition - documentation about promotion and this field override would be good, but as it is there is no documentation of promotion in general and I think that would be necessary context for a description of this field override to make sense.

NormySan’s picture

Applied the latest patch but the dropdown still shows all paragraph types even though only a few are allowed similar to #19. I would be happy to help out with this issue to get this done since it's kind of a dealbreaker for using the library.

bvoynick’s picture

Strange, I'm not able to replicate the issue. I was using Gin for testing most recently, which is built on top of Claro - not impossible it's a very Claro-specific issue but seems unlikely.

Are you using any additional Paragraphs ecosystem modules, such as Layout Paragraphs?

The only ecosystem module I've confirmed this patch works with is Paragraphs Browser.

NormySan’s picture

I'm also using Gin and I'm on Drupal 9.3.7. I'm going to try to apply the patch again, could be that I did something wrong the first time around.

NormySan’s picture

The latest patch works well. I misunderstood the functionality and thought that the checkbox "Allow adding to library" would also control the available options in the select list when adding a library item but I had to configure this through the field override settings.

c_archer’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm the patch in #22 works well with the Gin theme. As per the comment on #26 I had to update the Add paragraph field at admin/config/content/paragraphs_library_item/fields/paragraph-field-override to only the show the ones users can add.

berdir’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/modules/paragraphs_library/src/Form/LibraryItemSettingsForm.php
@@ -32,6 +33,9 @@ class LibraryItemSettingsForm extends ConfigFormBase {
     $form['account'] = array(
       '#markup' => '<p>' . $this->t('There are no settings yet.') . '</p>',
     );
+    $form['types_message'] = [
+      '#markup' => '<p>' . $this->t('If you want to control which Paragraph types are visible in the library, go to the <a href=":paragraph_field_override">Paragraph field override</a> settings.', [':paragraph_field_override' => Url::fromRoute('paragraphs_library_item.paragraph_field_overrides')->toString()]) . '</p>',
+    ];
 

That's a setting :)

I don't see why this needs a new form, just put it in the existing empty settings form?

abhijith s’s picture

StatusFileSize
new5.13 KB
new11.47 KB

Updated patch as per #28 suggestion.

abhijith s’s picture

Status: Needs work » Needs review
scottsawyer’s picture

This patch seems to be working well for me.
Restricted the library to a single bundle - only that bundle is able to be created.
Restricted to two bundles - only those two bundles are able to be created.

basvredeling’s picture

Status: Needs review » Reviewed & tested by the community

The "allow adding to library" function is really confusing as #26 mentioned already. I've opened another issue for that: #3319157: Rename "Allow adding to library" to "Allow promoting to library"

Patch in #29 works well.

mahde’s picture

After Drupal 9.5.5 upgrade I am not able to update the Paragraphs selections in paragraphs_library_item and nothing is saved. I am using Gin theme.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

I unfortunately got to another issue that added the save settings button/parent call first, so this needs a reroll.

sergiur’s picture

Status: Needs work » Needs review
StatusFileSize
new5.03 KB

Rerolled to fix the conflict in LibraryItemsSettingsForm.php buildForm return

mahde’s picture

Thanks for the patch but still when I need to update the excluded paragraph types (when I add or remove checkbox) and hit save then it's not saving.

banoodle’s picture

I tried path #35. It applies cleanly, but the problem originally reported in this task persists.

ninobrownh20’s picture

Patch #35 is working for me to resolve this issue, as mentioned above, you need to make the selections for what paragraphs can be added to the library at admin/config/content/paragraphs_library_item I didn't experience any issue with updating the excluded paragraph types, everything saved as normal. Running Drupal 10.2.2 and paragraphs 1.17.0

banoodle’s picture

I saw comment #38 and noticed that I was on paragraphs 1.16.0.

I just upgraded to 1.17.0 and applied patch #35 and now it is working for me!

Thank you!

eduardo morales alberti’s picture

Status: Needs review » Reviewed & tested by the community

  • Berdir committed 5b5bd1f0 on 8.x-1.x
    Issue #3090101 by scott_euser, bvoynick, Berdir, Abhijith S, tostinni,...
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Finally Merged, thanks everyone for sticking with this.

Status: Fixed » Closed (fixed)

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