You are here

function emapi_media_from_uri in Embedded Media Field 6.3

Loads a media object based on the given URI.

Parameters

string $uri: A stream, referenced as "scheme://target".

Return value

mixed The fully populated media object, or FALSE.

4 calls to emapi_media_from_uri()
emapi_parse in emapi/emapi.module
Parses a URL or embed code into a media object.
emfield_field in ./emfield.module
Implementation of hook_field().
emfield_widget in ./emfield.module
Implementation of hook_widget()
theme_emfield_formatter_default in includes/themes/emfield.themes.inc
Display a link to the original media.

File

emapi/emapi.module, line 236
Provides an API for parsing, storage, and display of third party media.

Code

function emapi_media_from_uri($uri) {
  $media =& emapi_static('emapi_media', array());

  // First check to see if we've already loaded this media.
  foreach ($media as $emid => $item) {
    if ($item->uri == $uri) {
      return $item;
    }
  }

  // Next see if the item is in the db.
  $results = db_query("SELECT emid, uri, uid, status, timestamp FROM {emapi_media} WHERE uri = '%s'", $uri);
  if ($result = db_fetch_object($results)) {
    $media[$result->emid] = emapi_media_from_db_result($result);
    return $media[$result->emid];
  }

  // Finally we simply create a new media object.
  if (($class = emapi_get_provider_class($uri)) && class_exists($class)) {
    global $user;
    $item = new $class($uri);
    $item
      ->set_uid($user->uid);
    $item
      ->set_status(EMAPI_STATUS_PERMANENT);
    $item
      ->set_timestamp(time());
    drupal_alter('emapi_media', $item);
    return $item;
  }

  // No media matches the given URI.
  return FALSE;
}