2

I have added some videos to my product via the add video section under product "Images And Videos". I want to be able to display the videos added this way in a custom section. The code currently looks like this:

$images = $full_product->getMediaGalleryImages();
foreach($images as $image){
  if(in_array($image->getFile(), $used_images)){
    continue;
  }
  ?>
  <li>
    <?php
    if($image->getMediaType() == "external-video") { 
      ?>
      <iframe width="640" height="480" src="<?php echo $image->getVideoUrl(); ?>" frameborder="0" allowfullscreen></iframe>
      <?php
    } else { // general show an image
    ?>
      <img src="<?php echo $image->getUrl(); ?>" alt="<?php echo $_productNameStripped; ?>" />
    <?php } ?>
  </li>
<?php } ?>

I want to display only certain images and videos in the list (hence the $used_images bit), so far it is working fine except the getVideoUrl for youtube returns me a wrongly formatted youtube url.

I get: https://www.youtube.com/watch?v=abcdefghij

but I need: https://www.youtube.com/embed/abcdefghij

It's possible for me to write some php code that would pull out the video code and get me the correct url but I want to know if their are magento methods for this or displaying videos/iframes. Creating my own methods shortcut here might cause me problems with other magento features (such as vimeo videos) not working in this display.

1

Write code like that call

preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","<iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>", $image->getVideoUrl());
-1

You can get embeded code using,

$image->getVideo()

Now your iframe look like below way,

<iframe width="640" height="480" src="http://www.youtube.com/embed/<?php echo $image->getVideo() ?>" frameborder="0" allowfullscreen></iframe>
  • Gave that a try but I'm getting null when calling that. – Typhado Jul 6 '17 at 7:02
  • check which value is returned $full_product->getVideo() – Rakesh Jesadiya Jul 6 '17 at 7:07
  • Both return null. This is an image added via the image/media manager for magento 2 not a video attribute added to the product. – Typhado Jul 6 '17 at 7:09
  • What is $image in your case? It's simple DataObject. It doesn't have any methods, and __call getVideo() will return nothing. – zhartaunik May 21 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.