1

I am trying to print the articles tags in the meta area of the html. I am able to print them, however, they contain all the markup, where as I just need the plain text, comma separated list, much like it prints when using the meta module. Here is what I am using.

function mytheme_preprocess_html(&$variables) {
if ($node = menu_get_object()) {  
  if ($node && isset($node->nid)) {
  $node = node_load($node->nid);
   node_build_content($node);
   $variables['field_tags'] = render($node->content['field_tags']);
   }
 }
}
3
  • what is the content type of field_tags? do you know what the node structure looks like?
    – Jance
    Commented Dec 1, 2014 at 18:45
  • Just article for the content type. As far as the structure, it's just title, body, tags, author, etc. Standard stuff. Hope that is what you meant. Commented Dec 1, 2014 at 19:11
  • Sorry, I meant field type (text, long text, etc), multi valued?
    – Jance
    Commented Dec 1, 2014 at 19:22

1 Answer 1

1

The reason your getting html in your output is because your using render(), which is just a wrapper for drupal_render. Per that function's documentation, it "Renders HTML given a structured array tree."

If you want just something like: Cats, Dogs, Otters

it would probably be better to just pull out those values, ether by looping through the field_tags array and pulling out the value into an array and then imploding your array, or simply looping through and creating a string. So instead of render(), use something like:

$tags = array();
foreach($node->field_tags['und'] as $tag){
  $tags[] = $tag['value'];
}
$variables['field_tags'] = implode(',', $tags);
7
  • Thanks. Yes, I was thinking of this as well, however, I am getting a white screen of death when trying the above or a variation of it. Silly question, but can you do a foreach loop in the preprocess function? Commented Dec 1, 2014 at 20:26
  • You can. It generally helps if I don't put typos in my code though :) Sorry about that. $node['field_tags'] should be $node->field_tags. I'll update my answer.
    – Jance
    Commented Dec 1, 2014 at 20:36
  • Thanks. No more white screen of death :) Only thing is it's not printing the tags, just "tags": ",," Seems to be not getting the data? Here is what I am using so far: function mytheme_preprocess_html(&$variables) { if ($node = menu_get_object()) { if ($node && isset($node->nid)) { $node = node_load($node->nid); node_build_content($node); $tags = array(); foreach($node->field_tags['und'] as $tag){ $tags[] = $tag['value']; } $variables['field_tags'] = implode(',', $tags); } } } } Commented Dec 2, 2014 at 15:03
  • Sounds like your looping though ok, it's just the value of $tag['value'] isn't set. You might wanna look into installing devel on your site, then trying dsm ($tag) from inside the for loop to see what the values in $tag are. Then switch ['value'] with the correct index name (it's probably name or something like that if the field is a term reference)
    – Jance
    Commented Dec 2, 2014 at 15:11
  • Thanks, I was trying to add more to my comment, but ran out. I was thinking the same, need to find the proper 'value'. Commented Dec 2, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.