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']);
   }
 }
}
  • what is the content type of field_tags? do you know what the node structure looks like? – Jance Dec 1 '14 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. – HanSoloShotFirst Dec 1 '14 at 19:11
  • Sorry, I meant field type (text, long text, etc), multi valued? – Jance Dec 1 '14 at 19:22
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);
  • 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? – HanSoloShotFirst Dec 1 '14 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 Dec 1 '14 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); } } } } – HanSoloShotFirst Dec 2 '14 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 Dec 2 '14 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'. – HanSoloShotFirst Dec 2 '14 at 15:12

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.