Search code examples
htmldrupaldrupal-7drupal-theming

Wrap comment reply text into a span tag


Drupal comment reply have the next structure

<li class="comment-reply first">
 <a href="/comment/reply/12/1">reply</a>
</li>

But I want the next structure

<li class="comment-reply first">
  <a href="/comment/reply/12/1">
    <span class="rep">reply</span>
  </a>
</li>

My question is with the function comment_link I can change the markup of this link or I need use a hook or theme function


Solution

  • You can implement hook_comment_view to alter the output of comments. You'll need to add this to a custom module in a .module file

    <?php
    
    function hook_comment_view($comment, $view_mode, $langcode) {
      foreach($comment->content['links']['comment']['#links'] as $delta => $link) {
        // Only apply this new markup to the reply link
        if(strpos($link['href'], 'comment/reply') === 0) {
          $title = $link['title'];
          $comment->content['links']['comment']['#links'][$delta]['title'] = '<span class="rep">' . $title . '</span>';
        }
      }
    }
    

    Remember to replace "hook" at the beginning of the function name to the name of your module. Example:

    function mymodule_comment_view($comment, $view_mode, $langcode) {