Twitter's documentation on Embedded tweets.

A couple of things the module could do automatically:

Comments

steinmb’s picture

Title: Embedded Tweets » Embedded Tweets input filter

+1 from me. Patches are welcome :)

sillygwailo’s picture

StatusFileSize
new2.25 KB

First attempt. Needs:

  • failover for if/when api.twitter.com is not available (on uncached requests, is the filter cache good enough?)
  • regex that looks for Twitter URLs
sillygwailo’s picture

Status: Active » Needs work
deciphered’s picture

If it helps, I wrote a Custom Formatter for Embedded Tweets: http://customformatters.com/formatters/14/embeddedtweets

dddave’s picture

Version: 7.x-3.x-dev » 7.x-6.x-dev

As far as I can see there is no other module that allows the embedding of tweets into normal content (i.e. not using blocks). To allow that we have to whitelist script-tags and this is a security issue on sites with many editors. Imho this would be a killer addition to Drupal itself and this module in particular.

balleyne’s picture

Version: 7.x-6.x-dev » 7.x-5.x-dev
Assigned: Unassigned » balleyne
Status: Needs work » Needs review
StatusFileSize
new3.83 KB

I've attached a patch that creates a new input filter, utilizing the pre-existing statuses_oembed() function already contained inside twitter.lib.php to perform the API call. (The only downside is that requires an authenticated Twitter account... whereas the Twitter API doesn't actually seem to require authentication for this particular API call.)

I've also copied code from the URL filter in filter.module to treat Twitter URLs like that filter does, i.e. don't convert URLs contained inside tag attributes (like the href attribute in a link tag).

I've made the patch to 7.x-5.x, because I need this functionality for a site currently using 7.x-5.x, but I'd be happy to apply it to 7.x-6.x if needed or preferred.

Lastly, this filter should probably appear after any "limited allowed HTML tags" type filter, since it injects a script tag from the Twitter API, but as the patch stands it doesn't currently warn Drupal site builders who might not consider that. Similarly, it won't warn a user if there are no authenticated accounts available, it just won't perform the substitution.

(Please forgive my boldness in changing the issue settings, or any mistakes I've made in generating the patch -- I'm rather new to submitting patches.)

Hope that helps...

dddave’s picture

Wohooo!

I am very much looking forward to testing this one out tonight. And I especially don't mind the version change as I am using v5 exclusively at the moment. ;)

dddave’s picture

Works like a charm. Congrats, you upped the usability of this module massively. THANKS A MILLION!

dddave’s picture

Status: Needs review » Needs work

Somewhere between the core update and the new dev of this module this stopped working correctly.

dddave’s picture

Status: Needs work » Needs review

Ok, this was caused by the Do Not Track Me FF extension. Still works like a charm!

dddave’s picture

Has this anybody working with https://drupal.org/project/wysiwyg_ckeditor or wysiwyg+ckeditor 4.x?

I've no idea how but during my testruns the "turn urls into links" filter went behind this one. Of course this cannot work when that happens. So I happily report that this works like a charm when used with wysiwyg_ckeditor.

I really wonder what people are doing on their site's when they want to embed tweets into content bodies. Seems like a very common approach in todays blogging culture yet hardly anyone seems to be testing this awesome patch. :(

tim_dj’s picture

works perfect!

However it does not cache the return of oembed which is advisable because otherwise it will connect to twitter every request

gaxze’s picture

Issue summary: View changes

Not having a great deal of success here - especially when pasting URL's for tweets with media.

I've realised it's actually an issue with caching and twitter returning a 429 error: https://dev.twitter.com/docs/error-codes-responses

gaxze’s picture

I've had a go at working on a patch to cache the requests as mentioned in #12

gaxze’s picture

It appears my patching skills are useless - this is meant to be a patch addition to #12

Code below:

/**
 * Retrieve Embedded Tweet.
 *
 * @param $id
 *   status id of the tweet
 * @return
 *   array response from Twitter API.
 */
function twitter_statuses_oembed($id) {
  // check if cached version of the tweet exists.
  if ($cache = cache_get('twitter_oembed_'.$id)) {
    if($cache->expire > time()) {
      // return cache
      $twitter = $cache->data;
    } else {
      // clear cache if expired
      cache_clear_all('twitter_oembed_'.$id, 'cache');
    }
  } else {
    // request tweet
    $twitter = twitter_connect();
    // set cache for 2 days.
    cache_set('twitter_oembed_'.$id, $twitter, 'cache', time() + 2880*60);
  }

  if ($twitter) {
    return $twitter->statuses_oembed($id);
  }
}

dddave’s picture

Assigned: balleyne » Unassigned

Just to be clear and so I can test this (I suck at coding but can test patches):

Latest 5x dev, patched and then your code where?

gaxze’s picture

The website i've applied this to is using the version: 7.x-5.8

I've basically applied the patch from #6 and then made the following changes to twitter.inc
From:

/**
 * Retrieve Embedded Tweet.
 *
 * @param $id
 *   status id of the tweet
 * @return
 *   array response from Twitter API.
 */
function twitter_statuses_oembed($id) {
  $twitter = twitter_connect();
  if ($twitter)
    return $twitter->statuses_oembed($id);
}

To:

/**
 * Retrieve Embedded Tweet.
 *
 * @param $id
 *   status id of the tweet
 * @return
 *   array response from Twitter API.
 */
function twitter_statuses_oembed($id) {
  if ($cache = cache_get('twitter_oembed_'.$id)) {
    if($cache->expire > time()) {
      // return cache
      $twitter = $cache->data;
    } else {
      cache_clear_all('twitter_oembed_'.$id, 'cache');
    }
  } else {
    // request tweet
    $twitter = twitter_connect();
    // set cache for 2 hours.
    cache_set('twitter_oembed_'.$id, $twitter, 'cache', time() + 2880*60);
  }

  if ($twitter) {
    return $twitter->statuses_oembed($id);
  }
}
dddave’s picture

I applied this change to the current dev (patched) and it seems to be fine. Code makes sense to me but that really doesn't mean anything.

damienmckenna’s picture

Status: Needs review » Needs work

Triggering the testbot.

damienmckenna’s picture

Status: Needs work » Needs review

Triggering the testbot.

happysnowmantech’s picture

I've attached another patch that combines the changes from #6 and #17 into a single patch that can be applied cleanly (I applied it to version 7.x-5.8). I had previously tried the patch from #14 but it did not apply cleanly.

dddave’s picture

Patch works as advertised even when applied to the latest 7.x-5.x-dev (minor offset aside).

dddave’s picture

I am hesitant to RTBC this although I have the patch in production for a while now because my code skills are really nothing to trust. A second opinion would be nice. I would really like to see this patch go in. Glad to see the tests pass.

Sergey Filimonov’s picture

Looks like there is an error, you can find it if you use several twitts in one text area.

In this code:

        foreach($tweets as $tweet) {
          $url = $tweet[0];
          $id = $tweet[3];
          $embed = twitter_statuses_oembed($id);
          $chunks[$i] = str_replace($url, $embed['html'], $chunk);
        }

You need to change this line

$chunks[$i] = str_replace($url, $embed['html'], $chunk);

to this:

$chunks[$i] = str_replace($url, $embed['html'], $chunks[$i]);

Otherwise only last twitt will be shown.

jenlampton’s picture

Thanks for the patches! :)

Thus far it looks like we're unable to render images, videos, and link previews. Is that still yet to be done? I'd be happy to add that.

Also, it looks like the current implementation works only when the "Limit allowed HTML tags" filter is disabled, Is anyone else running into that problem?

jenlampton’s picture

Okay, here's an updated patch with some minor code style formatting updates, and new options for embedded media, alignment, and conversation threading.

To answer my own question about about filters, I just needed to move the "Embed tweet" filter below the "Limit allowed HTML tags" filter and everything works great :)

damienmckenna’s picture

Thanks everyone for working on this!

@jenlampton: Would you mind adding a note to the README.txt about the text filter, along with a mention about positioning it in relation to other filters? Some tests might be useful, but they're not required.

dddave’s picture

Playing around with the (cleanly applying) patch and I am seeing this: TwitterException: Not Found in Twitter->request() (Zeile 156 von MYSITE/drupal/sites/all/modules/twitter/twitter.lib.php).

edit: Saw this in logs first, tried a new post with an embedded tweet which went fine. Went on to edit the post and pasted the embed code of a tweet (not the link to it) and the test site sunk.

dddave’s picture

StatusFileSize
new131.05 KB

Referrer and message from the log.

dddave’s picture

Had no time to dig deeper but it seems this is indeed coming from this patch alone. Current unpatched .dev seems to work fine. Additionally I noticed that said error also was thrown on cron runs.

damienmckenna’s picture

Status: Needs review » Needs work
StatusFileSize
new38.43 KB

This isn't working for me if a link to a tweet is the first text in the field. I'm testing it in the Full HTML text format, the Embed Tweet filter is first in the list, so the list looks like the following:

  • Embed Tweets
  • Convert URLs into links
  • Convert line breaks into HTML (i.e.
    and

    )

  • Correct faulty and chopped off HTML

I added dpm($chunks); right after the preg_split() and it shows the following items:

I'm not discounting there being something wrong with my setup, e.g. I'm a little confused as to why the link has already been converted to a link.

damienmckenna’s picture

Status: Needs work » Needs review

Ok, never mind, it seems like I needed to re-save the text formats in order for it to work properly.

damienmckenna’s picture

StatusFileSize
new6.06 KB

Rerolled, for 7.x-5.x.

damienmckenna’s picture

BTW the updated patch in #34 includes a note in the filter that says "Should be sorted after the "Limit allowed HTML tags", if used."

damienmckenna’s picture

Version: 7.x-5.x-dev » 7.x-6.x-dev
StatusFileSize
new6.06 KB

For 7.x-6.x.

Status: Needs review » Needs work

The last submitted patch, 36: twitter-n1365452-36-7.x-6.x.patch, failed testing.

damienmckenna’s picture

Version: 7.x-6.x-dev » 6.x-5.x-dev
Status: Needs work » Needs review
StatusFileSize
new7.48 KB

For 6.x-5.x.

  • DamienMcKenna committed f8703e8 on 6.x-5.x
    Issue #1365452 by sillygwailo, balleyne, GaxZE, happysnowmantech,...

  • DamienMcKenna committed 3c685a7 on
    Issue #1365452 by sillygwailo, balleyne, GaxZE, happysnowmantech,...
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks everyone!

Status: Fixed » Closed (fixed)

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

nickonom’s picture

Version: 6.x-5.x-dev » 7.x-6.x-dev
Status: Closed (fixed) » Active

Currently, we need to allow the <script> tag to use this input filter and that is really not desirable for security reasons. Isn't possible to inject the required script, for example in header section of any page where the filter is used, without allowing the <script> tag?

adamps’s picture

Version: 7.x-6.x-dev » 6.x-5.x-dev
Status: Active » Closed (fixed)

@nickonom Please could you open a new issue for your new request "input filter without need for script tag"?

This issue was fixed 10 months ago, and it confuses people like me finding the thread from Google if the status doesn't match that.

You can of course link your new issue from here to help other people who might have a similar idea.

adamps’s picture

@nickonom Also note the description of "Embed Tweets" filter:

Converts URLs for Twitter status updates into embedded Tweets. Should be sorted after the "Limit allowed HTML tags" filter, if used.

If you get the order right I don't think you need to allow script.