I know that I can make new variables available in my node.tpl.php files by using preprocess functions, and I know that I can theme specific fields in a node by using field.tpl.php files. But how do I add a new field to a node programatically, so that it is displayed when print render($content) is called?

I specifically want to add a new field that includes the feed importer that generated the node. I know how to get this data and have hacked something together that works, but I'd like to know if there's a way that I should be doing this. Perhaps I should be adding the field in a custom module rather than in the theme layer?

  • Preprocess hooks are not for adding fields to nodes; you need another hook for that.
    – apaderno
    Commented Jan 26, 2016 at 13:05
  • I suspected as much; which hook should I use? Should I be adding fields in a module, or in the theme layer?
    – Jim
    Commented Jan 26, 2016 at 18:05
  • Definitively, that is a task for a module.
    – apaderno
    Commented Jan 26, 2016 at 18:59
  • If nobody answers earlier, I will answer between 2 hours.
    – apaderno
    Commented Jan 26, 2016 at 19:01
  • I would say theme layer. You talk about print render, which suggest you only want to display this 'field' and not have it persisted in any way or the user to edit or interact with it. If you can compute its value from context you have and only want to display, then that is what theme layer tweaking is all about. Commented Jan 27, 2016 at 7:13


Your Answer

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

Browse other questions tagged or ask your own question.