We Rock Your Web in the Press!

Drupal customize comment subject field?

Home Forums Tech Web Development Drupal customize comment subject field?

Post a New Topic

This topic contains 1 reply, has 2 voices, and was last updated by  Alex Schenker 2 years, 4 months ago.

  • Author
    Posts
  • #3381

    DrupalJunkie
    Member

    By default, Drupal 6 gives me the option to enable or disable the comment subject field. To keep the comment form lean and clean, I prefer to disable the comment subject field for our users. However, that also means that by default, the clickable link text of the comment (ie. comment subject), will simply be the first few words of the first sentence of the comment. The drawback to this is that, in the “recent comments” block (or anywhere else comments are referenced), the comment link title no longer gives a good indication of the subject matter of the comment.

    Therefore – it would be ideal if I could keep the comment subject field disabled to make life easier for my users, but give me the option, while approving comments, to add/ edit the comment subject field.

    Can this be done in Drupal 6?

  • #4386

    Alex Schenker
    Keymaster

    Hi DrupalJunkie,

    Excellent question. There’s two solutions to your approach. The simple one, and the more extended, customizable one. It is possible to actually theme the comment form, giving you more or less complete control over its look and elements. But to simply achieve the functionality you’re looking for, you could also take this approach:

    For content types (such as page) that you wish to have the comment subject field only show up for logged in users that meet a specific role requirement, you can add the following code to your template file(s) (in this example, we use the page content type, or page.tpl.php):

    <?php
    global $user;
    $admin = false;
    $allowed = array(‘content-admin’,'editor’);
    foreach($user->roles as $role){
      if(in_array($role, $allowed)) {
          $admin = true;

      }
    }
    ?>

    <?php if ($admin == false) { print ‘<div class=”logged-out”>’; } ?>
    <?php print $content; ?>
    <?php if ($admin == false) { print ‘</div>’; } ?>

    Basically what this does is wrap the content in a “logged-out” div for users that are not logged in and do not belong within a certain role (content admin and editor in this case). Next, we simply kill the subject comment wrapper for logged out (anonymous) users by adding this statement to our theme’s style sheet (styles.css):

    .logged-out #edit-subject-wrapper { display: none }

    Note that you’ll need to enable the comment subject field for this page content type for this to work. What the code snippet does is check to see if a user is logged in and if that user belongs to a certain role. If they don’t, the comment subject field (which is optional), will simply be hidden from the comment form.

You must be logged in to reply to this topic.