Hi,
I see this post: http://drupal.org/node/608810#comment-2532040, but this is on D6.
Could this also be done on D7 please using Link field?
Thanks a lot in advance for your reply!
Greetings,
Martijn

CommentFileSizeAuthor
#4 custom_formatter_shorturl.jpg85.99 KBsummit
#2 Linkshorturl.zip771 bytessummit

Comments

summit’s picture

Hi, I am not a programmer, but maybe one of you can built a formatter from this piece of code from An abstract of à solution On the twitter module?
http://drupal.org/files/issues/twitter-245453-1.patch

If so thanks a lot in advance!

Greetings, Martijn

summit’s picture

StatusFileSize
new771 bytes

Hi,
1) I patched shorturl to D7: http://drupal.org/node/1033046#comment-5400618
2) I enabled custom formatters on D7 17 okt. dev version
3) I added the instructions from http://drupal.org/node/608810#comment-2532040 for the link module custom formatter

return l($element['#item']['title'], shorturl_shorten($element['#item']['url']));

I needed to change this to:

return l('#instance']['settings']['title_value'], shorturl_shorten(['#items']['url']));

for not getting errors as: Notice: Undefined variable: element in eval() (line 1 van /httpdocs/sites/all/modules/custom_formatters/engines/php.inc(79) : eval()'d code).

What I want is that the static link field title is shown with the shorturl as link-url-field.

Now I do not get an error. But I also not see the formatter.
What do I do wrong please?

Attached the custom module I made as the method shown on: http://drupal.org/node/608810#comment-2532040

Can anyone help please to get this formatter working?

summit’s picture

Hi, I see this issue where may be something can be learned to get it working: http://drupal.org/node/1110132#comment-4837998

EDIT: tried the following as formatter:

$linkshorturl = shorturl_shorten($variables['#items'][0]['url'], TRUE));
$output = '<a href="' . $linkshorturl .'">' .  $variables['#instance']['settings']['title_value'] . "</a>';
return $output;

$variables['#items'][0]['url'] because of this issue: http://drupal.org/node/1110132#comment-4588726

I am on travel now. Please help to get this fixed. Would be awesome to be able to use shorturl as custom formatter for a link-field!
Greetings, Martijn

summit’s picture

StatusFileSize
new85.99 KB

Hi, I am still trying this, and I have some results.
1) I changed back the formatter to:

return l($variables['#object']->title, shorturl_shorten($variables['#items'][0]['url']));

2) I had to remove split() and replace it by explode() in shorturl.engine.inc

Now I see the title and as url the shorturl.
A) But clicking on this shorturl, I am not redirected to my long-url, which should be the action.
EDIT: Module short url (on homepage http://drupal.org/project/shorturl) says: http://example.com/sDf will redirect to the original long URL with HTTP 302 code
But I got the shorturl, but it is not redirected outside the site. the long url is an external url.

B) I would like to use the variable in attached image. How should I handle this instead of $variables['#object']->title?

Can somebody help with this please?
Greetings, Martijn

deciphered’s picture

Hi Martijn,

The variable in the attached image should also be available in the $variables object, just do a DPM of that in your Custom Formatter and then navigate to the object appropriately. If you need help, just stick dpm($variables); above your existing formatter code then supply me with an image of the expanded output so I can help.

Cheers,
Deciphered.

summit’s picture

Hi Deciphered first a great 2012 to you!

1) See here where the variabel is:

#instance (Array, 13 elements)
   label (String, 10 characters ) ProductURL
   widget (Array, 5 elements)
   settings (Array, 8 elements)
        validate_url (Integer) 1
        url (Integer) 0
        title (String, 5 characters ) value
        title_value (String, 25 characters ) Boek de aanbieding vanaf   <== I want to output this variable

What should I put into the custom formatter?

Formatter itself works, only not with url variable /?= ..I think that is a shorturl problem.

Greetings, Martijn

deciphered’s picture

Based on that output the path to the variable you want is:

$variables['#instance']['settings']['title_value']

However, it should be on #item or #items as well, which would be the better choice.

summit’s picture

Hi Deciphered, the output path worked, thanks!
Only problems now are with shorturl_shorten($variables['#items'][0]['url']));
1) the shorturl is allways the same
2) the redirect url doesn;t have the query string from the original url.

EDIT: Could these things be connected. Could it be that $variables['#items'][0]['url'] which is used for the shorturl_shorten already is the url without query string, and therefore (because all nodes have same http://wintersport.sunweb.nl/reizen/ peace of url) the shorturl is always the same.

The error is than that the query string is cut off likewise: http://drupal.org/node/1397700#comment-5443574

greetings, Martijn

deciphered’s picture

What are the values of the URLs being passed through shorturl_shorten()?

I know some shortening services don't handle query strings at all yet others do. If your not using Clean URLs you really should be.

summit’s picture

Status: Active » Fixed

Hi,
It was a link module problem, see this issue-patch http://drupal.org/node/1309658#comment-5168960
Thanks for your help with this issue!

greetings,
Martijn

Status: Fixed » Closed (fixed)

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

summit’s picture

Hi,
I have still one tiny thingy with the array of something. How should I get the variable in this line, like http://drupal.org/node/1379704#comment-5439796

#object (Object) stdClass
    field_prijs (Array, 1 element)
       und (Array, 1 element)
          0 (Array, 3 elements)
             amount (String, 5 characters ) 31000

I just can't get it right. I tried:

$variables['#object']['field_prijs']['und'][0]['amount']
$variables['#object']['field_prijs']['und'][0]->amount

But no success. Can you help me please?
EDIT: I found it!!

$variables['#object']->field_prijs['und'][0]['amount'] 

Thanks!
Greetings, Martijn

deciphered’s picture

Anything that comes from an object/class should be referenced with an arrow (->) and anything coming from an array should be referenced with square brackets ([]).

Look at the output a little closer:
#object is a class, therefore anything referenced from it should be referenced by an arrow
Everything else are arrays, therefore they should be referenced by square brackets.

So you should be using the following code:
$variables['#object']->field_prijs['und'][0]['amount']

summit’s picture

Thanks for your quick reply and explanation. I am not a programmer, many thanks for your help!
Greetings, Martijn