I've made a first token integration, first for global usage and per node type. It needs some work ie I have to look into comments, blocks and users titles.

First question: are there more people interested in this token integration?

--- sharethis.module (e1bd71e)
+++ sharethis.module (4cc09ea)
@@ -211,6 +211,24 @@
     '#type' => 'checkbox',
     '#default_value' => variable_get('sharethis_late_load', FALSE),
   );
+  if (module_exists('token')) {
+    $form['advanced']['sharethis_title_global'] = array(
+      '#title' => t('Title'),
+      '#description' => t('You can use tokens to generate the title when sharing. By default the node title will be used.'),
+      '#type' => 'textfield',
+      '#default_value' => variable_get('sharethis_title_global', FALSE),
+      '#element_validate' => array('token_element_validate'),
+      '#token_type' => array('node')
+    );
+
+    $form['advanced']['token_help'] = array(
+      '#title' => t('Replacement patterns'),
+      '#theme' => 'token_tree',
+      '#token_types' => array('node'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+      );
+  } 
   $form['advanced']['sharethis_twitter_suffix'] = array(
     '#title' => t("Twitter Suffix"),
     '#description' => t("Optionally append a Twitter handle, or text, so that you get pinged when someone shares an article. Example: <em>via @YourNameHere</em>"),
@@ -314,6 +332,17 @@
   // Get the full path to insert into the Share Buttons.
   $mPath = url($path_module, array('absolute'=>TRUE));
   $mTitle = $node->title;
+  if(module_exists('token')){
+    // First check if token is available by content type
+    $_tokenTitle = variable_get('sharethis_title_' . $node->type, FALSE);
+    if($_tokenTitle == FALSE){
+      // Use global
+      $_tokenTitle = variable_get('sharethis_title_global', FALSE);
+    }
+    if($_tokenTitle != FALSE){
+      $mTitle = token_replace($_tokenTitle, array('node'=>$node), array('clear'=>TRUE));
+    }
+  }
   
   // Only display the ShareThis buttons if this node fits all the requirements
   if (strpos($data_options['nodeType'], $node->type) !== FALSE) { // Make sure this is the right type of node.
@@ -353,7 +382,50 @@
     }
   }
 }
+/**
+ * hook_form_alter()
+ */
+function sharethis_form_node_type_form_alter(&$form, $form_state) {
+  if( module_exists('token')) { // sharethis allowed for this content type?
+    $_sharethisOptions = sharethis_get_options_array();
+    $nodeTypes = explode(',',$_sharethisOptions['nodeType']);
+    $form['sharethis'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('ShareThis'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#group' => 'additional_settings',
+      '#tree' => TRUE,
+      '#weight' => 10,
+      '#disabled' => !in_array($form['#node_type']->type,$nodeTypes),
+     );
 
+    $form['sharethis']['sharethis_title_'.$form['#node_type']->type] = array(
+      '#title' => t('ShareThis title'),
+      '#description' => t('You can use tokens to generate the title when sharing. By default the node title will be used.'),
+      '#type' => 'textfield',
+      '#default_value' => variable_get('sharethis_title_'.$form['#node_type']->type, FALSE),
+      '#element_validate' => array('token_element_validate'),
+      '#token_type' => array('node')
+    );
+
+    $form['sharethis']['token_help'] = array(
+      '#title' => t('Replacement patterns'),
+      '#theme' => 'token_tree',
+      '#token_types' => array('node'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+    );
+    
+    $form['#submit'][] = 'sharethis_node_type_form_submit';
+  }
+}
+
+function sharethis_node_type_form_submit($form, &$form_state) {
+  $type = trim($form_state['values']['type']);
+  variable_set('sharethis_title_'.$type, $form_state['values']['sharethis']['sharethis_title_'.$type]);
+}
+
 /**
  * sharethisGetOptionArray is a helper function for DB access.
  *

Comments

studio-days’s picture

Hey I'm VERY interested in this!

I installed the patch, which has brought up the token integration form into the Advanced Settings. Great. BUT it doesn't seem to work when clicking on a Share icon, eg Tumblr.

djschoone’s picture

THanks for testing. Will look into it. Are you testing on a node?

djschoone’s picture

I tested with Tumblr and it works for me. Did you put in a global token or a specific token (ie a field which is not available for that node)?

Chaulky’s picture

StatusFileSize
new12.67 KB

I've made some additional progress on the Token integration. I added support to use Tokens for more of the ShareThis information. According to the ShareThis there are four attributes you can set to control what information gets shared (though for some reason i doubt this is actually a complete list).

  • st_url
  • st_title
  • st_image
  • st_summary

I've added token fields for all four of these. The tokens can be set on the ShareThis configuration page, or on the node type edit forms (like djschoone did). These tokens are only used when you adding ShareThis to a node, so I disable the token fields if the user selects "Block" as the location.

This doesn't work well with the Views field handler, I'd have to duplicate all the logic in sharethis_node_view to get all the right values. Instead, I think we need a separate function that takes a node (or maybe even and entity) and returns the values that should be used for ShareThis. Then, those values can be fed into a theme function to create the proper HTML. I know there's a separate issue to use a theme function (I actually have a patch in there waiting) so we can do that outside the scope of this issue and just use the current sharethis_get_button_HTML() function. That would make it much easier to get all the sharing data from the Views field handler. I don't mind doing that, but this patch was already getting kind of big so I wanted to put it out there for people to see and give feedback.

As an added bonus, using Tokens for the sharing information makes it show you can use more than just the title and url when you have a list of nodes with ShareThis buttons. Without it, there's no way to know what image or summary to use when sharing.

marenm’s picture

Is there a more recent version of this patch? Would love to use it but I'm getting a failure at line 385 (Hunk #2 FAILED at 385.)
Tried to manually put in the new info but still can't get it to work.
Tokens show up in the admin but don't seem to be outputting sensical data.

Thanks!

marenm’s picture

Also, @Chaulky - would be very interested in the extended patch to get this working with Views field handler. Did that ever go anywhere?
Thanks again!

Chaulky’s picture

@marenm, I haven't done anything with this really since that comment. I actually didn't notice that the issue to replace sharethis_get_button_HTML() with a theme function got committed. With that finished, this may get a little easier. Unfortunately I've been out of the Drupal world for a bit due to a change of projects at work (doing a lot of .NET now). I may have some time next weekend though (beginning of March) to look at re-rolling this patch.

rickharington’s picture

Would be great to see this integrated

lukio’s picture

StatusFileSize
new13.27 KB

Hello!

I modified the patch to work against 7.x-2.5 with views handler link integrated.
Hope it helps!

lukio’s picture

Status: Active » Needs review

The last submitted patch, 4: sharethis-token_integration-1844674-4.patch, failed testing.

navneet0693’s picture

Patch doesn't applies to 7.x-2.x-dev. Re-rolling required.

navneet0693’s picture

Version: 7.x-2.5 » 7.x-2.x-dev

Changing branch to be developed and testing against latest development branch.

navneet0693’s picture

Status: Needs review » Needs work
yogeshmpawar’s picture

Assigned: djschoone » yogeshmpawar
yogeshmpawar’s picture

Assigned: yogeshmpawar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new10.88 KB

I have added patch with token integration against 7.x-2.x branch.
Please review.

vladimiraus’s picture

Status: Needs review » Closed (outdated)

Drupal 7 is no loner supported.
Marking as outdated.