Hi,

right now AddToAny sets the URL for nodes with this code:

$link_url 	= url('node/' . $node->nid, array('absolute' => 1));

in addtoany.module::_addtoany_create_button()

Add some way to specify the URL format from the Admin UI. Using the token module would be good, but I don't know if you want this as a dependency.

Examples with possible tokens: I'd use [nid] to get something like http://ao2.it/51 or I could use [shorturl] if I am using http://drupal.org/project/shorturl

Regards,
Antonio

Comments

micropat’s picture

Status: Active » Needs work

Great feature request. The module easily supports custom domains hosted by bit.ly or awe.sm and also custom short URLs via AddToAny's JavaScript API; however, it would be tricky to get custom short URLs to work with the current module version.

For reference:
http://www.addtoany.com/buttons/customize/link_tracking

To demonstrate a non-working example, you'd specify the following in the AddToAny settings panel > Additional options > Additional script:

a2a_config.track_links = 'custom';
a2a_config.track_links_key = 'http://example.com/abc123';

Problem is, http://example.com/abc123 needs to be a variable for each short URL.

Also note that one important aspect about using AddToAny's config above is that AddToAny will only enable shortening for services where short URLs are appropriate. For instance, Twitter & Facebook, but not Digg or Google Bookmarks. So I'd advise against replacing $link_url.

Essentially, the settings panel's Additional script box would need to support some kind of 'template variables' or token.

Token module: I'd usually say let's veto a dependency, but it appears that the Token module made it upstream to Drupal 7, so I don't mind using it if it's standard use, efficient, and makes sense.

Any thoughts on a patch or best practices for this kind of thing?

ao2’s picture

So if I get it right, the url in a2a_config.track_links_key will be used only when needed by the given service?

Btw, enabling tokens in the Additional script field is not too hard:

diff -pruN addtoany.module.orig addtoany.module
--- addtoany.module.orig        2011-03-10 16:25:39.000000000 +0100
+++ addtoany.module     2011-03-10 18:54:57.000000000 +0100
@@ -130,6 +130,15 @@ function addtoany_menu() {
 function _addtoany_header_script() {
 
        $script_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://static.addtoany.com/menu/page.js' : 'http://static.addtoany.com/menu/page.js';
+
+  // Replace tokens in the Additional script field
+       $additional_js = variable_get('addtoany_additional_js', '');
+       if (function_exists('token_replace')) {
+               if(arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2)) {
+                       $node = node_load(arg(1));
+                       $additional_js = token_replace($additional_js, $type = 'node', $node);
+               }
+       }
 
        $javascript_header = "var a2a_config=a2a_config||{},"
                        . "da2a={done:false,"
@@ -158,7 +167,7 @@ function _addtoany_header_script() {
                        . "}"
                . "};"
                . "a2a_config.tracking_callback=['ready',da2a.script_onready];"
-               . variable_get('addtoany_additional_js', '');
+               . $additional_js;
 
        drupal_add_js($javascript_header, 'inline');
 }

This allows us, to have somenthing like the following properly replaced:

a2a_config.track_links = 'custom';
a2a_config.track_links_key = '[site-url]/[nid]';

If that looks right I can send a proper git am friendly patch.

Thanks,
Antonio

ressa’s picture

Is it possible to use the original Drupal URLs, like example.com/node/1027978, and would it be hard to set up?

izmeez’s picture

Version: 6.x-3.3 » 6.x-3.4

+1 to the question in Comment #3, how to configure addtoany to use a short Drupal url, http://example.com/node/12345

ao2’s picture

with the patch above you just use a2a_config.track_links_key = '[site-url]/node/[nid]'; in the last snippet.

Please report if that works for you.

Thanks,
Antonio

IWasBornToWin’s picture

I just downloaded version 7. Do I need to go somewhere in the module code in order to use a custom url?

enginpost’s picture

I am working with Drupal 8, but I think this could work for Drupal 7 too. This worked for me...

I added the contrib metatag module and set the shortlink URL metatag in the Advanced section to the path: [site:url]/node/[node:nid]

That got the short(ish) node number URL on the page for me with the right dynamic tokens.

With the AddToAny module loaded, I added the following additional javascript in the "AddToAny" configuration settings "additional options" area:

if( document.querySelector('link[rel="shortlink"]') ){
  a2a_config.track_links = 'custom';
  a2a_config.track_links_key = document.querySelector('link[rel="shortlink"]').getAttribute("href");
}

This should assign the basic Drupal node number url as the path. If you want to redirect that back to a nicer pathauto clean url alias that exists for the node, then ready my blog post about auto-redirecting node number urls back to aliased paths (so people don't bookmark the ugly node number url).

micropat’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)