0

I have an custom tpl.php inside of theme for some node type. It works fine but I would like my custom module to append some HTML after each such node (content of tpl.php). And I don't want to override the output from tpl.php inside of theme (just add something at the end of it).

Is there an hook for that or should I use different approach?

1 Answer 1

1

You can use the node view alter hook in your module to add/change your node object:

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete node content structure has been built.

If you have devel enabled, you can dpm($node) inside your node_view_alter function to see the node structure and then you can add to it as needed from the module.

3
  • Sorry for a bit silly question but should the function be function hook_node_view_alter(&$build) or function mymodule_hook_node_view_alter(&$build)? I tried both but it looks like neither of them is called. Could you maybe provide sample body of this function?
    – PolGraphic
    Nov 23, 2016 at 15:46
  • 1
    Should be mymodule_node_view_alter(&$build) Did you clear caches after implementing this? Can you add your code to your original question? You can also read this excellent tutorial on understanding the hook system Nov 23, 2016 at 16:28
  • Thank you for the link and clarification. After clearing cache everything works fine :)
    – PolGraphic
    Nov 23, 2016 at 16:40

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.