- Issue created by @FrankieD3
I've been tinkering with hook_preprocess_media_oembed_iframe()
for a bit and only just realized that the GET variable for the video start time had not been included.
I know there's various ways of achieving this from the field value in Twig, as I've done this in the past, such as simply using the raw source URL from the entity reference and rendering an iframe directly.
I'd much prefer to use the built-in rendering for this, but the big hangup lies in the start time GET variable. The only GET variable attached pre-render seems to be the feature=oembed
parameter.
Below is my current usage of the aforementioned hook. Is there anything I can do here to retrieve the parameter from the referenced entity?
function mytheme_preprocess_media_oembed_iframe(array &$variables) {
/** @var \Drupal\media\OEmbed\Resource $resource */
$resource = $variables['resource'];
if ($resource->getProvider()->getName() === 'YouTube') {
// Construct a DOMDocument from the given iframe markup.
$dom = Html::load((string) $variables['media']);
/** @var \DOMElement $iframe */
$iframe = $dom->getElementsByTagName('iframe')->item(0);
// Remove superfluous attributes.
$iframe->removeAttribute('allow');
$iframe->removeAttribute('allowfullscreen');
// Allow for the iframe to be lazy loaded.
$iframe->setAttribute('loading', 'lazy');
// Alter the iframe src attribute,
$iframe_src = $iframe->getAttribute('src');
$iframe_src = str_replace('www.youtube.com', 'www.youtube-nocookie.com', $iframe_src);
// Append the start time parameter...
$iframe->setAttribute('src', $iframe_src);
// Replace the media with the altered markup.
$variables['media'] = Html::serialize($dom);
}
}
Active
10.2 β¨
Last updated