Skip to content

Commit

Permalink
HSD8-1527 Fetch oembed data and title on client side if none exists (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish authored Nov 28, 2023
1 parent 3035fe2 commit 69eafb7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
14 changes: 14 additions & 0 deletions js/stanford-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function ($, Drupal, once) {
'use strict';
Drupal.behaviors.stanfordMedia = {
attach: function (context, settings) {
$(once('oembed-titles', '[data-oembed-resource]', context)).each(function () {
const $oembedElement = $(this);
fetch($oembedElement.attr('data-oembed-resource'))
.then(response => response.json())
.then(oembedData => $oembedElement.attr('title', oembedData.title || `${oembedData.provider_name} ${oembedData.type}`));
});
},
};

})(jQuery, Drupal, once);
4 changes: 4 additions & 0 deletions stanford_media.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ display:
css:
component:
dist/css/stanford_media.css: {}
js:
js/stanford-media.js: {}
dependencies:
- core/once
48 changes: 46 additions & 2 deletions stanford_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,64 @@
* stanford_media.module
*/

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\editor\Entity\Editor;
use Drupal\file\FileInterface;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaInterface;
use Drupal\stanford_media\Form\MediaLibraryEmbeddableForm;
use Drupal\stanford_media\Form\MediaLibraryFileUploadForm;
use Drupal\stanford_media\Form\MediaLibraryGoogleFormForm;

/**
* Implements hook_preprocess_HOOK().
*/
function stanford_media_preprocess_field(&$variables) {
if ($variables['entity_type'] != 'media') {
return;
}

/** @var \Drupal\Core\Field\FieldItemListInterface $field_items */
$field_items = $variables['element']['#items'];
/** @var \Drupal\media\MediaInterface $media */
$media = $field_items->getEntity();

if ($media->getSource()->getPluginId() != 'oembed:video') {
return;
}

/** @var \Drupal\media\OEmbed\UrlResolverInterface $url_resolver */
$url_resolver = \Drupal::service('media.oembed.url_resolver');
foreach ($variables['items'] as $delta => &$item) {
if (
isset($item['content']['#attributes']['title']) ||
isset($item['content']['#iframe']['#attributes']['title'])
) {
// Title attribute is already provided.
continue;
}

$url = $media->get($variables['field_name'])->get($delta)->getString();
if ($url && UrlHelper::isValid($url)) {
$oembed_url = $url_resolver->getResourceUrl($url);
if (isset($item['content']['#iframe'])) {
// Support oembed lazyload module.
$item['content']['#iframe']['#attributes']['data-oembed-resource'] = $oembed_url;
}

$item['content']['#attributes']['data-oembed-resource'] = $oembed_url;

$variables['#attached']['library'][] = 'stanford_media/display';
}
}

}

/**
* Implements hook_entity_type_alter().
*/
Expand Down Expand Up @@ -140,7 +184,7 @@ function stanford_media_media_view_alter(array &$build, EntityInterface $entity,
foreach (_stanford_media_get_dialog_plugins($entity) as $plugin) {
$plugin->embedAlter($build, $entity);
}
$build['#attached']['library'][] = 'stanford_media/display';
// $build['#attached']['library'][] = 'stanford_media/display';
}

/**
Expand Down

0 comments on commit 69eafb7

Please sign in to comment.