Set available paragraph type(s) on a paragraph field

Created on 22 November 2024, 2 months ago

Problem/Motivation

I'm having an issue setting available paragraphs on a paragraph field in the code.
The paragraph selection is not visible in field settings, for example; /admin/structure/types/manage/product/fields/node.product.field_component

Steps to reproduce

Create a content type "product" and add a paragraph field named "field_component".
Create a paragraph of type "text".
Then attempt to add a paragraph of type "text" to the "field_component" field on a "product" content type via code, in a custom module.

// Load the field configuration for 'field_component'.
$field_definition = \Drupal::entityTypeManager()->getStorage('field_config')
    ->load('node.product.field_component');

// Retrieve the allowed paragraph types for the field.
$allowed_values = $field_definition->getSetting('handler_settings')['target_bundles'];

// Add 'text' paragraph type if not already allowed.
if (!isset($allowed_values['text'])) {
    $allowed_values['text'] = 'text';  // Add the 'text' paragraph type to the allowed values.

    // Update the field configuration to save the new allowed values.
    $field_definition->setSetting('handler_settings', [
        'target_bundles' => $allowed_values,
    ]);
    $field_definition->save();
}

The "text" paragraph is added to the "field_component" field on a "product" content type. I can see it when adding a node content, however, the "text" paragraph does not seem to be reflected on the /admin/structure/types/manage/product/fields/node.product.field_component page.

🐛 Bug report
Status

Active

Version

1.18

Component

Code

Created by

🇮🇪Ireland b0gucki3 Limerick

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @b0gucki3
  • 🇮🇪Ireland b0gucki3 Limerick
  • 🇨🇭Switzerland berdir Switzerland

    Our reference handler has two settings, to also control order as well as the inverted stuff. I recommend to change the settings through the UI and then diff the changes with config import/export.

  • 🇮🇪Ireland b0gucki3 Limerick

    That does not really answer my question, as I wanted to set the available paragraphs in a paragraph field using code only, not the UI.

    That said, I figured out how to do it with code alone.

    As @berdir mentioned, there are two handler settings (target_bundles and target_bundles_drag_drop). The target_bundles_drag_drop setting needs to be configured. Here's an example implementation, in case someone else needs it.

    $field_definition = \Drupal::entityTypeManager()->getStorage('field_config')
        ->load('node.' . $content_type . '.' . $field_name);
    
    // Ensure the field exists and has the target_bundles setting.
    if ($field_definition) {
        // Get the handler settings, which contain 'target_bundles'.
        $handler_settings = $field_definition->getSetting('handler_settings');
    
        // Define the allowed types with their configuration.
        $target_bundles = $handler_settings['target_bundles'];
        $target_bundles[$paragraph_type] = $paragraph_type;
    
        // Define the target_bundles_drag_drop structure.
        $target_bundles_drag_drop = $handler_settings['target_bundles_drag_drop'];
        $target_bundles_drag_drop[$paragraph_type] = [
            'enabled' => true,
        ];
    
        // Define the handler settings.
        $handler_settings = [
            'target_bundles' => $target_bundles,
            'negate' => 0,
            'target_bundles_drag_drop' => $target_bundles_drag_drop,
        ];
    
        // Update the field configuration.
        $field_definition->setSetting('handler_settings', $handler_settings);
    
        // Save the updated field configuration.
        $field_definition->save();
    }
    
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024