The module correctly picks up all the data from a tweet minus the image or image card.

With instagram and facebook import the images correctly.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nasero created an issue. See original summary.

arezeeman’s picture

I returned to some code today that working fine last fall but now doesn't seem to get the images from a tweet--seems likely to be the same problem. I think it's a change in the Twitter API, not an issue with Social Content. See this:

https://dev.twitter.com/overview/api/upcoming-changes-to-tweets

Here's what I've learned (keep in mind thay I know virtually nothing about the twitter API beyond what I needed to get my code working):

  • Apparently there are now "extended" tweets and "classic" tweets.
  • Tweets are fetched by default in "compatability mode," which truncates them if they are extended.
  • To get complete info for extended tweets, you have to add "tweet_mode=extended" to the query.
  • When you fetch tweets in extended mode, the tweet is returned as 'full_text', not 'text'. There's a new field called 'display_text_range' which is supposed to be useful but I'm ignoring it.
  • Another change between last fall and now is that media in a retweet does not show up in the 'entities' field in the main record. It only shows up in the 'entities' field of 'retweeted_status'.

First step to get things working is to add the "tweet_mode=extended" flag, which can be done with a patch to social_content_twitter.class.inc:

  protected function getRowsAccount($settings, $global_settings, $last_id) {
    $params = array(
      'screen_name' => $settings['account'],
      'exclude_replies' => TRUE,
      'count' => $settings['limit'],
      'tweet_mode' => 'extended',	// <<<<< ADD THIS
    );

Next thing is deal with the 'full_text' vs. 'text' issue and the retweeted media. Instead of patching, I made a subclass of SocialContentTwitter, though hopefully these changes can be rolled into the module code.

<?php

// File MYMOD.social_content_twitter_override.inc

class MymodSocialContentTwitter extends SocialContentTwitter {
  public function prepareRow($row) {
    $mappings = $this->getFieldMappings();
    if (isset($row->full_text)) {
      // Theoretically should respect 'display_text_range', which indicates [start, end]
      // of full_text to display. But that will throw off the mappings in 'entities',
      // since they seem to refer to 'full_text' as a whole (as of 2017-04-14).
      $row->text = $row->full_text;
    }
    if (isset($row->retweeted_status->full_text)) {
      $row->retweeted_status->text = $row->retweeted_status->full_text;
    }
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    // Get media from retweet:
    // Duplicate code in parent (social_content_twitter.class.inc) that gets first media
    // item from $row->entities, but apply it to $row->retweeted_status->entities.
    if (isset($mappings['media']) && isset($row->retweeted_status) && !isset($row->media)) {
      if (isset($row->retweeted_status->entities->media)) {
        foreach ($row->retweeted_status->entities->media as $item) {
          if (isset($item->media_url)) {
            $picture = $this->saveExternalFile($item->media_url, $mappings['media']);
            $row->media = !empty($picture) ? $picture : NULL;
            if ($picture) {
              break;
            }
          }
        }
      }
    }
  }
}

Need this hook to point to the override class:

function MYMOD_social_content_class_info_alter(&$classes) {
  if (isset($classes['twitter'])) {
    $classes['twitter'] = 'MymodSocialContentTwitter';
  }
}

Finally, need to add a line to the module .info file and rebuild the class registry.

files[]=MYMOD.social_content_class_override.inc

Many thanks for Social Content--very nice, flexible design.

troesler’s picture

Thanks arezeeman,
Where do you add the hook

function MYMOD_social_content_class_info_alter(&$classes) {
  if (isset($classes['twitter'])) {
    $classes['twitter'] = 'MymodSocialContentTwitter';
  }
}
arezeeman’s picture

I put it in a custom module that I use for site-specific code that I don't want to put in the theme. You could also put it into your theme's template.php file. You might be able to load the subclass in your theme, too--I don't see why not, but I've never tried it. Or you could make a little custom module--let's call it lcm.

File lcm.info:

name = "Little Custom Module"
description = "Fix for Social Content"
core = 7.x
package = Custom
project = "lcm"
files[]=lcm.social_content_class_override.inc

File lcm.module:

<?php

function lcm_social_content_class_info_alter(&$classes) {
  if (isset($classes['twitter'])) {
    $classes['twitter'] = 'LcmSocialContentTwitter';
  }
}

File lcm.social_content_class_override.inc:

<?php

class LcmSocialContentTwitter extends SocialContentTwitter {
  public function prepareRow($row) {
// etc... (see code in earlier post)
  }
}

Once the files are in place, in the directory sites/all/modules/custom/lcm, you'd have to enable the module. That's the code I've added to my custom module, but I haven't tested that it as a little stand-alone module, so it's possible I've missed something.

troesler’s picture

Thank you again, especially for your chrystal clear explaination ;)

It works like a charm !

Melju’s picture

I still have the issue. When I'm enabling your module ans when I go to the configuration page of Social Content it display a white page.
What am I doing wrong?

Thanks

tostinni’s picture

Status: Active » Needs review
FileSize
2.17 KB

I prepared a patch based on this code, thank you arezeeman.

  • Pere Orga committed 85e21a2 on 7.x-2.x authored by tostinni
    Issue #2845235 by tostinni, arezeeman: Unable to get tweet images
    
Pere Orga’s picture

Status: Needs review » Fixed

Merged to dev, thanks!

Status: Fixed » Closed (fixed)

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