I have a field in my nodetype called 'original url'. I fill this, when I create a node, with an external url. Then when I save this node, I want that url to be shortened (I use bit.ly) and the short url be displayed in the node (instead of the original url). I have no idea how to do this.
The block is useless to me, I want it in the node creation, not somewhere outside of it. I tried using rules, but I can't see how that works. I tried using 'set a data value' and then using php to call 'shorten_url', but this doesn't work with the replacement patterns and if I put a link directly in the code it makes the short url, but doesn't display it.
Can someone help me out?
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | errorlink_not_shorten.png | 1.26 KB | eboss |
| #4 | errorlink.png | 9.28 KB | eboss |
Comments
Comment #1
icecreamyou commented#1203620: Integrate Shorten URLs with Link
Comment #2
Lios commentedWell, it took me a while to figure out, seeing that link uses a different module to create links (short url, instead of shorten url, which uses a different function), but I finally got it. Maybe this will help someone else (drupal 7):
Get Shorten URLs (obviously).
Get Link.
Get Custom Formatters.
Create a content type to use the link in, or add the field to an existing type. I made a new type and added a field labeled 'Link', named 'field_link' with field- and widgettype Link. Create a node of that type and don't forget to fill the link field.
Create a custom formatter. Choose a name ('Shorten URL'), use format PHP, select the field type you want to use (in my case 'field_link', which I just created), and put in the formatter:
return l(shorten_url($original = $variables['#items']['0']['url'], $service = 'bit.ly'), shorten_url($original = $variables['#items']['0']['url'], $service = 'bit.ly'));I repeated the 'shorten_url' part twice, because the first one is the title you see and the second is the actual link and I need both to be the same. You could use a different title, just replace the first part, before the comma. If you want to know where to find the variables, install Devel. You can then see the variables in the preview (just click the yellow bar to expand). As you can see I use the service bit.ly, but obviously just enter here whatever you use.
Open the Preview tab and select the entity type 'Node', the bundle is the node type, the field is the link field you created and the entity is the node you created before. Hit Preview and you should see the correct short url.
Now save this and go back to your content type. Go to manage display. You see here the field you made for the link and the format it is in. Select your custom format here (in my case Custom: Shorten URL) and save. That's it! Now every time you create a node of that type, it will automagically convert the link you entered to a short URL and display that instead!
Comment #3
icecreamyou commentedNice solution.
Comment #4
eboss commentedI have followed these steps but the result like this -> http://www.pasarplus.com/www.tinyurl.com/cormjof, where there are 2 times www. so when the click does not lead to the url it should be. Can help me fix it?. Here I include the picture.
Comment #5
icecreamyou commentedMake the link absolute:
Comment #6
eboss commentedthanks in advance, www now not appear twice. However, new problems arise now, where links are inserted are not shortened by bit.ly, which I have already signed up and enter the username and the bit.ly API key. Please help again: (