Add rel nofollow to all external links in the node body and comment body

For for node body field use below code:

/**
* Override or insert variables into the node template.
*/
function template_preprocess_node(&$variables) {

  /* Add nofollow */
  if(isset($variables['content']['body'][0]['#markup'])) {
    $html_dom = filter_dom_load($variables['content']['body'][0]['#markup']);
    $variables['content']['body'][0]['#markup'] = nofollowed($variables['content']['body'][0]['#markup'], $html_dom->getElementsByTagName('a'), $html_dom);
  }

}

/* Function to add rel nofollow to external link only */
function nofollowed($str, $tags, $html_dom) {
  if(!is_array($tags)) {
    foreach ($tags as $tag) {
      $this_tag = $tag->getAttribute('href');
      if (0 !== stripos($this_tag, '/') && 0 !== stripos($this_tag, url('', array('absolute' => TRUE)))) {
        $tag->setAttribute('rel', 'nofollow');
      }
    }
  }
  $str = filter_dom_serialize($html_dom);
  return $str;
}

And for comment_body field use below code:

/**
* Override or insert variables into the comment template.
*/
function template_preprocess_comment(&$variables) {

  /* Add nofollow */
  if(isset($variables['content']['comment_body'][0]['#markup'])) {
    $html_dom = filter_dom_load($variables['content']['comment_body'][0]['#markup']);
    $variables['content']['comment_body'][0]['#markup'] = nofollowed_comment($variables['content']['comment_body'][0]['#markup'], $html_dom->getElementsByTagName('a'), $html_dom);
  }

}

/* Function to add rel nofollow to external link only */
function nofollowed_comment($str, $tags, $html_dom) {
  if(!is_array($tags)) {
    foreach ($tags as $tag) {
      $this_tag = $tag->getAttribute('href');
      if (0 !== stripos($this_tag, '/') && 0 !== stripos($this_tag, url('', array('absolute' => TRUE)))) {
        $tag->setAttribute('rel', 'nofollow');
      }
    }
  }
  $str = filter_dom_serialize($html_dom);
  return $str;
}

This is useful for example, we only allow dofollow links only on signature. I use this solutions on Replr.

Adding new variable for the Author Pane (Follow Links)

Here's how to adding new variable for the Author Pane (Follow links). The demo is on Bersosial.com.

function template_preprocess_author_pane(&$variables) {

  $variables['user_follow'] = '';
  $uid = $variables['account']->uid;
  $name1 = 'twitter';
  $name2 = 'googleplus';

  $twitter = db_query('SELECT path FROM {follow_links} WHERE uid = :uid and name = :name', array(':uid' => $uid, ':name' => $name1))->fetchField();
  $googleplus = db_query('SELECT path FROM {follow_links} WHERE uid = :uid and name = :name', array(':uid' => $uid, ':name' => $name2))->fetchField();
  
  if (!empty($twitter)) {
     $twitter = ' <a href="' .$twitter. '"><img typeof="foaf:Image" alt="Follow me on Twitter" title="Follow me on Twitter" src="/sites/all/modules/follow/icons/wpzoom26/icon-twitter.png"/></a> ';
     $variables['user_follow'] .= $twitter;
  }

  if (!empty($googleplus)) {
     $google = ' <a rel="author" href="' .$googleplus. '"><img typeof="foaf:Image" alt="Follow me on Google+" title="Follow me on Google+" src="/sites/all/modules/follow/icons/wpzoom26/icon-googleplus.png"/></a> ';
     $variables['user_follow'] .= $google;
  }

}

After that add the following code in author-pane.tpl.php or advanced-forum.naked.author-pane.tpl.php.

<?php /* Follow */ ?>
<?php if (isset($user_follow)): ?>
  <div class="author-pane-line author-follow">
    <?php print $user_follow; ?>
  </div>
<?php endif; ?>

Module used are Follow, Author Pane, and Advanced Forum.

Latest Reform commit: Exclude UID 1

Here is the latest commit for one of my Drupal module, Reform.

But first, what is Reform module?

Perhaps sometimes you want something simple, such as limiting the minimum length and maximum length of a username. This module allows you to do simple things like that.
function reform_name_validate($form, &$form_state) {
  global $user;
  if (isset($form_state['values']['name'])) {
    if ($user->uid != 1) {
      if ($error = reform_validate_name($form_state['values']['name'])) {
        form_set_error('name', $error);
      }
    }
  }
}

UID 1 will always be able to pass through all the long and the short boundaries of username. Read the release note for Reform 7.x-1.0-alpha2.

Comment Fragment: Rewrite and Redirect Comment Permalink

A few days ago I releasing new module to rewrite and redirect the comment permalink to use a fragment for SEO purposes. The default Drupal 7 comment permalink has the following format, www.example.com/comment/1#comment-1, if a node has 10 comments, each page with the URL /comment/[comment-id] loads identical content to the node itself.

10 comments is fine but how about 1000 comments? 1000 duplicate contents is very bad for SEO and our website may be the target of Google's sandbox. To resolve this issues than I rewrite the comment URLs to www.example.com/node/[nid]#[comment-id] or www.example.com/[pathalias]#[comment-id] than redirect the old URL format to new format.

This module name is Comment Fragment, visit module project page here.

Check if checkbox field is checked (Drupal 7 theming)

Recently, I need the following features for one of client Drupal 7 site:

There is one content type that requires option to change the layout just by using checkbox, what is needed is an inline CSS that are added automatically if the checkbox is checked, so the CSS is added to node.tpl.php directly.

Checkbox created using Field module, the Boolean. Here's the code that I add:

<?php if (!empty($content['field_boolean']['#items'][0]['value']) == '1'): ?>
    <style type="text/css">
      // some css style here...
    </style>
<?php endif; ?>

Change the field_boolean with your field name.