0

I have a module that's making custom RSS feeds for me.

In a content type, I have my tags stored in "field_tags" entity reference field.

My goal is to get the names of all the tags, and ultimately put them into an array, so they can go into a markupless RSS field.

When I'm pulling the data, I get it with this:

foreach ($this->dbh->selectVideos(self::RPP) as $nid) {
  $node = $this->entityMgr->getStorage('node')->load($nid);

  if (!empty($node)) {
    $data->nodes[] = [
      'title'    => $this->filterTxt($node->title->value),
      'body'     => $this->filterTxt($node->get('field_paragraph')->value),
      'body2'     => $this->filterTxt($node->get('field_paragraph')->value),
      'created'  => date('D, d M Y H:i:s', $node->created->value) . ' GMT',
      'guid'     => $node->id(),
      'img'      => $this->getImgUrl($node, self::IMG_FIELD),
      'keywords' => $this->filterTxt($node->get('field_tags')->value),
      'video'    => $node->get('field_vid_url')->getString(),
    ];
  }
}
return $this->render($data, 'nameoffeed');

}

The problem is that 'field_tags' is an entity reference field, so it's turning up blank.

I had partial success with this:

    if ($node->hasField('field_tags')){
      $entity_ref = $node->get('field_tags');
      if ($entity_ref->count() > 0){
        $tagoutput = entity_view($entity_ref->entity, 'default');
      }

Then setting 'keywords' to equal $tagoutput. This grabs the first tag, which isn't great as I need them all, and then along with it comes a whole html layout enclosure that won't work for an RSS feed.

I'm confused what the correct way is to get a proper list of tag names. In D6 I had this list as plain text, which wasn't great because you wouldn't be able to click on one in the page view and be taken to a list of all pages with that tag, but at least the tags worked on the feeds easily.

Any thoughts?

2 Answers 2

0

You can get the array of reference entities from node using

$node->get('field_tags')->referencedEntities();

So the code will look something like below:

// Get the referenced entities
$tags = $node->get('field_tags')->referencedEntities();
// Check if we have output. (Assuming we get an array, You will have to check what your output it.)
if (!empty($tags)) {
  // Go through each entity.
  foreach ($tags as $tag) {
    // Write your Business Logic here to create your Keywords Array.
  }
}
// Use the Keywords Array for further use.

I Hope this helps you.

0

How do I get a plaintext list of tags from a multi value entity reference field?

$tag_names = [];
foreach ($node->field_tags->referencedEntities() as $term) {
  $tag_names[] = $term->getName();
}

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.