diff --git a/js/stanford-media.js b/js/stanford-media.js new file mode 100644 index 0000000..cec4161 --- /dev/null +++ b/js/stanford-media.js @@ -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); diff --git a/stanford_media.libraries.yml b/stanford_media.libraries.yml index e4bd783..d8ff155 100644 --- a/stanford_media.libraries.yml +++ b/stanford_media.libraries.yml @@ -10,3 +10,7 @@ display: css: component: dist/css/stanford_media.css: {} + js: + js/stanford-media.js: {} + dependencies: + - core/once diff --git a/stanford_media.module b/stanford_media.module index d6069c0..9fe0c74 100644 --- a/stanford_media.module +++ b/stanford_media.module @@ -5,13 +5,13 @@ * 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; @@ -19,6 +19,50 @@ 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(). */ @@ -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'; } /**