4

I try to list all fields where a paragraph type is referenced (to find all contents which have paragraphs of a specific type).

My problem: I do a EntityQuery on field_config entity type to find the fields. But, the condition on which paragraph_type are targeted doesn't work correctly and return all fields which can content a paragraph disregarding the type.

Here my code:

// Recover my paragraph_type selected in my form.
$paragraph_type = $form_state->getValue('paragraph_type');

// List all field_config which referred this paragraph_type.
$query = \Drupal::entityQuery('field_config')
  ->condition('field_type', 'entity_reference_revisions')
  ->condition('settings.handler', 'default:paragraph')
  ->condition('settings.handler_settings.target_bundles', $paragraph_type, 'IN');

$field_config = $query->execute();

For exemple, I have a paragraph type "press_release_about" which can be add only in press release in a specific field. But for this paragraph (and all others), it's all fields which can contains a paragraph in the system which are returned.

When I inspect a field_config which referred paragraph, I can see that settings.handler_settings.target_bundles is an array of the paragraph types which can be add with this field. The format is:

[settings:protected] => Array
    (
        [handler] => 'default:paragraph'
        [handler_settings] => Array
            (
                [target_bundles] => Array
                    (
                        [1col_text] => 1col_text
                        [1col_image] => 1col_image
                        [1col_quote] => 1col_quote
                        [1_col_graphique] => 1_col_graphique
                        [2_cols_texte_media] => 2_cols_texte_media
                        [2_cols_texte_graphique] => 2_cols_texte_graphique
                        [2_cols_text] => 2_cols_text
                    )
              )
         )

If I remove the condition ->condition('settings.handler_settings.target_bundles', $paragraph_type, 'IN'), this change nothing. And if I change the INby NOT IN, I have no result.

I don't know if I don't write my condition correctly or if my error is elsewhere.

0

try to use an array instead of the value of $paragraph_type.

that would be like this :

->condition('settings.handler_settings.target_bundles', [$paragraph_type], 'IN')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.