You are here

function media_parse_to_uri in D7 Media 7.4

Same name and namespace in other branches
  1. 7 media.module \media_parse_to_uri()
  2. 7.2 media.module \media_parse_to_uri()
  3. 7.3 media.module \media_parse_to_uri()

This will parse a url or embedded code into a unique URI.

The function will call all modules implementing hook_media_parse($url), which should return either a string containing a parsed URI or NULL.

@NOTE The implementing modules may throw an error, which will not be caught here; it's up to the calling function to catch any thrown errors.

@NOTE In emfield, we originally also accepted an array of regex patterns to match against. However, that module used a registration for providers, and simply stored the match in the database keyed to the provider object. However, other than the stream wrappers, there is currently no formal registration for media handling. Additionally, few, if any, stream wrappers will choose to store a straight match from the parsed URL directly into the URI. Thus, we leave both the matching and the final URI result to the implementing module in this implementation.

An alternative might be to do the regex pattern matching here, and pass a successful match back to the implementing module. However, that would require either an overloaded function or a new hook, which seems like more overhead than it's worth at this point.

@TODO Once hook_module_implements_alter() is in core (see the issue at http://drupal.org/node/692950) we may want to implement media_media_parse() to ensure we were passed a valid URL, rather than an unsupported or malformed embed code that wasn't caught earlier. It will needed to be weighted so it's called after all other streams have a go, as the fallback, and will need to throw an error.

Parameters

string $url: The original URL or embed code to parse.

Return value

string The unique URI for the file, based on its stream wrapper, or NULL.

See also

media_parse_to_file()

media_add_from_url_validate()

1 call to media_parse_to_uri()
media_parse_to_file in ./media.module
Parse a URL or embed code and return a file object.

File

./media.module, line 584
Media API

Code

function media_parse_to_uri($url) {

  // Trim any whitespace before parsing.
  $url = trim($url);
  foreach (module_implements('media_parse') as $module) {
    $success = module_invoke($module, 'media_parse', $url);
    $context = array(
      'url' => $url,
      'module' => $module,
    );
    drupal_alter('media_parse', $success, $context);
    if (isset($success)) {
      return $success;
    }
  }
}