4 * Contains \Drupal\bootstrap\Plugin\Preprocess\FileUploadHelp.
7 namespace Drupal\bootstrap\Plugin\Preprocess;
9 use Drupal\bootstrap\Annotation\BootstrapPreprocess;
10 use Drupal\bootstrap\Bootstrap;
11 use Drupal\bootstrap\Utility\Element;
12 use Drupal\bootstrap\Utility\Variables;
13 use Drupal\Component\Render\FormattableMarkup;
14 use Drupal\Component\Utility\Html;
15 use Drupal\Core\Field\FieldFilteredMarkup;
19 * Pre-processes variables for the "file_upload_help" theme hook.
21 * @ingroup plugins_preprocess
23 * @BootstrapPreprocess("file_upload_help",
24 * replace = "template_preprocess_file_upload_help"
27 class FileUploadHelp extends PreprocessBase implements PreprocessInterface {
32 public function preprocessVariables(Variables $variables) {
33 if (!empty($variables['description'])) {
34 $variables['description'] = FieldFilteredMarkup::create($variables['description']);
39 $cardinality = $variables['cardinality'];
40 if (isset($cardinality)) {
41 if ($cardinality == -1) {
42 $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
45 $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
49 $upload_validators = $variables['upload_validators'];
50 if (isset($upload_validators['file_validate_size'])) {
51 $descriptions[] = t('@size limit.', ['@size' => format_size($upload_validators['file_validate_size'][0])]);
53 if (isset($upload_validators['file_validate_extensions'])) {
54 $extensions = new FormattableMarkup('<code>@extensions</code>', [
55 '@extensions' => implode(', ', explode(' ', $upload_validators['file_validate_extensions'][0])),
57 $descriptions[] = t('Allowed types: @extensions.', ['@extensions' => $extensions]);
60 if (isset($upload_validators['file_validate_image_resolution'])) {
61 $max = $upload_validators['file_validate_image_resolution'][0];
62 $min = $upload_validators['file_validate_image_resolution'][1];
63 if ($min && $max && $min == $max) {
64 $descriptions[] = t('Images must be exactly <strong>@size</strong> pixels.', ['@size' => $max]);
66 elseif ($min && $max) {
67 $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]);
70 $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', ['@min' => $min]);
73 $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', ['@max' => $max]);
77 $variables['descriptions'] = $descriptions;
79 if ($descriptions && $this->theme->getSetting('popover_enabled')) {
81 $id = Html::getUniqueId('upload-instructions');
84 '#title' => t('Upload requirements'),
85 '#url' => Url::fromUserInput("#$id"),
86 '#icon' => Bootstrap::glyphicon('question-sign'),
88 'class' => ['icon-before'],
89 'data-toggle' => 'popover',
90 'data-html' => 'true',
91 'data-placement' => 'bottom',
92 'data-title' => t('Upload requirements'),
95 $build['requirements'] = [
96 '#type' => 'container',
97 '#theme_wrappers' => ['container__file_upload_help'],
100 'class' => ['hidden', 'help-block'],
101 'aria-hidden' => 'true',
104 $build['requirements']['descriptions'] = [
105 '#theme' => 'item_list__file_upload_help',
106 '#items' => $descriptions,
108 $variables['popover'] = $build;