From 34713d9acbbcf380f34d1675c3406f8714949ee7 Mon Sep 17 00:00:00 2001 From: buttle Date: Mon, 30 Aug 2021 17:27:26 +0200 Subject: [PATCH] injects our attribs into iframe element --- src/Site/BlockLayout/Vimeo.php | 72 +++++++++++++++++++--------- view/common/block-layout/vimeo.phtml | 8 ++-- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/Site/BlockLayout/Vimeo.php b/src/Site/BlockLayout/Vimeo.php index def2a8e..8561185 100644 --- a/src/Site/BlockLayout/Vimeo.php +++ b/src/Site/BlockLayout/Vimeo.php @@ -8,8 +8,8 @@ use Omeka\Site\BlockLayout\AbstractBlockLayout; use Laminas\View\Renderer\PhpRenderer; use Laminas\Form\FormElementManager; - use Vimeo\Form\VimeoBlockForm; +use DOMDocument; class Vimeo extends AbstractBlockLayout { @@ -66,27 +66,53 @@ class Vimeo extends AbstractBlockLayout public function render(PhpRenderer $view, SitePageBlockRepresentation $block) { - $attachments = $block->attachments(); - if (!$attachments) { - return ''; - } + $attachments = $block->attachments(); + if (!$attachments) { + return ''; + } + $media = $attachments[0]->item()->media()[0]; + $width = $media->mediaData()['width']; + $width = 100; + $height = 0; - $thumbnails = []; - static $id = 0; + // creates an iframe injecting our width and calculated ratio and height + $attribs=" "; + $original_width = 0; + $original_height = 0; + $doc = new DOMDocument(); + $doc->loadHTML($media->mediaData()['html']); + $html_element= $doc->getElementsByTagName('iframe')->item(0); + if ($html_element->hasAttributes()) { + foreach ($html_element->attributes as $attr) { + $name = $attr->nodeName; + $value = $attr->nodeValue; + if ($name == 'height') { + $original_height = $value; + continue; + } + if ($name == 'width') { + $original_width = $value; + $attrib = $name.'="'.$width.'%"'; + $attribs .= $attrib.PHP_EOL; + continue; + } + $attrib = $name.'="'.$value.'"'; + $attribs .= $attrib.PHP_EOL; + } + } + if ($original_height !== 0) { + $ratio = $original_width / $original_height; + $attrib = 'ratio="'.$ratio.'"'; + $attribs .= $attrib.PHP_EOL; + } + $iframe = ""; - $media = $attachments[0]->item()->media()[0]; - $vimeo_width = $media->mediaData()['width']; - $vimeo_height = $media->mediaData()['height']; - $ratio = $vimeo_width / $vimeo_height; - - return $view->partial('common/block-layout/vimeo', [ - 'title' => $block->dataValue('title'), - 'renderSourceLink' => $block->dataValue('renderSourceLink'), - 'item_url' => $attachments[0]->item()->url(), - 'width' => $block->dataValue('width'), - 'video_id' => $media->mediaData()['video_id'], - 'ratio' => $ratio, - 'vimeo_iframe' => $media->mediaData()['html'], - ]); - } -} + return $view->partial('common/block-layout/vimeo', [ + 'title' => $block->dataValue('title'), + 'renderSourceLink' => $block->dataValue('renderSourceLink'), + 'item_url' => $attachments[0]->item()->url(), + 'ratio' => $ratio, + 'vimeo_iframe' => $iframe + ]); + } + } diff --git a/view/common/block-layout/vimeo.phtml b/view/common/block-layout/vimeo.phtml index ce87dde..86b1d20 100644 --- a/view/common/block-layout/vimeo.phtml +++ b/view/common/block-layout/vimeo.phtml @@ -3,7 +3,8 @@ $this->headScript()->appendFile($this->assetUrl('js/height_calculator.js', 'Vimeo')); ?> -
+