public static function WebformManagedFileBase::processManagedFile in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::processManagedFile()
Process callback for managed file elements.
File
- src/
Plugin/ WebformElement/ WebformManagedFileBase.php, line 690
Class
- WebformManagedFileBase
- Provides a base class webform 'managed_file' elements.
Namespace
Drupal\webform\Plugin\WebformElementCode
public static function processManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
// Disable inline form errors for multiple file upload checkboxes.
if (!empty($element['#multiple'])) {
foreach (Element::children($element) as $key) {
if (isset($element[$key]['selected'])) {
$element[$key]['selected']['#error_no_message'] = TRUE;
}
}
}
// Truncate multiple files.
// Checks if user has uploaded more files than allowed.
// @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::validateMultipleCount
// @see \Drupal\file\Element\ManagedFile::processManagedFile.
if (!empty($element['#multiple']) && $element['#multiple'] > 1 && !empty($element['#files']) && count($element['#files']) > $element['#multiple']) {
$total_files = count($element['#files']);
$multiple = $element['#multiple'];
$fids = [];
$removed_names = [];
$count = 0;
foreach ($element['#files'] as $delta => $file) {
if ($count >= $multiple) {
unset($element['file_' . $delta]);
unset($element['#files'][$delta]);
$removed_names[] = $file
->getFilename();
$file
->delete();
}
else {
$fids[] = $delta;
}
$count++;
}
$element['fids']['#value'] = $fids;
$element['#value']['fids'] = $fids;
$args = [
'%title' => $element['#title'],
'@max' => $element['#multiple'],
'@count' => $total_files,
'%list' => implode(', ', $removed_names),
];
$message = t('%title can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args);
\Drupal::messenger()
->addWarning($message);
}
if (!empty($element['#multiple']) && !empty($element['#files']) && count($element['#files']) === $element['#multiple']) {
$element['upload']['#access'] = FALSE;
// We can't complete remove the upload button because it breaks
// the Ajax callback. Instead, we are going visually hide it from
// browsers with JavaScript disabled.
$element['upload_button']['#attributes']['style'] = 'display:none';
}
// Preview uploaded file.
if (!empty($element['#file_preview'])) {
// Get the element's plugin object.
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
/** @var \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase $element_plugin */
$element_plugin = $element_manager
->getElementInstance($element);
// Get the webform submission.
/** @var \Drupal\webform\WebformSubmissionForm $form_object */
$form_object = $form_state
->getFormObject();
/** @var \Drupal\webform\webform_submission $webform_submission */
$webform_submission = $form_object
->getEntity();
// Create a temporary preview element with an overridden #format.
$preview_element = [
'#format' => $element['#file_preview'],
] + $element;
// Convert '#theme': file_link to a container with a file preview.
$fids = isset($element['#webform_key']) ? (array) $webform_submission
->getElementData($element['#webform_key']) : [];
foreach ($fids as $delta => $fid) {
$child_key = 'file_' . $fid;
// Make sure the child element exists.
if (!isset($element[$child_key])) {
continue;
}
// Set multiple options delta.
$options = [
'delta' => $delta,
];
$file = File::load((string) $fid);
// Make sure the file entity exists.
if (!$file) {
continue;
}
// Don't allow anonymous temporary files to be previewed.
// @see template_preprocess_file_link().
// @see webform_preprocess_file_link().
if ($file
->isTemporary() && $file
->getOwner()
->isAnonymous() && strpos($file
->getFileUri(), 'private://') === 0) {
continue;
}
$preview = $element_plugin
->previewManagedFile($preview_element, $webform_submission, $options);
if (isset($element[$child_key]['filename'])) {
// Single file.
// Covert file link to a container with preview.
unset($element[$child_key]['filename']['#theme']);
$element[$child_key]['filename']['#type'] = 'container';
$element[$child_key]['filename']['#attributes']['class'][] = 'webform-managed-file-preview';
$element[$child_key]['filename']['#attributes']['class'][] = Html::getClass($element['#type'] . '-preview');
$element[$child_key]['filename']['preview'] = $preview;
}
elseif (isset($element[$child_key]['selected'])) {
// Multiple files.
// Convert file link checkbox #title to preview.
$element[$child_key]['selected']['#wrapper_attributes']['class'][] = 'webform-managed-file-preview-wrapper';
$element[$child_key]['selected']['#wrapper_attributes']['class'][] = Html::getClass($element['#type'] . '-preview-wrapper');
$element[$child_key]['selected']['#label_attributes']['class'][] = 'webform-managed-file-preview';
$element[$child_key]['selected']['#label_attributes']['class'][] = Html::getClass($element['#type'] . '-preview');
$element[$child_key]['selected']['#title'] = \Drupal::service('renderer')
->render($preview);
}
}
}
// File placeholder.
if (!empty($element['#file_placeholder']) && (empty($element['#value']) || empty($element['#value']['fids']))) {
$element['file_placeholder'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'webform-managed-file-placeholder',
Html::getClass($element['#type'] . '-placeholder'),
],
],
// Display placeholder before file upload input.
'#weight' => $element['upload']['#weight'] - 1,
'content' => WebformHtmlEditor::checkMarkup($element['#file_placeholder']),
];
}
return $element;
}