function template_preprocess_file_upload_help

Same name in other branches
  1. 9 core/modules/file/file.field.inc \template_preprocess_file_upload_help()
  2. 8.9.x core/modules/file/file.field.inc \template_preprocess_file_upload_help()
  3. 11.x core/modules/file/file.module \template_preprocess_file_upload_help()

Prepares variables for file upload help text templates.

Default template: file-upload-help.html.twig.

Parameters

array $variables: An associative array containing:

  • description: The normal description for this field, specified by the user.
  • upload_validators: An array of upload validators as used in $element['#upload_validators'].
1 call to template_preprocess_file_upload_help()
LegacyFileThemeTest::testTemplatePreprocessFileUploadHelp in core/modules/file/tests/src/Kernel/LegacyFileThemeTest.php
@covers ::template_preprocess_file_upload_help

File

core/modules/file/file.module, line 1220

Code

function template_preprocess_file_upload_help(&$variables) {
    $description = $variables['description'];
    $upload_validators = $variables['upload_validators'];
    $cardinality = $variables['cardinality'];
    $descriptions = [];
    if (!empty($description)) {
        $descriptions[] = FieldFilteredMarkup::create($description);
    }
    if (isset($cardinality)) {
        if ($cardinality == -1) {
            $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
        }
        else {
            $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
        }
    }
    if (isset($upload_validators['file_validate_size'])) {
        @trigger_error('\'file_validate_size\' is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'FileSizeLimit\' constraint instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
        $descriptions[] = t('@size limit.', [
            '@size' => ByteSizeMarkup::create($upload_validators['file_validate_size'][0]),
        ]);
    }
    if (isset($upload_validators['FileSizeLimit'])) {
        $descriptions[] = t('@size limit.', [
            '@size' => ByteSizeMarkup::create($upload_validators['FileSizeLimit']['fileLimit']),
        ]);
    }
    if (isset($upload_validators['file_validate_extensions'])) {
        @trigger_error('\'file_validate_extensions\' is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'FileExtension\' constraint instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
        $descriptions[] = t('Allowed types: @extensions.', [
            '@extensions' => $upload_validators['file_validate_extensions'][0],
        ]);
    }
    if (isset($upload_validators['FileExtension'])) {
        $descriptions[] = t('Allowed types: @extensions.', [
            '@extensions' => $upload_validators['FileExtension']['extensions'],
        ]);
    }
    if (isset($upload_validators['file_validate_image_resolution']) || isset($upload_validators['FileImageDimensions'])) {
        if (isset($upload_validators['file_validate_image_resolution'])) {
            @trigger_error('\'file_validate_image_resolution\' is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'FileImageDimensions\' constraint instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
            $max = $upload_validators['file_validate_image_resolution'][0];
            $min = $upload_validators['file_validate_image_resolution'][1];
        }
        else {
            $max = $upload_validators['FileImageDimensions']['maxDimensions'];
            $min = $upload_validators['FileImageDimensions']['minDimensions'];
        }
        if ($min && $max && $min == $max) {
            $descriptions[] = t('Images must be exactly <strong>@size</strong> pixels.', [
                '@size' => $max,
            ]);
        }
        elseif ($min && $max) {
            $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels. Images larger than <strong>@max</strong> pixels will be resized.', [
                '@min' => $min,
                '@max' => $max,
            ]);
        }
        elseif ($min) {
            $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', [
                '@min' => $min,
            ]);
        }
        elseif ($max) {
            $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', [
                '@max' => $max,
            ]);
        }
    }
    $variables['descriptions'] = $descriptions;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.