Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created October 20, 2012 15:06
Show Gist options
  • Save bangpound/3923512 to your computer and use it in GitHub Desktop.
Save bangpound/3923512 to your computer and use it in GitHub Desktop.
oembed wip
diff --git a/oembedcore.module b/oembedcore.module
index e473913..ac774ea 100644
--- a/oembedcore.module
+++ b/oembedcore.module
@@ -110,7 +110,7 @@ function oembedcore_theme() {
'template' => 'oembed',
'file' => 'oembedcore_theme.inc',
'path' => $path,
- 'variables' => array('embed' => NULL, 'title' => '', 'content' => ''),
+ 'render element' => 'element',
),
);
}
@@ -441,7 +441,7 @@ function oembedcore_oembed_fetch($provider, $url, $matches, $attributes = array(
// handy when theming the output.
if ($embed) {
$embed->original_url = $url;
- $embed->provider = $provider;
+ $embed->provider_machine_name = $provider['name'];
}
return $embed;
@@ -459,6 +459,7 @@ function oembedcore_element_info() {
'#attributes' => array(),
'#pre_render' => array(
'oembedcore_pre_render_fetch',
+ 'oembedcore_pre_render_attributes',
'oembedcore_pre_render_retheme',
'oembedcore_pre_render_content',
),
@@ -470,6 +471,7 @@ function oembedcore_element_info() {
'#attributes' => array(),
'#pre_render' => array(
'oembedcore_pre_render_fetch',
+ 'oembedcore_pre_render_attributes',
'oembedcore_pre_render_children',
),
);
@@ -482,6 +484,7 @@ function oembedcore_element_info() {
'#pre_render' => array(
'oembedcore_pre_render_pre_fetch',
'oembedcore_pre_render_fetch',
+ 'oembedcore_pre_render_attributes',
'oembedcore_pre_render_content',
'oembedcore_pre_render_thumbnail',
),
@@ -499,6 +502,42 @@ function oembedcore_pre_render_pre_fetch($element) {
}
/**
+ *
+ */
+function oembedcore_pre_render_attributes($element) {
+
+ // Only act when the oEmbed response is true.
+ if (!empty($element['#printed'])) {
+ return $element;
+ }
+
+ $embed = $element['#embed'];
+
+ $element['#attributes_array'] = array(
+ 'class' => 'oembed oembed-'. $embed->type,
+ );
+ $element['#title_attributes_array'] = array(
+ 'class' => 'oembed-title',
+ );
+ $element['#content_attributes_array'] = array(
+ 'class' => 'oembed-content',
+ );
+
+ if (!empty($embed->provider_name)) {
+ $provider_name = $embed->provider_name;
+ }
+ else if (!empty($embed->provider_url)) {
+ $provider_name = $embed->provider_url;
+ }
+ else {
+ $provider_name = parse_url($element['#url'], PHP_URL_HOST);
+ }
+ $element['#attributes_array']['class'] .= ' oembed-'. drupal_html_class($provider_name);
+
+ return $element;
+}
+
+/**
* Change oEmbed request into a thumbnail.
*
* This is here for Media module WYSIWYG support. When a video or image is added
@@ -601,9 +640,7 @@ function oembedcore_pre_render_children($element) {
'#href' => $element['#url'],
'#options' => array(
'absolute' => TRUE,
- 'attributes' => array(
- 'class' => 'oembed-title'
- )
+ 'attributes' => $element['#title_attributes_array'],
),
);
}
@@ -612,7 +649,7 @@ function oembedcore_pre_render_children($element) {
// oEmbed photos are constructed entirely with HTML phrase elements.
case 'photo':
- $element['#prefix'] = '<span class="oembed oembed-photo">';
+ $element['#prefix'] = '<span'. drupal_attributes($element['#attributes_array']) .'>';
$element['#suffix'] = '</span>';
$attributes = array(
@@ -627,9 +664,7 @@ function oembedcore_pre_render_children($element) {
'#options' => array(
'html' => TRUE,
'absolute' => TRUE,
- 'attributes' => array(
- 'class' => 'oembed-content'
- )
+ 'attributes' => $element['#content_attributes_array'],
),
);
break;
@@ -640,7 +675,7 @@ function oembedcore_pre_render_children($element) {
// hook_element_info_alter().
case 'link':
if (!empty($embed->title)) {
- $element['title']['#options']['attributes']['class'] .= ' oembed-link';
+ $element['title']['#options']['attributes']['class'] .= ' '. $element['#attributes_array']['class'];
}
break;
@@ -648,11 +683,11 @@ function oembedcore_pre_render_children($element) {
// cannot be used.
case 'rich':
case 'video':
- $element['#prefix'] = '<div class="oembed oembed-'. $embed->type . '">';
+ $element['#prefix'] = '<div'. drupal_attributes($element['#attributes_array']) .'>';
$element['#suffix'] = '</div>';
$element['content'] = array(
- '#prefix' => '<div class="oembed-content">',
+ '#prefix' => '<div'. drupal_attributes($element['#content_attributes_array']) .'>',
'#markup' => $embed->html,
'#suffix' => '</div>',
);
@@ -673,7 +708,7 @@ function oembedcore_pre_render_retheme($element) {
}
$embed = $element['#embed'];
- $element['#theme'] = 'oembed__'. $embed->type .'__'. $embed->provider['name'];
+ $element['#theme'] = 'oembed__'. $embed->type .'__'. $embed->provider_name;
return $element;
}
@@ -689,6 +724,8 @@ function oembedcore_pre_render_fetch($element) {
return $element;
}
+ drupal_alter(array('oembed_response') + array($element['#type'] .'_response'), $embed);
+
$element['#embed'] = $embed;
// Recalculate cache expire time based on response.
diff --git a/oembedprovider/oembedprovider.inc b/oembedprovider/oembedprovider.inc
index c97b1e6..e010433 100644
--- a/oembedprovider/oembedprovider.inc
+++ b/oembedprovider/oembedprovider.inc
@@ -29,10 +29,11 @@ function _oembedprovider_handle_request($format = '') {
// Check that we got a url
if (empty($_GET['url'])) {
header('HTTP/1.0 406 Not acceptable. The url parameter is required.');
- print call_user_func($f['callback'], _oembedprovider_result('link', array(
+ print call_user_func($f['callback'], array(
+ 'type' => 'link',
'title' => t('The url parameter is required'),
'error' => 1,
- )));
+ ));
die;
}
@@ -56,19 +57,21 @@ function _oembedprovider_handle_request($format = '') {
}
else {
header('HTTP/1.0 404 Not found.');
- print call_user_func($f['callback'], _oembedprovider_result('link', array(
+ print call_user_func($f['callback'], array(
+ 'type' => 'link',
'title' => t('Could not fetch an embed for this url'),
'error' => 1,
- )));
+ ));
}
die;
}
else {
header('HTTP/1.0 404 Not found.');
- print call_user_func($f['callback'], _oembedprovider_result('rich', array(
+ print call_user_func($f['callback'], array(
+ 'type' => 'rich',
'title' => t('Could not find a provider that supports this url'),
'error' => 1,
- )));
+ ));
die;
}
}
@@ -81,7 +84,6 @@ function _oembedprovider_handle_request($format = '') {
*/
function _oembedprovider_result($type, $props) {
return array_merge(array(
- 'type' => $type,
'version' => '1.0',
'provider_name' => variable_get('site_name', ''),
'width' => 0,
@@ -108,14 +110,18 @@ function _oembedprovider_node_provider($provider, $url, $matches) {
$node = node_load($nid);
if ($node && node_access('view', $node, drupal_anonymous_user())) {
$author = user_load($node->uid);
- //TODO: The title and author data is currently returned as part of the rich-text html as well - this makes some clients show duplicate title and/or author information
- // TODO Please change this theme call to use an associative array for the $variables parameter.
- $result = _oembedprovider_result('rich', array(
+ // TODO: The title and author data is currently returned as part of the
+ // rich-text html as well - this makes some clients show duplicate title
+ // and/or author information.
+ // TODO: Please change this theme call to use an associative array for the
+ // $variables parameter.
+ $result = array(
'html' => theme('oembed_node', array('node' => $node)),
+ 'type' => 'rich',
'title' => $node->title,
'author_name' => $author->name,
'author_url' => url('user/' . $author->uid, array('absolute' => TRUE)),
- ));
+ );
}
unset($block_endless_recursion[$nid]);
diff --git a/oembedprovider/oembedprovider.module b/oembedprovider/oembedprovider.module
index 778f763..ebcdbfc 100644
--- a/oembedprovider/oembedprovider.module
+++ b/oembedprovider/oembedprovider.module
@@ -5,75 +5,29 @@
* Module for providing content as defined in the oEmbed specification
*/
-/**
- * Implements hook_menu().
- */
-function oembedprovider_menu() {
- $menu = array();
-
- $handler = array(
- 'type' => MENU_CALLBACK,
- 'file' => 'oembedprovider.inc',
- 'page callback' => '_oembedprovider_handle_request',
- 'page arguments' => array(2),
- 'access arguments' => array('access content'),
+function oembedprovider_server_info() {
+ return array(
+ 'name' => 'oEmbed provider',
+ 'path' => 'oembed',
);
-
- $menu['oembed/endpoint'] = $menu['oembed/endpoint/%'] = $handler;
-
- return $menu;
}
-/**
- * Implements hook_theme().
- */
-function oembedprovider_theme() {
- $themes = array();
- $themes['oembed_node'] = array(
- 'arguments' => array(
- 'node' => NULL,
- ),
- );
- return $themes;
-}
+function oembedprovider_server() {
+ $endpoint_path = services_get_server_info('endpoint_path', 'services/oembed');
+ $canonical_path = trim(drupal_substr($_GET['q'], drupal_strlen($endpoint_path)), '/');
+ $canonical_path = explode('/', $_GET['q']);
+ $endpoint_path_count = count(explode('/', $endpoint_path));
+ for ($x = 0; $x < $endpoint_path_count; $x++) {
+ array_shift($canonical_path);
+ }
+ $canonical_path = implode('/', $canonical_path);
+ if (empty($canonical_path)) {
+ $canonical_path = $_GET['url'];
+ }
-/**
- * The default provider to handle nodes
- *
- * @param string $url
- * @param array $matches
- */
-function oembedprovider_node_provider($provider, $url, $matches) {
module_load_include('inc', 'oembedprovider');
- return _oembedprovider_node_provider($provider, $url, $matches);
-}
-
-/**
- * Default theme implementation for oembed_node. We just mirror the node_view function
- * and pass everything on to the node template.
- *
- * @param object $node
- * @return string
- * The html representation of the node
- */
-function theme_oembed_node($vars) {
- $node = $vars['node'];
- $build = node_view($node);
- return drupal_render($build);
-}
-/**
- * Implements hook_oembedprovider().
- *
- * @return array
- */
-function oembedprovider_oembedprovider() {
- $base_url = url('', array('absolute' => TRUE)) . 'node/*';
- return array(
- $base_url => array(
- 'callback' => 'oembedprovider_node_provider',
- ),
- );
+ return _oembedprovider_handle_request();
}
/**
diff --git a/theme/oembedcore_theme.inc b/theme/oembedcore_theme.inc
index 0e89555..3f231c8 100644
--- a/theme/oembedcore_theme.inc
+++ b/theme/oembedcore_theme.inc
@@ -9,14 +9,19 @@
* Template for oEmbed theme.
*/
function template_preprocess_oembed(&$vars, $hook) {
- $embed = $vars['embed'];
+ $element = $vars['element'];
+ $embed = $element['#embed'];
if ($embed) {
+ $vars['content'] = $element['#content'];
+ $vars['title'] = $element['#title'];
$vars['original_url'] = $embed->original_url;
- $vars['classes_array'][] = 'oembed-'. $embed->type;
- $vars['title_attributes_array']['class'][] = 'oembed-title';
- $vars['content_attributes_array']['class'][] = 'oembed-content';
+ $vars['classes_array'] += explode(' ', $vars['element']['#attributes_array']['class']);
+ unset($element['#attributes_array']['class']);
+
+ $vars['title_attributes_array'] += $element['#title_attributes_array'];
+ $vars['content_attributes_array'] += $element['#content_attributes_array'];
// oEmbed links render using core link theme unless other preprocess
// functions suggest other hooks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment