Theming Twitter views

Last modified: April 11, 2009 - 06:36

Using Twitter module with Views2 on a membership site and needed a way to link back to members on Twitter, so wrote this views-view-fields--tweet.tpl.php override that links each tweet back to site member's Twitter account:

<div class="twitter_users_picture">
<a href="http://www.domain.com/<?php print $row->users_name; ?>"><img src="http://www.domain.com/<?php print $row->users_picture; ?>" /></a>
</div>

<div class="twitter_users_name">
<a href="http://www.domain.com/<?php print $row->users_name; ?>"><?php print $row->users_name; ?></a> says:
</div>

<div class="twitter_text">
<?php print $row->twitter_text; ?>
</div>

<div class="twitter_created_time">
Tweeted <?php print format_date($row->twitter_created_time); ?>
</div>

<div class="twitter_screen_name">
<a href="http://www.twitter.com/<?php print $row->twitter_screen_name; ?>">Follow <?php print $row->twitter_screen_name; ?> on Twitter</a>
</div>

I'm not a programmer or themer, so there are probably better ways to do this but it solved some output formatting issues for me. I'm not good enough to get the @names in the tweets to link, although it would be nice!

Making @names and links work

dangibas - September 12, 2009 - 17:25

Hi!

You can try and integrate this to the module... it turns urls in tweets into proper linked urls and also the @whoever into proper links too...

I've tested it and it works stand alone and also in Wordpress - In Drupal not tested yet because of caching issues in my original code. Full working version (standalone) available here - http://hygen.net/blog/?p=536

<?php
$PrintMe
= $Variable_Containing_Twitters_Text;
$PrintMe = url2link ($PrintMe);
$PrintMe preg_replace("/@([A-Za-z0-9_]+)/","<a href='http://www.twitter.com/$1'>$0</a> ",$PrintMe) . '';
print
$PrintMe;

       
       
/* Functions */
       
       
function reduceurl($url, $url_length) {
       
$reduced_url = substr($url, 0, $url_length);
        if (
strlen($url) > $url_length) $reduced_url .= '...';
        return
$reduced_url;
        }
        function
url2link($linktext) {
       
$linktext = preg_replace("#(([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*))#e", "'<a href=\"$1\">' . reduceurl(\"$3\", 25) . '</a>'", $linktext);
        return
$linktext;
        }
?>

Dan Gibas, HYGEN

 
 

Drupal is a registered trademark of Dries Buytaert.