Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created February 9, 2013 08:35
Show Gist options
  • Save bangpound/4744600 to your computer and use it in GitHub Desktop.
Save bangpound/4744600 to your computer and use it in GitHub Desktop.
Written for oEmbed module (but not committed), this snippet extracts script elements from the html property of oEmbed responses that are "video" or "rich." The scripts are added to a the render array as #attached. This also suggests a way to create produce opengraph meta tags from oEmbed media by using #attached['html_head']
<?php
$embed = $element['#embed'];
// Check if the oEmbed response provides a thumbnail image.
if (empty($embed['html'])) {
$element['#printed'] = TRUE;
return $element;
}
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
@$doc->loadHTML('<body>'. $embed['html'] .'</body>');
$scripts = $doc->getElementsByTagName('script');
foreach ($scripts as $script) {
if ($script->hasAttribute('src')) {
$src = $script->getAttribute('src');
$data = array(
'type' => 'external',
);
if ($script->hasAttribute('defer')) {
$data['defer'] = TRUE;
}
$element['#attached']['js'][$src] = $data;
$script->parentNode->removeChild($script);
}
}
// This would be the place to filter embedded HTML.
$element['#content'] = array(
'#markup' => '',
);
foreach ($doc->getElementsByTagName('body')->item(0)->childNodes as $node) {
$element['#content']['#markup'] .= $doc->saveHTML($node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment