public function MediaBulkUploadForm::buildForm in Media Bulk Upload 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
\Drupal\media_bulk_upload\Entity\MediaBulkConfigInterface|null $media_bulk_config: The media bulk configuration entity.
Return value
array The form structure.
Throws
\Exception
Overrides FormInterface::buildForm
File
- src/
Form/ MediaBulkUploadForm.php, line 148
Class
- MediaBulkUploadForm
- Class BulkMediaUploadForm.
Namespace
Drupal\media_bulk_upload\FormCode
public function buildForm(array $form, FormStateInterface $form_state, MediaBulkConfigInterface $media_bulk_config = NULL) {
$mediaBulkConfig = $media_bulk_config;
if ($mediaBulkConfig === NULL) {
return $form;
}
$mediaTypeManager = $this->mediaSubFormManager
->getMediaTypeManager();
$mediaTypes = $this->mediaSubFormManager
->getMediaTypeManager()
->getBulkMediaTypes($mediaBulkConfig);
$mediaTypeLabels = [];
foreach ($mediaTypes as $mediaType) {
$extensions = $mediaTypeManager
->getMediaTypeExtensions($mediaType);
natsort($extensions);
$this
->addAllowedExtensions($extensions);
$maxFileSize = $mediaTypeManager
->getTargetFieldMaxSize($mediaType);
if (empty($maxFileSize)) {
$maxFileSize = $this->mediaSubFormManager
->getDefaultMaxFileSize();
}
$mediaTypeLabels[] = $mediaType
->label() . ' (max ' . $maxFileSize . '): ' . implode(', ', $extensions);
if ($this
->isMaxFileSizeLarger($maxFileSize)) {
$this
->setMaxFileSizeForm($maxFileSize);
}
}
$form['#tree'] = TRUE;
$form['information_wrapper'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'media-bulk-upload-information-wrapper',
],
],
];
$form['information_wrapper']['information_label'] = [
'#type' => 'html_tag',
'#tag' => 'label',
'#value' => $this
->t('Information'),
'#attributes' => [
'class' => [
'form-control-label',
],
'for' => 'media_bulk_upload_information',
],
];
$form['information_wrapper']['information'] = [
'#theme' => 'item_list',
'#title' => $this
->t('Media Types:'),
'#items' => $mediaTypeLabels,
];
$form['information_wrapper']['warning'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#id' => 'media_bulk_upload_information',
'#name' => 'media_bulk_upload_information',
'#value' => '<p>Please be
aware that if file extensions overlap between the media types that are
available in this upload form, that the media entity will be assigned
automatically to one of these types.</p>',
];
$form['dropzonejs'] = [
'#type' => 'dropzonejs',
'#title' => $this
->t('Dropzone'),
'#required' => TRUE,
'#dropzone_description' => $this
->t('Click or drop your files here'),
'#max_filesize' => $this->maxFileSizeForm,
'#extensions' => implode(' ', $this->allowed_extensions),
];
if ($this->mediaSubFormManager
->validateMediaFormDisplayUse($mediaBulkConfig)) {
$form['fields'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Fields'),
'shared' => [
'#field_parents' => [
'fields',
'shared',
],
'#parents' => [
'fields',
'shared',
],
],
];
$this->mediaSubFormManager
->buildMediaSubForm($form['fields']['shared'], $form_state, $mediaBulkConfig);
}
$form['media_bundle_config'] = [
'#type' => 'value',
'#value' => $mediaBulkConfig
->id(),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}