Search code examples
drupaldrupal-7drupal-modulesdrupal-theming

I can't seem to override a function using template.php


I'm trying to alter the comment links in my Drupal output, and I think I have found the function I want to influence, which is function comment_node_view($node, $view_mode).

It is in the Comment module. The problem is I can't seem to effect it, when I try to override it by putting it in my Template.php file and add my theme_ to the function name? In my template.php it looks like this now:

function themename_comment_node_view($node, $view_mode)

if I take off the themename_ it causes an error saying I can't redeclare it. I can copy the comment module and edit it directly but I thought this was how I theme something?


Solution

  • Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks.

    comment_node_view() is a hook, but it's not an alter hook (differently the hook name would end with "_alter").

    Why cannot themes implement hook_node_view()?

    Because hook_node_view() is invoked in comment_build_comment() using the following code:

      // Allow modules to make their own additions to the comment.
      module_invoke_all('comment_view', $comment, $view_mode, $langcode);
      module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
    

    As it is also highlighted from the comment, module_invoke_all() invokes the hooks implemented in modules, not themes.

    If you want to change how a comment is rendered, from a theme, you should create the comment.tpl.php template file for your theme.