4

I am trying to change the way comments are displayed in drupal 7 for a specific content type. I am using garland theme. Ideally I would like to modify comment display in my module as opposed to doing it in the theme.

I have successfully implemented hook_form_comment_form_alter to hide the author name this way:

function mymodule_form_comment_form_alter(&$form, &$form_state, $form_id) {
  //  krumo($form_id);                                                   
  switch($form_id) {
    case 'comment_node_mycontenttype_form':
      hide($form['author']);
      break;
  }
}

Now I would like to alter the comments to:

  1. Hide the comment title link
  2. Hide the "reply delete edit" links

I tried to implement hook_comment_view_alter() and hook_node_alter() but these seem not to get called. When I use the following code, nothing happens (no krumo outputs and no modifications).

function mymodule_comment_view_alter(&$build) {
  krumo($build);
}
function mymodule_node_alter(&$build) {
  krumo($build);
  hide($build['links']['comment']['#links']['comment-reply']);
}

The only way I found was to modify garland_preprocess_comment() in template.php in garland theme. But this applies to all content type and also I would like to have all my code in my module.

Is there a proper way to do this?

How come hook_comment_view_alter() and hook_node_alter() don't seem to get called?

Note I am using panels to add my content. So I have added the comments and the comment form using panels module. Therefore there is no field to toggle "allow comment title" as there is in structure->content type->comment settings. Even though the "allow comment title" field is unchecked, titles are still displayed. it seems like panels overrides this setting somehow.

2

To put this in my module I actually just did put the following code in my module:

function mymodule_preprocess_comment(&$vars) {
$vars['submitted'] = $vars['created'] . ' — ' . $vars['author'];
switch( $vars['node']->type )
 {
 case 'myContentType':
 $vars['title'] =FALSE;
 $vars['content']['links']['comment']['#links']['comment-reply'] = FALSE;
 $vars['content']['links']['comment']['#links']['comment-edit'] = FALSE;
 $vars['content']['links']['comment']['#links']['comment-delete'] = FALSE;
 break;
 }
} 
0

I can't say this is the "proper" way since technically you'd be modifying a core file, but normally the easiest way would be to create a custom file in your theme's directory called comment.tpl.php and make your modifications there. But, since you're using the Garland theme from core, which already has a comment.tpl.php file... Well, any changes you make to it will be overridden the next time you update to the next release of Drupal. Plus, don't hack core.

That said, assuming you were using a different theme, you could use Garland's comment.tpl.php file as a point of reference and applying some minor modifications like so:

Garland's comment.tpl.php file:

<?php
?>
<div class="<?php print $classes . ' ' . $zebra; ?>"<?php print $attributes; ?>>

  <div class="clearfix">

    <span class="submitted"><?php print $submitted ?></span>

  <?php if ($new): ?>
    <span class="new"><?php print drupal_ucfirst($new) ?></span>
  <?php endif; ?>

  <?php print $picture ?>

    <?php print render($title_prefix); ?>
    <h3<?php print $title_attributes; ?>><?php print $title ?></h3>
    <?php print render($title_suffix); ?>

    <div class="content"<?php print $content_attributes; ?>>
      <?php hide($content['links']); print render($content); ?>
      <?php if ($signature): ?>
      <div class="clearfix">
        <div>—</div>
        <?php print $signature ?>
      </div>
      <?php endif; ?>
    </div>
  </div>

  <?php print render($content['links']) ?>
</div>

Modified version for your theme:

<?php
?>
<div class="<?php print $classes . ' ' . $zebra; ?>"<?php print $attributes; ?>>

  <div class="clearfix">

    <span class="submitted"><?php print $submitted ?></span>

  <?php if ($new): ?>
    <span class="new"><?php print drupal_ucfirst($new) ?></span>
  <?php endif; ?>

  <?php print $picture ?>

    <div class="content"<?php print $content_attributes; ?>>
      <?php hide($content['links']); print render($content); ?>
      <?php if ($signature): ?>
      <div class="clearfix">
        <div>—</div>
        <?php print $signature ?>
      </div>
      <?php endif; ?>
    </div>
  </div>
</div>

After you make any such changes, remember to clear the cache so the changes are recognized by the system.

  • This works but I also need to make it specific to a content type. Not for all comments. Also I would like to make these alterations from inside my module. – Baba May 21 '13 at 2:37
0

I actually found a semi-solution which is to modify garland_preprocess_comment in template.php as such:

function garland_preprocess_comment(&$vars) {
 $vars['submitted'] = $vars['created'] . ' — ' . $vars['author'];
switch( $vars['node']->type )
{
  case 'myContentType':
  $vars['title'] =FALSE;
  $vars['content']['links']['comment']['#links']['comment-reply'] = FALSE;
  $vars['content']['links']['comment']['#links']['comment-edit'] = FALSE;
  $vars['content']['links']['comment']['#links']['comment-delete'] = FALSE;
  break;
 }
}

but I would like to do something similar but in my module instead of in the theme. What hook should i use or how do i get hook_comment_view_alter or hook_node_alter to actually get called in my module?

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.