Would like to have the option to assign "open links in new window" if possible. Great module, btw!

CommentFileSizeAuthor
#2 open_links_in_new_window-1271874-2.patch426 byteswolmi
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

q0rban’s picture

Hey there! As you can see on the project page, I recommend the External module for opening these links in a new window. :)

wolmi’s picture

I think this patch can resolve the problem, better than install a new module without drupal 7 release.

q0rban’s picture

Status: Active » Closed (won't fix)

Wolmi, thanks for the patch, but I'm sorry, I will not be adding target="_blank". It is not valid html. External module is a very simple module that just adds some js to the page. You could look at that js and do something similar. :)

q0rban’s picture

Alternately, you can use a custom theme function in your theme to add target in the way you want. :)

rojesaga’s picture

Issue tags: +Follow, +open in new window

Thanks for the unoffical patch, works great for me. Is just I was looking for. And yes, is better than install a new module that is not ported to D7

defconjuan’s picture

The following solution DOES NOT require adding the external module; DOES NOT hack the follow module; and is valid HTML5. We basically use a theme function override to override the functionality of follow's theme_follow_link function.

Say you're using a theme called facepalm.

Step 1

Add the following code to the bottom of facepalm's template.php file:

function facepalm_follow_link($variables) {
  $link = $variables['link'];
  $title = $variables['title'];
  $classes = array();
  $classes[] = 'follow-link';
  $classes[] = "follow-link-{$link->name}";
  $classes[] = $link->uid ? 'follow-link-user' : 'follow-link-site';
  $attributes = array(
    'class' => $classes,
    'title' => follow_link_title($link->uid) .' '. $title,
    /* The following line is the only line added/different from the stock function defined with 'follow' */
    'data-popup' => 'true',
  );
  $link->options['attributes'] = $attributes;
  return l($title, $link->path, $link->options) . "\n";
}

Step 2

Add the following jQuery-dependent JavaScript to facepalm's html.tpl.php somewhere before the closing </head> tag:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $("a[data-popup]").live('click', function(e) {
            window.open($(this)[0].href);
            // Prevent the link from actually being followed
            e.preventDefault();
        });
    });
</script>

Notes

  • In Step 1, you may be tempted to use 'target' => '_blank', instead of 'data-popup' => 'true',. Don't. target="_blank" is not valid HTML5. But, if you did this, you would NOT require Step 2 or jQuery.
  • This solution requires jQuery (but it's Drupal and everything requires jQuery nowadays.)
  • Bonus: You can add the attribute data-popup="true" to any link in your site and it will now open in a new window.