0
votes

I'm new to Drupal. I looked here and on google for a while before asking, but I'm sure I can't find the answer because I don't know how to ask the question.

Here is what's going on. I'm using a custom module to load certain entities and then output them in a specific format for an application to access. The problem is that the NODE BODY contains special information and media files that should be converted. My goal is to obtain the HTML output that would normally be used on this field.

// Execute an EntityFieldQuery
$result = $query->execute();
if (isset($result['node'])) {
  $article_items_nids = array_keys($result['node']);
  $article_items = entity_load('node', $news_items_nids);
}

// Loop through each article
foreach ($article_items as $article) {
   return $article->body[LANGUAGE_NONE]['0']['value'];
}

All of this works great. The only problem is that I get things like this in the output:

[[{"type":"media","view_mode":"media_original","fid":"283","attributes":{"alt":"","class":"media-image","data-thmr":"thmr_32","height":"400","width":"580"}}]]

or

*protoss_icon*

My goal is to find a way that these items are converted just like they are when these articles are viewed normally.

I've tried doing things such as:

render(field_view_field('node', $article, 'body'));

or

render($article->body[LANGUAGE_NONE]['0']['value']);

without success. Thanks for any help, I'm learning so I don't have a complete grasp of the process drupal uses to build output.

1
The text is part of the module Media and can be converted to markup with the function media_filter($html); - Kevin

1 Answers

0
votes

You can try something like this (this works only with nodes not with other custom entity types):

$node = node_load($nid);
$field = field_get_items('node', $node, 'your_field_name');
$output = field_view_value('node', $node, 'your_field_name', $field[$delta]);

the field_view_value returns a renderable array for a single field value. (from drupal api documentation)