Is it possible to open the links to twitter users and/or URL's in a new window?

I have a twitter block using Views. I have tried to make the links show up in a new window but I can't find any options for that.

Any ideas?

Comments

abasso’s picture

I personally think this should be by default, anyway, if you are willing to hack the module, you have to change the following lines in the _twitter_filter_text function to:

$replacements = array(
    '><a href="' . $destination . '/${1}" target="_blank">' . $prefix . '${1}</a>',
    '<a href="' . $destination . '/${1}" target="_blank">' . $prefix . '${1}</a>',
    '${1}<a href="' . $destination . '/${2}" target="_blank">' . $prefix . '${2}</a>',
  );

PS. this is only for usernames/hastags, not urls in the tweet, for that use something like external links module.

juampynr’s picture

Version: 7.x-3.0-beta3 » 7.x-3.x-dev
Component: Miscellaneous » Code

The issue mentioned by @abasso has been fixed at #1386518: Hashtag and Account Links Broken (extra slash).

I think it makes sense to open these links in a new window. Best would be to give that option within Views as a filter setting.

Waiting for someone to suggest a patch for this.

juampynr’s picture

Title: External Links » Open @usernames and #hastags in new windows
Status: Active » Fixed

Actually, it does make total sense to open these links in new windows.

Committed to Drupal 6 and 7 versions. Many thanks.

http://drupalcode.org/project/twitter.git/commitdiff/f7a5621
http://drupalcode.org/project/twitter.git/commitdiff/8784ff8

kjholla’s picture

I have made massive changes to the Views that are used to display the tweets. This was done in version 6-3.0 beta 9 though.

My changes do the following:

  1. Open @usernames and #tags in a new windows
  2. Open all URLs in a new window
  3. Display twitpic previews
  4. Embed Youtube Videos

The modified view code uses the Custom PHP Field module to add a Custom PHP field to the Twitter View.

The PHP code in this new field is as below:

Custom PHP Field 1:

<?php
$message = $data->twitter_text;
$tailer = '';
$url_pattern = '(?:\/(?:[a-zA-Z0-9\#\!\/\?&\_\-,\.\=]*[a-zA-Z0-9\/&\_\-])?)?';

if (preg_match_all('/(?:^|[\ ]|[\n])(?:[hH][tT]{2}[pP][sS]?:\/\/)?(?:twitpic\.com)(' . $url_pattern . ')/', $message, $images, PREG_SET_ORDER) > 0)
{
   for($iter = 0; $iter < sizeof($images); $iter++)
   {
      $tailer = $tailer . '<br /><a href="http://twitpic.com' . $images[$iter][1] . '" rel="nofollow" target="_blank" style="word-wrap: break-word"><img src="http://twitpic.com/show/thumb' . $images[$iter][1] . '.jpg" height="100" /></a><br />';
   }
}

if (preg_match_all('/(?:^|[\ ]|[\n])(?:[hH][tT]{2}[pP][sS]?:\/\/)?(?:youtu\.be)(' . $url_pattern . ')/', $message, $videos, PREG_SET_ORDER) > 0)
{
   for($iter = 0; $iter < sizeof($videos); $iter++)
   {
      $tailer = $tailer . '<br /><div style="margin:auto; text-align:center"><iframe width="420" height="315" src="http://www.youtube.com/embed/' . $videos[$iter][1] . '" frameborder="0" allowfullscreen></iframe></div><br />';
   }
}

$message = preg_replace('/(^|[\ ]|[\n])([0-9a-zA-Z\.-]+\.(?:[cC][oO][mM]|[cC][oO]|[lL][yY]|[uU][kK]|[iI][nN])' . $url_pattern . ')/', '$1<a href="http://$2" rel="nofollow" target="_blank" style="word-wrap: break-word">$2</a>', $message);

$message = preg_replace('/(^|[\ ]|[\n])([hH][tT]{2}[pP][sS]?:\/\/[0-9a-zA-Z\.-]+\.[a-zA-Z]{2,4}' . $url_pattern . ')/', '$1<a href="$2" rel="nofollow" target="_blank" style="word-wrap: break-word">$2</a>', $message);

$message = preg_replace('/(^|[\ ]|[\n])([wW]{3}\.[0-9a-zA-Z\.-]+\.[a-zA-Z]{2,4}' . $url_pattern . ')/', '$1<a href="http://$2" rel="nofollow" target="_blank" style="word-wrap: break-word">$2</a>', $message);

$message = preg_replace('/(^|[\ ]|[\n])\@([a-zA-Z0-9\_]+)/', ' $1<b>@<a href="http://www.twitter.com/$2" rel="nofollow" target="_blank" style="word-wrap: break-word">$2</a></b>', $message);

$message = preg_replace('/(^|[\ ]|[\n])\#([^\ ]+)/', '$1#<a href="http://search.twitter.com/search?q=%23$2" rel="nofollow" target="_blank" style="word-wrap: break-word">$2</a>', $message);

echo '<span style="word-wrap: break-word">' . $message . $tailer . '</span>';
?>

Custom PHP Field 2:

<?php
$timedifference = time() - $data->twitter_created_time;

echo '<div style="font-size:85%; font-weight:bold">' . (($timedifference > 64800)? 'Posted' : 'about') . ' <a href="http://www.twitter.com/' . $data->twitter_account_screen_name . '/statuses/' . $data->twitter_id . '" rel="nofollow" target="_blank">' . (($timedifference > 64800)? format_date($data->twitter_created_time, 'custom', 'g:i A M jS, Y') : format_interval($timedifference, 2) . ' ago') . '</a> via ' . preg_replace('/\ ?rel\=\"nofollow\"/i', ' rel="nofollow" target="_blank"', $data->twitter_source) . ($data->twitter_in_reply_to_status_id ? ' <a href="http://www.twitter.com/' . $data->twitter_in_reply_to_screen_name . '/statuses/' . $data->twitter_in_reply_to_status_id . '" rel="nofollow" target="_blank">in reply to ' . $data->twitter_in_reply_to_screen_name . '</a>' : '') . '</div>';
echo '<div>&nbsp;</div>';
?>

Regards,
KH

juampynr’s picture

Could be great if we can get some of that stuff in. There was an old issue where someone asked for twitpic previews.

Could you please write a separate issue per feature with a patch? That way me and the rest of the community can review, discuss and finally commit them.

Thanks!

kjholla’s picture

To be honest, I tried writing a patch to begin with; but failed. Hence, I went ahead and edited the Twitter view using the Views interface on the site. I no longer use the Twitter block and page views that come with the module - I have now modified it to use the above code within the View using the Custom PHP field.

I can give it one more go but this may require someone who is more well-versed with the Views module and how the Twitter module interacts with it.

Regards,
KH

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.