Link sharer on Drupal

Printer-friendly versionPrinter-friendly version

I just wanted something like the "share on facebook" button, but to post links on my (this) blog. I thought I would have to write a module to do that but.. no, Drupal itself allows us to do that directly :)

First, the CCK "Bookmark" Content

First of all, I created a CCK content named "bookmark". Here is the exported CCK code.
Download Bookmark CCK here

Second: the "bookmark" button

Yep, I copied this from the facebook "SHARE IT" button.. :)
First, we need to enable the Prepopulate Drupal module, that allows us to prefill the forms by URL. Then, create some javascript code that could do the job:

1
2
3
4
5
6
7
var d=document,f='http://www.example.org/node/add/bookmark-link',l=d.location,e=encodeURIComponent,p='?edit[field_url][0][url]='+e(l.href)+'&edit[title]='+e(d.title);
1;
a=function() {
  if (!window.open(f+p,'drupalsharer','toolbar=0,status=0,resizable=1,width=626,height=436')) l.href=f+p};
  if (/Firefox/.test(navigator.userAgent))  setTimeout(a,0); else{ a()
}
void(0)

(of course, remember to replace www.example.org whit your real site URL)

Then, compress it to one-line and urlencode it. The final result is similar to this:

1
javascript:var%20d%3Ddocument%2Cf%3D%27http%3A%2f%2fwww.example.org%2fnode%2fadd%2fbookmark-link%27%2Cl%3Dd.location%2Ce%3DencodeURIComponent%2Cp%3D%27%3Fedit%5Bfield_url%5D%5B0%5D%5Burl%5D%3D%27%2be%28l.href%29%2b%27%26edit%5Btitle%5D%3D%27%2be%28d.title%29%3B1%3Ba%3Dfunction%28%29%20%7Bif%20%28%21window.open%28f%2bp%2C%27drupalsharer%27%2C%27toolbar%3D0%2Cstatus%3D0%2Cresizable%3D1%2Cwidth%3D626%2Cheight%3D436%27%29%29l.href%3Df%2bp%7D%3Bif%20%28%2fFirefox%2f.test%28navigator.userAgent%29%29setTimeout%28a%2C0%29%3Belse%7Ba%28%29%7Dvoid%280%29%0A

Now, just create a bookmark in your browser pasting the above code as the bookmark URL, and the game is done!

Next step, a view to show bookmarks

I also created a view to display bookmarks. You can see it here.
Download Bookmarks view here

Who Am I?

~redShadow~ A.K.A. Samuele Santi is an Italian Open Source developer, currently working as a freelance developer, mainly in the web applications sector. Favourite programming languages: PHP and, of course, Python!

cocktails (1) bash (11) e-mail (2) audio (1) apt (1) cars (1) debug (1) algorythms (1) curl (1) C++ (2) como lake rovers (1) development (11) cartoons (1) Drupal (21) database (3) address book (2) dmcrypt (1) cryptography (1) Drupal Forms (1) aircrack (1) aoe (1) contact manager (1) alcool (1) doku (1) documentation (2) blender (3) archive (1) blogroll (7) code (3) citroen (1) circuits (1) 2v (1) arduino (1) apache (1) backup (3) camera mia (1) awstats (3) 3d (3) debian (1) caos (1)