Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewVideo.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class NewVideo extends \Magento\Backend\Block\Widget\Form\Generic
14 {
18  const PATH_ANCHOR_PRODUCT_VIDEO = 'catalog_product_video-link';
19 
23  protected $mediaHelper;
24 
28  protected $urlBuilder;
29 
33  protected $jsonEncoder;
34 
38  protected $videoSelector = '#media_gallery_content';
39 
48  public function __construct(
49  \Magento\Backend\Block\Template\Context $context,
50  \Magento\Framework\Registry $registry,
51  \Magento\Framework\Data\FormFactory $formFactory,
52  \Magento\ProductVideo\Helper\Media $mediaHelper,
53  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
54  array $data = []
55  ) {
56  parent::__construct($context, $registry, $formFactory, $data);
57  $this->mediaHelper = $mediaHelper;
58  $this->urlBuilder = $context->getUrlBuilder();
59  $this->jsonEncoder = $jsonEncoder;
60  $this->setUseContainer(true);
61  }
62 
69  protected function _prepareForm()
70  {
72  $form = $this->_formFactory->create([
73  'data' => [
74  'id' => 'new_video_form',
75  'class' => 'admin__scope-old',
76  'enctype' => 'multipart/form-data',
77  ]
78  ]);
79  $form->setUseContainer($this->getUseContainer());
80  $form->addField('new_video_messages', 'note', []);
81  $fieldset = $form->addFieldset('new_video_form_fieldset', []);
82  $fieldset->addField(
83  '',
84  'hidden',
85  [
86  'name' => 'form_key',
87  'value' => $this->getFormKey(),
88  ]
89  );
90  $fieldset->addField(
91  'item_id',
92  'hidden',
93  []
94  );
95  $fieldset->addField(
96  'file_name',
97  'hidden',
98  []
99  );
100  $fieldset->addField(
101  'video_provider',
102  'hidden',
103  [
104  'name' => 'video_provider',
105  ]
106  );
107  $fieldset->addField(
108  'video_url',
109  'text',
110  [
111  'class' => 'edited-data validate-url',
112  'label' => __('Url'),
113  'title' => __('Url'),
114  'required' => true,
115  'name' => 'video_url',
116  'note' => $this->getNoteVideoUrl(),
117  ]
118  );
119  $fieldset->addField(
120  'video_title',
121  'text',
122  [
123  'class' => 'edited-data',
124  'label' => __('Title'),
125  'title' => __('Title'),
126  'required' => true,
127  'name' => 'video_title',
128  ]
129  );
130  $fieldset->addField(
131  'video_description',
132  'textarea',
133  [
134  'class' => 'edited-data',
135  'label' => __('Description'),
136  'title' => __('Description'),
137  'name' => 'video_description',
138  ]
139  );
140  $fieldset->addField(
141  'new_video_screenshot',
142  'file',
143  [
144  'label' => __('Preview Image'),
145  'title' => __('Preview Image'),
146  'name' => 'image',
147  ]
148  );
149  $fieldset->addField(
150  'new_video_screenshot_preview',
151  'button',
152  [
153  'class' => 'preview-image-hidden-input',
154  'label' => '',
155  'name' => '_preview',
156  ]
157  );
158  $fieldset->addField(
159  'new_video_get',
160  'button',
161  [
162  'label' => '',
163  'title' => 'Get Video Information',
164  'name' => 'new_video_get',
165  'value' => __('Get Video Information'),
166  'class' => 'action-default'
167  ]
168  );
169  $this->addMediaRoleAttributes($fieldset);
170  $fieldset->addField(
171  'new_video_disabled',
172  'checkbox',
173  [
174  'class' => 'edited-data',
175  'label' => __('Hide from Product Page'),
176  'title' => __('Hide from Product Page'),
177  'name' => 'disabled',
178  ]
179  );
180  $this->setForm($form);
181  }
182 
188  public function getHtmlId()
189  {
190  if (null === $this->getData('id')) {
191  $this->setData('id', $this->mathRandom->getUniqueHash('id_'));
192  }
193  return $this->getData('id');
194  }
195 
201  public function getWidgetOptions()
202  {
203  return $this->jsonEncoder->encode(
204  [
205  'saveVideoUrl' => $this->getUrl('catalog/product_gallery/upload'),
206  'saveRemoteVideoUrl' => $this->getUrl('product_video/product_gallery/retrieveImage'),
207  'htmlId' => $this->getHtmlId(),
208  'youTubeApiKey' => $this->mediaHelper->getYouTubeApiKey(),
209  'videoSelector' => $this->videoSelector
210  ]
211  );
212  }
213 
219  protected function getProduct()
220  {
221  if (!$this->hasData('product')) {
222  $this->setData('product', $this->_coreRegistry->registry('product'));
223  }
224  return $this->getData('product');
225  }
226 
233  protected function addMediaRoleAttributes(Fieldset $fieldset)
234  {
235  $fieldset->addField('role-label', 'note', ['text' => __('Role')]);
236  $mediaRoles = $this->getProduct()->getMediaAttributes();
237  ksort($mediaRoles);
238  foreach ($mediaRoles as $mediaRole) {
239  $fieldset->addField(
240  'video_' . $mediaRole->getAttributeCode(),
241  'checkbox',
242  [
243  'class' => 'video_image_role',
244  'label' => __($mediaRole->getFrontendLabel()),
245  'title' => __($mediaRole->getFrontendLabel()),
246  'data-role' => 'role-type-selector',
247  'value' => $mediaRole->getAttributeCode(),
248  ]
249  );
250  }
251  return $this;
252  }
253 
259  protected function getNoteVideoUrl()
260  {
261  $result = __('YouTube and Vimeo supported.');
262  if ($this->mediaHelper->getYouTubeApiKey() === null) {
263  $result = __(
264  'Vimeo supported.<br />'
265  . 'To add YouTube video, please <a href="%1">enter YouTube API Key</a> first.',
266  $this->getConfigApiKeyUrl()
267  );
268  }
269  return $result;
270  }
271 
277  protected function getConfigApiKeyUrl()
278  {
279  return $this->urlBuilder->getUrl(
280  'adminhtml/system_config/edit',
281  [
282  'section' => 'catalog',
283  '_fragment' => self::PATH_ANCHOR_PRODUCT_VIDEO
284  ]
285  );
286  }
287 }
getData($key='', $index=null)
Definition: DataObject.php:119
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\ProductVideo\Helper\Media $mediaHelper, \Magento\Framework\Json\EncoderInterface $jsonEncoder, array $data=[])
Definition: NewVideo.php:48
setData($key, $value=null)
Definition: DataObject.php:72
addField($elementId, $type, $config, $after=false, $isAdvanced=false)
Definition: Fieldset.php:211