public function WebformManagedFileBase::prepare in Webform 8.5
Same name and namespace in other branches
- 6.x src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::prepare()
Prepare an element to be rendered within a webform.
Parameters
array $element: An element.
\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.
Overrides WebformElementBase::prepare
See also
\Drupal\webform\Element\WebformCompositeBase::processWebformComposite
1 call to WebformManagedFileBase::prepare()
- WebformImageFile::prepare in src/
Plugin/ WebformElement/ WebformImageFile.php - Prepare an element to be rendered within a webform.
1 method overrides WebformManagedFileBase::prepare()
- WebformImageFile::prepare in src/
Plugin/ WebformElement/ WebformImageFile.php - Prepare an element to be rendered within a webform.
File
- src/
Plugin/ WebformElement/ WebformManagedFileBase.php, line 256
Class
- WebformManagedFileBase
- Provides a base class webform 'managed_file' elements.
Namespace
Drupal\webform\Plugin\WebformElementCode
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
// Track if this element has been processed because the work-around below
// for 'Issue #2705471: Webform states File fields' which nests the
// 'managed_file' element in a basic container, which triggers this element
// to processed a second time.
if (!empty($element['#webform_managed_file_processed'])) {
return;
}
$element['#webform_managed_file_processed'] = TRUE;
// Must come after #element_validate hook is defined.
parent::prepare($element, $webform_submission);
// Check if the URI scheme exists and can be used the upload location.
$scheme_options = static::getVisibleStreamWrappers();
$uri_scheme = $this
->getUriScheme($element);
if (!isset($scheme_options[$uri_scheme])) {
$element['#access'] = FALSE;
$this
->displayDisabledWarning($element);
}
elseif ($webform_submission) {
$element['#upload_location'] = $this
->getUploadLocation($element, $webform_submission
->getWebform());
}
// Get file limit.
if ($webform_submission) {
$file_limit = $webform_submission
->getWebform()
->getSetting('form_file_limit') ?: \Drupal::config('webform.settings')
->get('settings.default_form_file_limit') ?: '';
}
else {
$file_limit = '';
}
// Validate callbacks.
$element_validate = [];
// Convert File entities into file ids (akk fids).
$element_validate[] = [
get_class($this),
'validateManagedFile',
];
// Check file upload limit.
if ($file_limit) {
$element_validate[] = [
get_class($this),
'validateManagedFileLimit',
];
}
// NOTE: Using array_splice() to make sure that static::validateManagedFile
// is executed before all other validation hooks are executed but after
// \Drupal\file\Element\ManagedFile::validateManagedFile.
array_splice($element['#element_validate'], 1, 0, $element_validate);
// Upload validators.
// @see webform_preprocess_file_upload_help
$element['#upload_validators']['file_validate_size'] = [
$this
->getMaxFileSize($element),
];
$element['#upload_validators']['file_validate_extensions'] = [
$this
->getFileExtensions($element),
];
// Define 'webform_file_validate_extensions' which allows file
// extensions within webforms to be comma-delimited. The
// 'webform_file_validate_extensions' will be ignored by file_validate().
// @see file_validate()
// Issue #3136578: Comma-separate the list of allowed file extensions.
// @see https://www.drupal.org/project/drupal/issues/3136578
$element['#upload_validators']['webform_file_validate_extensions'] = [];
$element['#upload_validators']['webform_file_validate_name_length'] = [];
// Add file upload help to the element as #description, #help, or #more.
// Copy upload validator so that we can add webform's file limit to
// file upload help only.
$upload_validators = $element['#upload_validators'];
if ($file_limit) {
$upload_validators['webform_file_limit'] = [
Bytes::toInt($file_limit),
];
}
$file_upload_help = [
'#theme' => 'file_upload_help',
'#upload_validators' => $upload_validators,
'#cardinality' => empty($element['#multiple']) ? 1 : $element['#multiple'],
];
$file_help = isset($element['#file_help']) ? $element['#file_help'] : 'description';
if ($file_help !== 'none') {
if (isset($element["#{$file_help}"])) {
if (is_array($element["#{$file_help}"])) {
$file_help_content = $element["#{$file_help}"];
}
else {
$file_help_content = [
'#markup' => $element["#{$file_help}"],
];
}
$file_help_content += [
'#suffix' => '<br/>',
];
$element["#{$file_help}"] = [
'content' => $file_help_content,
];
}
else {
$element["#{$file_help}"] = [];
}
$element["#{$file_help}"]['file_upload_help'] = $file_upload_help;
}
// Issue #2705471: Webform states File fields.
// Workaround: Wrap the 'managed_file' element in a basic container.
if (!empty($element['#prefix'])) {
$container = [
'#prefix' => $element['#prefix'],
'#suffix' => $element['#suffix'],
];
unset($element['#prefix'], $element['#suffix']);
$container[$element['#webform_key']] = $element + [
'#webform_managed_file_processed' => TRUE,
];
$element = $container;
}
// Add process callback.
// Set element's #process callback so that is not replaced by
// additional #process callbacks.
$this
->setElementDefaultCallback($element, 'process');
$element['#process'][] = [
get_class($this),
'processManagedFile',
];
// Add managed file upload tracking.
if (\Drupal::moduleHandler()
->moduleExists('file')) {
$element['#attached']['library'][] = 'webform/webform.element.managed_file';
}
}