I just switched a client's site from the Twitter Pull module to using Twitter/OAuth because of the changes in the Twitter API. Everything is working well - I have the twitter feed displayed in a block on the home page with the profile pictures of the Twitter accounts associated with the client's Twitter feed. There is one unexpected behavior though. When displaying the client's tweets, their twitter profile picture is displayed next to the tweet. When displaying people that tweeted @ the client, the profile pic for the account that tweeted @ my client is being displayed. However, when my client retweets another users tweets, my client's profile picture is displayed. I would expect the profile picture of the source of the original tweet to be displayed.

For demonstration's sake, see the retweet from my client's account (twitter-retweet.png) as displayed on my client's Twitter feed at https://twitter.com/JSAMarketing,

twitter retweet

with the retweet as displayed on my client's website (block-retweet.png) http://jamesstreetassoc.com/.

block retweet

I hoped that the profile pics could be the same on the twitter profile page and the website, but that's not what I'm seeing. Is there any way to display the profile picture of the original twitter account, as opposed to the profile pic of the person who simply retweeted that tweet?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

intrafusion’s picture

Component: Documentation » Code
Category: support » feature

I had the same issue when using the results provided through Views, however I found by adding the following code I managed to solve the problem, perhaps this could be included

    // Replace retweet data
    if (preg_match('/^RT\s@(\w+)\:\s/', $status->text, $matches)) {
      module_load_include('inc', 'twitter');
      $twitter = twitter_connect();

      $status->text = str_replace($matches[0], '', $status->text);
      $author = $twitter->users_show($matches[1]);
    }

This code goes in twitter_views_field_handlers.inc after

    // Load tweet and author.
    $status = twitter_status_load($values->twitter_id);
    $author = twitter_account_load($status->screen_name);
valgibson’s picture

Created a patch. Only works when a 'formatted tweet'-field is being rendered with Views.
Sorry, attachment name of the patch should have been "twitter-avatar-being-retweeted-2022717-7739815.patch"

DamienMcKenna’s picture

Version: 7.x-5.8 » 7.x-5.x-dev
Status: Active » Needs review

Thanks for the patch. Please remember to change the status to "needs review" when you do upload a patch, otherwise people might not see it.

Status: Needs review » Needs work

The last submitted patch, 2: twitter-avatar-being-retweeted-2022717-2.patch, failed testing.

_wdm_’s picture

An alternate approach is to handle it in a custom module with something like:


function mymodule_views_data_alter(&$data) {
  if (isset($data['twitter'])) {
    $data['twitter']['formatted_tweet_retweet'] = array(
      'title' => t('Formatted tweet with retweets'),
      'help' => t('Renders a tweet as it is shown at Twitter.com.'),
      'field' => array(
        'handler' => 'mymodule_twitter_views_handler_field_formatted_tweet',
      ),
    );
  }
}


class mymodule_twitter_views_handler_field_formatted_tweet extends views_handler_field {
  function query() {}

  function render($values) {
    drupal_add_js('//platform.twitter.com/widgets.js', 'external');
    drupal_add_css(drupal_get_path('module', 'twitter') . '/twitter.css');
    module_load_include('inc', 'twitter');

    // Load tweet and author.
    $status = twitter_status_load($values->twitter_id);
    $author = twitter_account_load($status->screen_name, FALSE);

    if (preg_match('/^RT\s@(\w+)\:\s/', $status->text, $matches)) {
      module_load_include('inc', 'twitter');
      $twitter = twitter_connect(NULL, TRUE, FALSE);

      $status->text = str_replace($matches[0], '', $status->text);
      $author = $twitter->users_show($matches[1]);
    }

    // Render the tweet.
    return theme('twitter_status', array(
      'status' => $status,
      'author' => $author,
      'reply' => t('Reply'),
      'retweet' => t('Retweet'),
      'favorite' => t('Favorite'),
    ));
  }
}

That class is based on the twitter_views_handler_field_formatted_tweet class and comment #1

NOTE: you must have an account configure as global at admin/config/services/twitter

frankdesign’s picture

I know the solution at Comment #1 is a hack but it did work (at least it did on previous releases of Twitter). But it doesn't work on 7.x-5.11 or 7.x-6.2. I also tried the custom module solution but I can't seem to get that working with the current releases either.

Anybody got any other ideas? Really would like to solve this issue.

Thanks

F

thatguy’s picture

I used the previous solutions but instead wanted to use the Profile image field so I modified them a bit. Also make sure one twitter account on your page has the "General" option selected at the admin/config/services/twitter options page.

function mymodule_views_data_alter(&$data) {
  if (isset($data['twitter_account'])) {
    $data['twitter_account']['profile_image_url'] = array(
      'title' => t('Profile image'),
      'help' => t(' The image used by the Twitter account.'),
      'field' => array(
         'handler' => 'mymodule_twitter_views_handler_field_profile_image',
      ),
    );
  }
}
 
class mymodule_twitter_views_handler_field_profile_image extends views_handler_field {
  function render($values) {
    if (preg_match('/^RT\s@(\w+)\:\s/', $values->twitter_text, $matches)) {
      module_load_include('inc', 'twitter');
      $twitter = twitter_connect(NULL, TRUE, FALSE);
      if ($twitter) {
        $status->text = str_replace($matches[0], '', $status->text);
        $author = $twitter->users_show($matches[1]);
        $value = $author->profile_image_url;
      }
      else {
        $value = $values->{$this->field_alias};
      }
    }
    else {
      $value = $values->{$this->field_alias};
    }
    $output = theme('image', array('path' => $value, 'width' => 48, 'height' => 48));
 
    // Convert to a protocol-relative URL so that the same image tag will work
    // regardless of whether the page is loaded via HTTP or HTTPS.
    return str_replace('http:', '', $output);
  }
}
intrafusion’s picture

Status: Needs work » Needs review
FileSize
824 bytes

Patch from #2 re-rolled against 7.x-5.x-dev

rutiolma’s picture

Status: Needs review » Reviewed & tested by the community

#8 works nicely

rutiolma’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
966 bytes

Unfortunately the previous patch didn't fully work.
It does work for RT of website defined accounts, not for other non-defined accounts.
I'm attaching a patch, almost the same as #8 but forcing a twitter authentication. Since this will just load a twitter account information for the RT, this doesn't seem to pose a security issue.

andrey.troeglazov’s picture

Assigned: Unassigned » andrey.troeglazov

andrey.troeglazov’s picture

Assigned: andrey.troeglazov » Unassigned
Status: Needs review » Fixed

Commited both to 7.5 and 7.6 dev branches.

andrey.troeglazov’s picture

Status: Fixed » Closed (fixed)

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

DamienMcKenna’s picture

DamienMcKenna’s picture