0

I would like to create "related item" with paragraphs.

I have 2 content type using paragraph field. This paragraph field has different options, like "quotes, related items, slider" etc.

I want to get some fields from "related item". "Related item" field is a "Entity reference".

Inside paragraphs type I have one called "related item" related item

Inside this I have a field type "entity reference"

I have different pages where I can add this paragraph and choose the entity.

I want to get specific fields from this entity type.

I know I can acomplish this with a view, relationship and contextual filters, but I don't how to do this with a paragraph or if it is possible.

1

You may have to jump through several hoops to get the data from the entity reference field.

  // Query entity types  
  $query = \Drupal::entityQuery('node');
  $query->condition('status', 1);
  $query->condition('type', 'your_content_type');
  $entity_ids = $query->execute();

  // Query paragraph types
  $paragraph = \Drupal::entityQuery('paragraph');
  $paragraph->condition('status', 1);
  $paragraph->condition('type', 'paragraph_type');
  $paragraph_ids = $paragraph->execute();

  // Loop through entity IDs
  $paragraph_output = array();
  foreach($paragraph_ids as $pid){

    // Load each paragraph
    $paragraph = Paragraph::load($pid);

    // Load each entity reference field
    $entity_id = $paragraph->get('field_entity_identifier')->getValue();

    // Get entity parent Node ID
    $parent_node = \Drupal::entityManager()->getStorage('node')->load($parent_id[0]['value']);

    // You'll need to insert another foreach loop here drilling down further into the node that is being referenced.

  }

First you have to loop through the content types. Then you have to look to see if those paragraphs are set and have data. Then you loop through the paragraph types data and get the nodes they reference. Then you have to loop referenced nodes.

It's not an exact solution, but it's how I load data in modules and create my own API endpoints for some fairly heavy frontend javascript applications.

| improve this answer | |
  • 1
    Hi, thanks for you reply, this helped me :) – Pedro Apr 30 at 10:23
  • Would you be willing to accept it as an answer or did it not quite answer the question for you? – Jason Glisson Apr 30 at 16:26
  • 1
    Hey, like I said this answear helped me, didn't use all of this, but it was a help for sure :) – Pedro May 4 at 13:15

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.