Hello if it possible to makes it compatible to used with Display Suite module.

It needs a field that can be set with a hook with this follow code.

     $fields['node']['sharethis'] = array(
        'title' => t('Share'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
     );

Now i can used only with normal pages. But pages layouts with display suite its cannot display
because its missing field to select the sharethis output.

Thanks Lennard

CommentFileSizeAuthor
#8 sharethis_ds_1239418_8.patch2.71 KBroborn

Comments

swentel’s picture

Also, in hook_node_view sharethis, should use a decent key like say 'sharethis', not 'my_additional_field'

swentel’s picture

This is better code:

/**
 * Implements hook_ds_fields_info().
 */
function sharethis_ds_fields_info($entity_type) {
   $fields = array();

    $data = get_options_array();
    if (isset($data['nodeType'])) {
      $node_types = explode(',', $data['nodeType']);
    }
    if (!empty($node_types)) {
      $ui_limit = array();
      foreach ($node_types as $type) {
        $ui_limit[] = $type . '|*';
      }

      // This key should be sharethis instead of my_additional_field in
      // hook_node_view() - remove this comment of course.
      $fields['node']['sharethis'] = array(
        'title' => t('ShareThis'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
        'ui_limit' => $ui_limit,
      );
    }
  }

  // Return fields.
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}
Lennard’s picture

So i hope that code will be added that makes the work with that module for future easier.

THX @ swentel

Lennard’s picture

The code have litle mistake !

The worked code is:

<?php
/**
* Implements hook_ds_fields_info().
*/
function sharethis_ds_fields_info($entity_type) {
   $fields = array();

    $data = get_options_array();
    if (isset($data['nodeType'])) {
      $node_types = explode(',', $data['nodeType']);
    }
    if (!empty($node_types)) {
      $ui_limit = array();
      foreach ($node_types as $type) {
        $ui_limit[] = $type . '|*';
      }

      // This key should be sharethis instead of my_additional_field in
      // hook_node_view() - remove this comment of course.
      $fields['node']['sharethis'] = array(
        'title' => t('ShareThis'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
        'ui_limit' => $ui_limit,
      );
  }

  // Return fields.
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}
?>

it work with sharethis_node_view changed to:

<?php
function sharethis_node_view($node, $view_mode, $langcode) {

  // First get all of the options for the sharethis widget from the database:
  $data_options = get_options_array();
  
  // This looks to see if the path variable has been posted by some rewrite module.
  // This is not super efficient, O(N), but N is often less than 20.
  $is_path = FALSE;
  foreach ($node as $k => $v) {
    if ($k == "path") {
      $is_path = TRUE;
    }
  }
  // This will choose the path to use.
  if ($is_path) {
    $path_module = $node->path;
  }
  else {
    $path_module = "/node/" . $node->nid;
  }
  global $base_url;
  // Get the full path to insert into the Share Buttons.
  $mPath = $base_url . $path_module;
  $mTitle = $node->title;
  
  // 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.
    if (($data_options['viewMode'] == "1") && ($view_mode == "teaser")) { // If "don't show for teaser" is enabled, and this is a teaser, don't do anything
      // Do nothing.
    } 
    else {
      // Create the code for chicklets.
  
      // The share buttons are simply spans of the form class='st_SERVICE_BUTTONTYPE' -- "st" stands for ShareThis.
      $type = substr($data_options['buttons'], 4);
      $type = $type == "_" ? "" : $type;
      $service_array = explode(",", $data_options['services']);
      $st_spans = "";
      for ($i=0; $i<sizeof($service_array); $i++) {
        // Strip the quotes from the element in the array (They are there for javascript)
        $service = explode(":", $service_array[$i]);
        $serviceCodeName = substr($service[1], 0, -1);
        $display = "displaytext=";
        $display .= $service[0] . "\"";
        $display = ($type == "") ? "" : $display;
        $st_spans .= "<span st_url=$mPath st_title='$mTitle' class='st_$serviceCodeName$type' $display></span>";
      }
  
      // These are the ShareThis scripts:
      // If switchTo5x is set to false, then the "classic" widget will be selected.
      $is_five = $data_options['widget'] == 'st_multi' ? 'true' : 'false';
      $publisher_id = $data_options['publisherID'];
      $st_js = "<script type='text/javascript'>var switchTo5x=$is_five;</script>
        <script type='text/javascript' src='http://w.sharethis.com/button/buttons.js'></script>
        <script type='text/javascript'>stLight.options({publisher:'$publisher_id'});</script>";

      // This puts the buttons on the node and adds the necessary scripts.
      // You can change the weight to change whether the buttons are near the top or bottom of the node.
      // Default is at the bottom:
      $node->content['sharethis'] = array(
        '#markup' => $st_spans . $st_js, 
        '#weight' => 10
      );
    }
  }
}
?>
bendruapal’s picture

where does this code go? which file? where do i copy paste it?

bendruapal’s picture

For all other beginners.. I copied the first part of the code into line 255 of sharethis.module. and replaced sharethis_node_view with second part of code as mentioned above.. works fine with DS now and a big thank you to Lennard for the code.

jbm’s picture

Hello, why did you need this code? A share this field appear automatically in display suite.

roborn’s picture

Status: Needs work » Needs review
StatusFileSize
new2.71 KB

Here's the patch for #4

I also fixed and simplified code for the absolute url $mPath

Lennard’s picture

Why does the features in long time not added in the new version ???

I can't understand this.

robloach’s picture

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

Is this up to 2.x-dev?

jvalencia’s picture

As per #7, what is the issue we are fixing?

ishmael-sanchez’s picture

I'm not concerned about the DS implementation but as mentioned in #1

hook_node_view sharethis, should use a decent key like say 'sharethis', not 'my_additional_field'

if refinements that make it compatible with DS will also allow the sharethis to be rendered without having to write print render($content['my_additional_field']); that would be awesome. Hope that clears things up at least a little bit. The module works great other that little weird rendering thing. Thanks for contributing.

m_wiking’s picture

I'm a bit confused. As per #7 it's working. I'm using the latest dev version and it doesn't work with Display suit.

The patches that are posted in this thread are not working either. What is the status now? It's a but confusing.

kclarkson’s picture

I too find this post confusing.

Can someone answer if this Patch is already included in the Dev version?

tinarey’s picture

Mhm, this is very confusing... here's what I figured:

- installed stable version, patched > "2 out of 3 hunks FAILED"; I now have a ShareThis field under manage display, but the icons don't show up on the node

- installed .dev version > get the icons in the right place but don't have a field to control under manage display... now that is weird. Also the ShareThis icons open a new window although I have the overlay (Multi-Post Widget) enabled in the settings... argh. (So I reckon the patch is not included in the .dev version)

- tried patching the .dev version > "2 out of 3 hunks FAILED" and I get a fatal error.

Does this clarify anything at all?

EDIT
btw here's a quick'n'dirty workaround (using the dev version but should be possible w/ the other one as well):
- in the ShareThis settings, set the location to "Block"
- under manage display create a new block field and choose the ShareThis block

At least this way you get full control over where to place the ShareThis icons (instead of just roughly assigning it to body or the links region). However the Facebook and Twitter links still open in a new window while mail and sharethis open in an overlay, not sure if this is correct behaviour...

Lennard’s picture

@ RaspberryBlack

the codes works only on the 2.0 or 2.2 Version not use it in 2.2-Dev.

I can not unterstand why the maintainer all the posts about display suite ignored.

Edit: for Use with new 2.2-Dev or higher is easier use only this one.

Add follow code to sharethis.module and have fun with sharethis and display suite.

<?php
/**
* Implements hook_ds_fields_info().
*/
function sharethis_ds_fields_info($entity_type) {
   $fields = array();

    $data = sharethis_get_options_array();
    if (isset($data['nodeType'])) {
      $node_types = explode(',', $data['nodeType']);
    }
    if (!empty($node_types)) {
      $ui_limit = array();
      foreach ($node_types as $type) {
        $ui_limit[] = $type . '|*';
      }

      // This key should be sharethis instead of my_additional_field in
      // hook_node_view() - remove this comment of course.
      $fields['node']['sharethis'] = array(
        'title' => t('ShareThis'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
        'ui_limit' => $ui_limit,
      );
  }

  // Return fields.
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}
?>

For future now i hope this code will integrate in further versions .. a big please to the maintainer.

Lennard’s picture

Assigned: Lennard » Unassigned

Is it now possible to implement the code for future.

A little answer is a good thing.

Thanks Lennard

tinarey’s picture

Sorry Lennard, I'm working flat out on other projects and haven't got around trying this yet... Hopefully I'll find some time this week.

Lennard’s picture

Thanks Rasberry i hope too.

Its not really worked it is only to insert the code.

I run the last code inside the last Developer Version on 2 websites and its worked perfektly.

Lennard’s picture

What is so difficult to add a simple worked code in new review ?

Please implement it ... Thanks.

Lennard’s picture

At all that will used sharethis with Display Suite Module.

Implement simple follow code in sharethis.module:

<?php
/**
* Implements hook_ds_fields_info().
*/
function sharethis_ds_fields_info($entity_type) {
   $fields = array();

    $data = sharethis_get_options_array();
    if (isset($data['nodeType'])) {
      $node_types = explode(',', $data['nodeType']);
    }
    if (!empty($node_types)) {
      $ui_limit = array();
      foreach ($node_types as $type) {
        $ui_limit[] = $type . '|*';
      }

      // This key should be sharethis instead of my_additional_field in
      // hook_node_view() - remove this comment of course.
      $fields['node']['sharethis'] = array(
        'title' => t('ShareThis'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
        'ui_limit' => $ui_limit,
      );
  }

  // Return fields.
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}
?>

It works with latest Developer Version and 2.4 Version of Sharethis.

Its simple code but makes full compatible to use with display suite module.

rlangille’s picture

I have added a patch that not only works for Display Suite, but also works for custom view modes added via hook_entity_info_alter, and I believe it to be a better solution that just making it work for Display Suite. Check it out over here: http://drupal.org/node/1555004

anilmadhub’s picture

Quick solution for me, i selected "Links area" as location in ShareThis configuration and i just use the "Link" field in manage display.
Hope this helps.

vchen’s picture

#23 worked for me! Thanks!

hazah’s picture

Thanks for the tip in #22, @rlangille. The combo is exactly what I was looking for.

I had to modify the function code a little because of the way the patch changed some of the data structures.

/**
* Implements hook_ds_fields_info().
*/
function sharethis_ds_fields_info($entity_type) {
   $fields = array();

    $data = sharethis_get_options_array();
    if (isset($data['viewModes'])) {
      $node_types = array_keys($data['viewModes']);
    }
    if (!empty($node_types)) {
      $ui_limit = array();
      foreach ($node_types as $type) {
        $ui_limit[] = $type . '|*';
      }

      // This key should be sharethis instead of my_additional_field in
      // hook_node_view() - remove this comment of course.
      $fields['node']['sharethis'] = array(
        'title' => t('ShareThis'),
        'field_type' => DS_FIELD_TYPE_IGNORE,
        'ui_limit' => $ui_limit,
      );
  }

  // Return fields.
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}
tim.plunkett’s picture

Status: Needs review » Closed (duplicate)

The patch in #1555004: Incompatible with custom view modes #8 didn't fully support DS, but the one in #10 does.

kubilayrd’s picture

#23- Great idea! Worked for me, thanks.

langelhc’s picture

@anilmadhub #23- Works for me, thnks.