class Upload in Media entity video 8
Same name and namespace in other branches
- 8.3 src/Plugin/EntityBrowser/Widget/Upload.php \Drupal\media_entity_video\Plugin\EntityBrowser\Widget\Upload
- 8.2 src/Plugin/EntityBrowser/Widget/Upload.php \Drupal\media_entity_video\Plugin\EntityBrowser\Widget\Upload
Uses upload to create media entity videos.
Plugin annotation
@EntityBrowserWidget(
id = "media_entity_video_upload",
label = @Translation("Upload video files"),
description = @Translation("Upload widget that creates media entity videos.")
)
Hierarchy
- class \Drupal\media_entity_video\Plugin\EntityBrowser\Widget\Upload extends \Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload
Expanded class hierarchy of Upload
File
- src/
Plugin/ EntityBrowser/ Widget/ Upload.php, line 19
Namespace
Drupal\media_entity_video\Plugin\EntityBrowser\WidgetView source
class Upload extends FileUpload {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'extensions' => 'mp4 webm ogg ogv',
'media bundle' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
if (!$this->configuration['media bundle'] || !($bundle = $this->entityTypeManager
->getStorage('media_bundle')
->load($this->configuration['media bundle']))) {
return [
'#markup' => $this
->t('The media bundle is not configured correctly.'),
];
}
if ($bundle
->getType()
->getPluginId() != 'video') {
return [
'#markup' => $this
->t('The configured bundle is not using video plugin.'),
];
}
$form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
$form['upload']['#upload_validators']['file_validate_extensions'] = [
$this->configuration['extensions'],
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function prepareEntities(array $form, FormStateInterface $form_state) {
$files = parent::prepareEntities($form, $form_state);
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
$bundle = $this->entityTypeManager
->getStorage('media_bundle')
->load($this->configuration['media bundle']);
$videos = [];
foreach ($files as $file) {
/** @var \Drupal\media_entity\MediaInterface $video */
$video = $this->entityTypeManager
->getStorage('media')
->create([
'bundle' => $bundle
->id(),
$bundle
->getTypeConfiguration()['source_field'] => $file,
]);
$videos[] = $video;
}
return $videos;
}
/**
* {@inheritdoc}
*/
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
if (!empty($form_state
->getTriggeringElement()['#eb_widget_main_submit'])) {
$videos = $this
->prepareEntities($form, $form_state);
array_walk($videos, function (MediaInterface $media) {
$media
->save();
});
$this
->selectEntities($videos, $form_state);
$this
->clearFormValues($element, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$bundle_options = [];
$form = parent::buildConfigurationForm($form, $form_state);
$form['extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Allowed extensions'),
'#default_value' => $this->configuration['extensions'],
'#required' => TRUE,
];
$bundles = $this->entityTypeManager
->getStorage('media_bundle')
->loadByProperties([
'type' => 'video',
]);
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
foreach ($bundles as $bundle) {
$bundle_options[$bundle
->id()] = $bundle
->label();
}
if (empty($bundle_options)) {
$url = Url::fromRoute('media.bundle_add')
->toString();
$form['media bundle'] = [
'#markup' => $this
->t("You don't have media bundle of the Video type. You should <a href='!link'>create one</a>", [
'!link' => $url,
]),
];
}
else {
$form['media bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Media bundle'),
'#default_value' => $this->configuration['media bundle'],
'#options' => $bundle_options,
];
}
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Upload:: |
public | function | ||
Upload:: |
public | function | ||
Upload:: |
public | function | ||
Upload:: |
protected | function | ||
Upload:: |
public | function |