Score:1

How to loop through and build an array from each referenced entity

cn flag

I’m trying to create a card grid component using paragraphs. I have a reference entity which references different card types with unlimited cardinality. I’m able to pull the values from the reference entity with the below code but this only pulls the initial values so I’m only getting data from one entity instead of each individual entity as they are added to the grid. I’m not sure why my foreach loop is not working in my preprocess paragraph function, can anyone steer me in the right direction?

if ($paragraph->getType() == 'card_grid') {
      if (!$paragraph->field_select_card_type->isEmpty()) {
        $field_select_card_type = $paragraph->field_select_card_type->referencedEntities();
        $card_array = [];
        foreach($field_select_card_type as $key => $card_type) {
          $card_title = "";
          $card_image = "";
          $card_url = "";
          $card_meta_description = "";
$card_type_data = $card_type->get('field_select_a_page')->referencedEntities(); 

$node_id = $card_type_data[0]->id();
          $node_data = \Drupal\node\Entity\Node::load($node_id);

          if($node_data) {
            $card_title = $node_data->title->value;
            if ($node_data->bundle() == "article") {
              $card_image = !$node_data->field_image->isEmpty() ? $node_data->field_image->entity->getFileUri() : "";
            }
            if ($node_data->bundle() == "page") {
              $card_image = !$node_data->field_featured_image->isEmpty() ? $node_data->field_featured_image->entity->getFileUri() : "";
            }
            $card_url = $node_data->toUrl()->toString();
          }

          $card_title = $card_type->field_override_title_->value ? $card_type->field_override_title_->value : $card_title;
          if($card_type->hasField('field_override_image')){
            $card_image = !$card_type->field_override_image->isEmpty() ? $card_type->field_override_image->entity->getFileUri() : $card_image;
          }
          $card_meta_description = $card_type->field_override_meta_description->value ? $card_type->field_override_meta_description->value : $card_meta_description;

          $card_array[$key]['card_title'] = $card_title;
          $card_array[$key]['card_image'] = $card_image;
          $card_array[$key]['card_url'] = $card_url;
          $card_array[$key]['card_meta_description'] = $card_meta_description;
        }

        $variables['card_array'] = $card_array;
      }

    }
Score:0
de flag

This isn't really a Drupal question, it's a PHP question, but at a glance, I'd say it's because you are overwriting the $variables['card_array'] on each loop instead of adding to it. You probably want this:

$variables['card_array'][] = $card_array;

Score:0
cn flag

I think this is the minimal code necessary to loop through the referenced entities and get a node from a single value field on the second level:

if ($paragraph->getType() == 'card_grid') {
  $cards = $paragraph->field_select_card_type->referencedEntities();
  foreach ($cards as $key => $card) {
    $node = $card->field_select_a_page->entity; 
    if ($node) {
      // work with $node
    }
  }
}
Shawn W avatar
cn flag
Thank You, I ran through the code again and realized it is indeed working to add the values to my custom array. I now just need to figure out how to loop the values in twig. I've updated my question.
4uk4 avatar
cn flag
Please don't change the topic of the question, ask a new one. But first read https://twig.symfony.com/doc/2.x/tags/for.html
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.