A different twitter widget for each #drupal node
I'm putting together a site that has a content type called Company. Each company may or may not have a twitter account where they broadcast updates. The requirement is to display a twitter widget on each company node if one exists.
My first thought was to search through the list of custom modules instead of trying to reinvent the wheel. I found several twitter-related modules, however nothing seemed to fit the bill. The closest module was the twitter module but that connects users accounts to their twitter accounts. I need something to connect companies (as nodes).
Later through a google search I found that twitter.com offers widgets, and specifically a profile widget. The profile widget, which you can now see on the right side of this blog, provides the html/script code to place inside of your website to display the twitter feed of any account. We can use this widget and create a drupal block with some PHP code to pull the twitter account from the company node and display this widget.
I'm going to assume that the reader has some basic knowledge of the CCK module and how to create a drupal block.
Step 1 - Adding twitter account field to the company content type
Head on over to Administer > Content Management > [Content Type] (admin/content/node-type/[content-type]/fields) and select the Manage Fields tab. Towards the bottom of the screen we'll add a new field. I gave mine the label of Twitter Account and field_twitter_account. Use the single line text field/widget type. Twitter accounts can be up to 20 characters so I set the field to allow a max of 20 characters of plain text.
Step 2 - Creating your content
Now that we have our content type created, let's create some content. Create a new node and populate the twitter account field. Go ahead and create several nodes if you like.
Step 3 - Create the Twitter Widget
Head on over to the twitter profile widget and follow the instructions provided to create the twitter profile widget. When you are done copy the code provided for later use.
Step 4 - Create the Twitter block
Head on over to Administer > Site Building > Blocks (admin/build/block/add) and select to add a new block. In the body of the block we are going to add the code copied from the twitter profile widget.
<?php
$node = menu_get_object();
$twitter_acct = $node->field_twitter_account[0]['value'];
if (!is_null($twitter_acct)) {
?><script src="http://foggyperspective.com/%3Ca%20href%3D"http://widgets.twimg.com/j/2/widget.js">http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
version: 2,
type: 'profile',
rpp: 6,
interval: 6000,
width: 'auto',
height: 200,
theme: {
shell: {
background: '#333333',
color: '#ffffff'
},
tweets: {
background: '#000000',
color: '#ffffff',
links: '#4aed05'
}
},
features: {
scrollbar: true,
loop: false,
live: true,
hashtags: true,
timestamp: true,
avatars: false,
behavior: 'default'
}
}).render().setUser('<?php
echo $twitter_acct;
?>').start();
</script><?php
}
?>
The code above in blue is PHP code that has been manually added to the code provided by the twitter profile widget. The PHP code does several things. It first loads the node object and then the twitter CCK field we created. If the field is not null then it will display the twitter profile code. Within the twitter profile code we also add the twitter account value to use.
Note: Make sure to select the input format of PHP code so that the block can evaluate the code.
Step 5 - Add the block to a region in your theme
Now that we have a block created we can add the block to any region within our theme. Make sure to make the block visibility settings such that it will only display on node pages for the content type we're interested in. If you are unfamiliar with how to do this, check out some php block visibility snippets or use the excellent Context module.
Step 6 - Drink Beer!
This is the most important step in the whole tutorial. Let's not forget to follow it.


10 comments
Thanks for a great article. I
Thanks for a great article. I have a similar requirement.
I am just wondering what is the purpose of creating an additional field (field_twitter_account) if you are putting the entire code in the Block?
Also, can this be done using Panel 3? So you would create a view first and give Node ID as arguments. Then in the Panel for add content, you add content from the view just created. How would you prevent 'hard coding' so it is suitable for any Company Node Type?
Cheers.
Good question.
Good question.
The reason for the field_twitter_account field is to tie a twitter account to a node (company, user, etc.) Then the block dynamically builds the twitter widget based on that field. This way you don't need to create different blocks for each twitter account. You create one block and it displays the correct twitter widget.
I haven't tried doing this with Panes so I'm not quite sure on that question.
But if you're trying to avoid 'hardcoding', the solution in this tutorial is to avoid that hardcoding. You might be hardcoding the twitter widget code in the block, but you're dynamically building the components of that widget to get the correct twitter account.
I have another article (http://foggyperspective.com/article/twitter-widget-using-computed-field-...) that shows how to use the computed cck field. You can store the widget code in the computed field which will also dynamically build the twitter widget based on that node's twitter account.
Thanks for the reply.
Thanks for the reply.
Can something similar be done for a Facebook,Youtube,Flickr for a 'Person' Content Type?
Cheers,
Is sure could... assuming
Is sure could... assuming that those other sites provide 'widgets' of some sort. Basically all you're doing is looking at the widget code a site offers, finding the 'variable' in that code and then dynamically populating that variable with a node field.
Hey, me and my friend are in
Hey, me and my friend are in the process of making a website, and we want to make a twitter widget for the website... But not the kind of widget that just shows when you tweet.. But we want the kind of widget that shows when someone tweets a certain word... For example our website is for a celebrity... So whenever somebody. Tweets with that celebrity's name in it... It would show up on the widget box! Is there a way to get that kind of widget because I've seen it on a website, but I don't know how to get it
Hey bibi... that should be
Hey bibi... that should be fairly simple to accomplish with Drupal.
There are a couple ways to go about this depending on your needs. If you simply want a block / twitter widget that shows all tweets based on a pre-defined search then take a look at the twitter search widget over at http://foggyperspective.com/article/twitter-widget-using-computed-field-.... You can define what the search criteria is and how you want the widget to look. There are also advanced search options. Then you simply need to copy that twitter provided code into a new block and display the block where ever you want on your site.
Now if you want to have a block that is specific to the node being displayed then we can use the CCK Computed field module. Take a look at my other article that creates a twitter widget by node: http://foggyperspective.com/article/twitter-widget-using-computed-field-...
What you'll do is create a node for each celebrity. Add a field to capture the twitter search term. Then follow my other article example with your new field.
Good luck!
is this snippet works on
is this snippet works on drupal7?
I'm using this snippet on
I'm using this snippet on Drupal 7. Most of the code is provided by Twitter except where I've thrown PHP in to dynamically pull the twitter account. So depending on how you implement the twitter username field on Drupal, your PHP may vary slightly.
Thanks for reply. Please send
Thanks for reply. Please send the php snippet in drupal 7
Thanks for the easy solution.
Thanks for the easy solution. I was looking for something similar and relied too much on existing modules. I did a lot of searching, downloading, tinkering and was never satisfied for something so easy. Finally found this page via Google and I'm very happy!
Nice to know that I can do this with other widgets (yes I'm still new but loving every minute)!
Thanks again!
Post new comment