0

I am trying to figure out why my HTML comment based tokens are not getting replaced in hook_views_post_render(). I have a view where I've got a couple of fields, let's say, nid and the content title. I want to replace the title with something else.

So, I've edited the content title and checked the box to rewrite the output of the field and added something like this:

<!--something-->

Now, in hook_views_post_render() I am doing this:

strtr($output, array('<!--something-->' => 'my changed output');

But the problem is, nothing happens. For some reason, $output doesn't have the HTML comment, it's like it is being stripped out. I have specifically looked at the options for the field in my view and I don't have any of the checkboxes checked that strips out the HTML. I can add other html in $output and they all work fine, so there is no problem with the html going through. So, I am wondering what the issue is. Would appreciate any help on this.

1

It is being stripped -- rewritten views field output still gets run through filter_xss_admin(), which strips out any HTML tag-like construct that isn't in the default allowed_tags list.

Instead of using a <!--something--> format, try using [SOMETHING] instead.

  • Thanks for that, I was thinking along similar lines. But if you look at the Drupal API for hook_views_post_render() (api.drupal.org/api/views/views.api.php/function/…), it says that, "In an ideal world, the actual output will include HTML comment based tokens, and then the post process can replace those tokens." That's what confused me. – SN_26 Feb 23 '15 at 6:19
  • Ah, that is confusing! That should have also mentioned that in the ideal example, that HTML-comment-based token would come programmatically from a preprocess function, a tpl file, or a custom field handler. – Les Lim Feb 23 '15 at 6:26
  • Yes, that would have certainly avoided this confusion. Really appreciate your help, I will go with the [SOMETHING] tag. – SN_26 Feb 23 '15 at 6:41
  • I have a follow up question. Hope you can help :). Ideally, what I want is to print a widget in place of a 'field x' that appears in any view. The widget takes the nid of the node as an argument. What is the best function to use to rewrite the output of the 'field x' any time it appears in any view and print the widget instead? – SN_26 Feb 23 '15 at 6:50
  • It sounds like you might want to override views-view-field-FIELD-NAME.tpl.php. If you click the "Information" link in the configuration for your view, it will show you all of the possible tpl filenames that views will search for to render each field. – Les Lim Feb 23 '15 at 8:08

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.