formElementManager = $formElementManager;
$this->defaultSettings = $defaultSettings;
}
public function getLabel() {
return 'Vimeo video';
}
public function form(PhpRenderer $view,
SiteRepresentation $site,
SitePageRepresentation $page = null,
SitePageBlockRepresentation $block = null
) {
$form = $this->formElementManager->get(VimeoBlockForm::class);
$data = $block
? $block->data() + $this->defaultSettings
: $this->defaultSettings;
$form->setData([
'o:block[__blockIndex__][o:data][title]' => $data['title'],
'o:block[__blockIndex__][o:data][width]' => $data['width'],
'o:block[__blockIndex__][o:data][ratio]' => $data['ratio'],
'o:block[__blockIndex__][o:data][renderSourceLink]' => $data['renderSourceLink'],
]);
$form->prepare();
$html = '';
$html .= $view->blockAttachmentsForm($block);
$html .= '';
$html .= $view->translate('Options'). '
';
$html .= '
';
$html .= $view->formCollection($form);
$html .= '
';
return $html;
}
public function render(PhpRenderer $view, SitePageBlockRepresentation $block)
{
$attachments = $block->attachments();
if (!$attachments) {
return '';
}
$media = $attachments[0]->item()->media()[0];
$width = $media->mediaData()['width'];
$width = 100;
$height = 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 = "";
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
]);
}
}