Guest User

Untitled

a guest
Jan 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. <?php
  2. namespace CompanyNameModuleNameControllerIndex;
  3.  
  4. use MagentoFrameworkAppActionAction;
  5.  
  6. class Test extends Action
  7. {
  8. protected $videoGalleryProcessor;
  9. protected $_product;
  10. public function __construct(
  11. MagentoFrameworkAppActionContext $context,
  12. MagentoCatalogModelProduct $product,
  13. CompanyNameModuleNameModelProductGalleryVideoProcessor $videoGalleryProcessor
  14. ){
  15. parent::__construct($context);
  16. $this->_product = $product;
  17. $this->videoGalleryProcessor = $videoGalleryProcessor;
  18. }
  19.  
  20. public function execute()
  21. {
  22. $productId = 1; // product id
  23. $product = $this->_product->load($productId);
  24. $product->setStoreId(0); //set store vise data
  25.  
  26. // sample video data
  27. $videoData = [
  28. 'video_id' => "abc", //set your video id
  29. 'video_title' => "title", //set your video title
  30. 'video_description' => "description", //set your video description
  31. 'thumbnail' => "image path", //set your video thumbnail path.
  32. 'video_provider' => "youtube",
  33. 'video_metadata' => null,
  34. 'video_url' => "https://www.youtube.com/watch?v=abc", //set your youtube video url
  35. 'media_type' => MagentoProductVideoModelProductAttributeMediaExternalVideoEntryConverter::MEDIA_TYPE_CODE,
  36. ];
  37.  
  38. //download thumbnail image and save locally under pub/media
  39. $videoData['file'] = $videoData['video_id'] . 'filename.jpg';
  40.  
  41. // Add video to the product
  42. if ($product->hasGalleryAttribute()) {
  43. $this->videoGalleryProcessor->addVideo(
  44. $product,
  45. $videoData,
  46. ['image', 'small_image', 'thumbnail'],
  47. false,
  48. true
  49. );
  50. }
  51. $product->save();
  52. }
  53. }
  54.  
  55. <?php
  56. namespace CompanyNameModuleNameModelProductGalleryVideo;
  57.  
  58. use MagentoFrameworkExceptionLocalizedException;
  59.  
  60. class Processor extends MagentoCatalogModelProductGalleryProcessor
  61. {
  62. /**
  63. * @var MagentoCatalogModelProductGalleryCreateHandler
  64. */
  65. protected $createHandler;
  66.  
  67. /**
  68. * Processor constructor.
  69. * @param MagentoCatalogApiProductAttributeRepositoryInterface $attributeRepository
  70. * @param MagentoMediaStorageHelperFileStorageDatabase $fileStorageDb
  71. * @param MagentoCatalogModelProductMediaConfig $mediaConfig
  72. * @param MagentoFrameworkFilesystem $filesystem
  73. * @param MagentoCatalogModelResourceModelProductGallery $resourceModel
  74. * @param MagentoCatalogModelProductGalleryCreateHandler $createHandler
  75. */
  76. public function __construct(
  77. MagentoCatalogApiProductAttributeRepositoryInterface $attributeRepository,
  78. MagentoMediaStorageHelperFileStorageDatabase $fileStorageDb,
  79. MagentoCatalogModelProductMediaConfig $mediaConfig,
  80. MagentoFrameworkFilesystem $filesystem,
  81. MagentoCatalogModelResourceModelProductGallery $resourceModel,
  82. MagentoCatalogModelProductGalleryCreateHandler $createHandler
  83. )
  84. {
  85. parent::__construct($attributeRepository, $fileStorageDb, $mediaConfig, $filesystem, $resourceModel);
  86. $this->createHandler = $createHandler;
  87. }
  88.  
  89. /**
  90. * @param MagentoCatalogModelProduct $product
  91. * @param array $videoData
  92. * @param array $mediaAttribute
  93. * @param bool $move
  94. * @param bool $exclude
  95. * @return string
  96. * @throws LocalizedException
  97. */
  98. public function addVideo(
  99. MagentoCatalogModelProduct $product,
  100. array $videoData,
  101. $mediaAttribute = null,
  102. $move = false,
  103. $exclude = true
  104. ) {
  105. $file = $this->mediaDirectory->getRelativePath($videoData['file']);
  106.  
  107. if (!$this->mediaDirectory->isFile($file)) {
  108. throw new LocalizedException(__('The image does not exist.'));
  109. }
  110.  
  111. $pathinfo = pathinfo($file);
  112. $imgExtensions = ['jpg', 'jpeg', 'gif', 'png'];
  113. if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
  114. throw new LocalizedException(__('Please correct the image file type.'));
  115. }
  116.  
  117. $fileName = MagentoMediaStorageModelFileUploader::getCorrectFileName($pathinfo['basename']);
  118. $dispretionPath = MagentoMediaStorageModelFileUploader::getDispretionPath($fileName);
  119. $fileName = $dispretionPath . '/' . $fileName;
  120.  
  121. $fileName = $this->getNotDuplicatedFilename($fileName, $dispretionPath);
  122.  
  123. $destinationFile = $this->mediaConfig->getTmpMediaPath($fileName);
  124.  
  125. try {
  126. /** @var $storageHelper MagentoMediaStorageHelperFileStorageDatabase */
  127. $storageHelper = $this->fileStorageDb;
  128. if ($move) {
  129. $this->mediaDirectory->renameFile($file, $destinationFile);
  130.  
  131. //Here, filesystem should be configured properly
  132. $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName));
  133. } else {
  134. $this->mediaDirectory->copyFile($file, $destinationFile);
  135.  
  136. $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName));
  137. }
  138. } catch (Exception $e) {
  139. throw new LocalizedException(__('We couldn't move this file: %1.', $e->getMessage()));
  140. }
  141.  
  142. $fileName = str_replace('\', '/', $fileName);
  143.  
  144. $attrCode = $this->getAttribute()->getAttributeCode();
  145. $mediaGalleryData = $product->getData($attrCode);
  146. $position = 0;
  147. if (!is_array($mediaGalleryData)) {
  148. $mediaGalleryData = ['images' => []];
  149. }
  150.  
  151. foreach ($mediaGalleryData['images'] as &$image) {
  152. if (isset($image['position']) && $image['position'] > $position) {
  153. $position = $image['position'];
  154. }
  155. }
  156.  
  157. $position++;
  158.  
  159. unset($videoData['file']);
  160. $mediaGalleryData['images'][] = array_merge([
  161. 'file' => $fileName,
  162. 'label' => $videoData['video_title'],
  163. 'position' => $position,
  164. 'disabled' => (int)$exclude
  165. ], $videoData);
  166.  
  167. $product->setData($attrCode, $mediaGalleryData);
  168.  
  169. if ($mediaAttribute !== null) {
  170. $product->setMediaAttribute($product, $mediaAttribute, $fileName);
  171. }
  172.  
  173. $this->createHandler->execute($product);
  174.  
  175. return $fileName;
  176. }
  177. }
  178.  
  179. <?php
  180. $_helper = $this->helper('MagentoCatalogHelperOutput');
  181. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  182. $_product = $objectManager->get('MagentoFrameworkRegistry')->registry('current_product');//get current product
  183. ?>
  184.  
  185. <?php if($proVideo = $_helper->productAttribute($_product,$_product->getVideoName(), 'video_name')):?>
  186. <?php $videoUrl = $this->getUrl('pub/media/catalog/product/videos/').$proVideo; ?>
  187. <div class="prod_video_bg">
  188. <video width="400" controls>
  189. <source src="<?php echo $videoUrl; ?>" type="video/mp4">
  190. </video>
  191. </div>
  192. <?php endif; ?>
Add Comment
Please, Sign In to add comment