I'd like to know when a field from a node it's a paragraph, I only get the type, that it's entity_reference_revisions, I use this method to get the information:


But I'm not able to get this information

 [settings:protected] => Array
        [target_type] => paragraph

The only way I get it's to check directly to the database to see it the name of the field it's in the table paragraphs_item_field_data.




You're probably looking for the EntityFieldManager service. It is available from the container as entity_field.manager. Its EntityFieldManager::getFieldDefinitions() method returns the field definitions for all fields that belong to a certain entity type and bundle (content type), keyed by their field names:

$definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
if (
  && $definition = $definitions['field_to_check']
  && $definition->getSetting('target_type')
  && $definition->getSetting('target_type') == 'paragraph'
) {
  // Is a field of a 'node' entity, belongs to the bundle
  // 'page', has the 'target_type' property, and finally
  // references paragraphs.

If you load the field storage definition using FieldStorageConfig::loadByName() only, you need to be careful: Field definitions can be different for the various bundle (content) types. So you'd need to additionally check, whether your bundle (content type) matches the bundle of the node you are investigating the reference field for.

| improve this answer | |

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.