2

I would like to get a functionality similar to Facebook: when you include any link in a status update, post or comment it will replace it with a thumbnail, link and short description of the page.

The emfield module has that functionality but it requires an extra field and can't do the replacement within a body copy.

Video Filter does it for videos but it requires a special syntax [video:url] that a user would have to learn. It does offer Wysiwyg integration.

Is there any way of achieving a seamless (Facebook-like) experience?

5
+100

A quick hack in Drupal 6 for the links would be:

<?php

function MYMODULE_filter($op, $delta=0, $format=-1, $text='') {
  switch ($op) {
    case 'list':
      return array (0 => t('MYMODULE Open Graph Link Maker'));

    case 'description':
      return t('Changes links into Open Graph Link Blocks with a thumbnail, title, and description');

    case 'no cache':
      return FALSE;

    case 'process':
      return _MYMODULE_og_block($text);

    case 'prepare':
      return $text;
  }
}

function _MYMODULE_og_block($text) {

  $text = preg_replace_callback("`(<p>|<li>|[ \n\r\t\(])(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", '_MYMODULE_link_to_og', $text);

  return $text;

}

function _MYMODULE_link_to_og($match) {

  $all = file_get_contents('http://' . $match[2]);      // alas get_meta_tags() only finds tags with name=, not property= like og: uses so we need to do this ourselves...
  $title=_MYMODULE_get_og_meta('title', $all);
  $image=_MYMODULE_get_og_meta('image', $all);
  $description=_MYMODULE_get_og_meta('description', $all);

  if ($title && $image) {
    $content=$match[1];
    $content.='<div style="border:1px solid grey;padding:5px;margin:5px;width:300px;">';
    $content.='<img style="height:50px;width:50px;margin-right:5px;float:left" src="' . $image . '">';
    $content.='<b><a target="_blank" href="http://' . $match[2] . '">' . $title . '</a></b><br>';
    $content.=$description;
    $content.='</div>';
    $content.=$match[3];
    return $content;
  } else {
    _filter_url_trim(NULL,72);                       // these might be different in D7
    return _filter_url_parse_partial_links($match);  // but all it does is the default behavior of changing it into a link
  }

}

function _MYMODULE_get_og_meta($meta, $text) {

  $foo=explode('<meta property="og:' . $meta . '" content="', $text);  // I am sure someone out there can do this better with preg_replace_callback :)
  $bar=explode('"', $foo[1]);
  return $bar[0];

}

which looks for www.__ and creates a simple facebook like div from the facebook og: meta tags. If you wanted to get fancier, of course you could look for the first image etc in the html in a similar fashion if they weren't present.

Now, I know you said you are using drupal 7, and I think that would be just changing MYMODULE_filter() to MYMODULE_filter_info() and changing up the arrays in the return as the filtering code itself should be the same. Obviously, css style it how you want, I just threw in some quick stuff.

Also, this obviously doesn't do any of the fancy facebook ahah stuff but for something coded while watching Hawaii Five-O, I hope it helps you out a bit :)

Oh, somewhat obviously, to turn it on, you would go into your input filters and add/enable it for filtered_html, for example, and turn off the original one that changes www.__ into links so you don't refilter it again.

  • cool. I get the idea. Embedly offers a cool jquery effect but requires it's own link form: embedly.github.com/jquery-preview/demo – uwe Feb 14 '12 at 5:31
  • @jimajamma I am also looking for such link filter which converts image and video links into an embedded content. I found oembed the best so far, but when I share a link with text before or after the link, it does not convert. If I share only the link, it works perfectly. Any idea how to overcome this problem? One way is to use fbsmp module, a submodule for statuses module which allows to share links, photos, videos with your status updates, but I do not know how to combine/integrate oembed with fbsmp – Umair Jan 6 '17 at 13:27
4

oEmbed project is your friend.

It has many caveats, but oEmbed + Media module will likely get you close, and the issue queue would love you if you help.

  • I am also looking for such link filter which converts image and video links into an embedded content. I found oembed the best so far, but when I share a link with text before or after the link, it does not convert. If I share only the link, it works perfectly. Any idea how to overcome this problem? One way is to use fbsmp module, a submodule for statuses module which allows to share links, photos, videos with your status updates, but I do not know how to combine/integrate oembed with fbsmp. – Umair Jan 6 '17 at 13:25
  • @kiamlaluno can you help? – Umair Jan 6 '17 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.