3 namespace Drupal\bootstrap\Plugin\Preprocess;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Variables;
7 use Drupal\Component\Render\FormattableMarkup;
8 use Drupal\Component\Utility\Html;
9 use Drupal\Core\Field\FieldFilteredMarkup;
13 * Pre-processes variables for the "file_upload_help" theme hook.
15 * @ingroup plugins_preprocess
17 * @BootstrapPreprocess("file_upload_help",
18 * replace = "template_preprocess_file_upload_help"
21 class FileUploadHelp extends PreprocessBase implements PreprocessInterface {
26 public function preprocessVariables(Variables $variables) {
27 if (!empty($variables['description'])) {
28 $variables['description'] = FieldFilteredMarkup::create($variables['description']);
33 $cardinality = $variables['cardinality'];
34 if (isset($cardinality)) {
35 if ($cardinality == -1) {
36 $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
39 $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
43 $upload_validators = $variables['upload_validators'];
44 if (isset($upload_validators['file_validate_size'])) {
45 $descriptions[] = t('@size limit.', ['@size' => format_size($upload_validators['file_validate_size'][0])]);
47 if (isset($upload_validators['file_validate_extensions'])) {
48 $extensions = new FormattableMarkup('<code>@extensions</code>', [
49 '@extensions' => implode(', ', explode(' ', $upload_validators['file_validate_extensions'][0])),
51 $descriptions[] = t('Allowed types: @extensions.', ['@extensions' => $extensions]);
54 if (isset($upload_validators['file_validate_image_resolution'])) {
55 $max = $upload_validators['file_validate_image_resolution'][0];
56 $min = $upload_validators['file_validate_image_resolution'][1];
57 if ($min && $max && $min == $max) {
58 $descriptions[] = t('Images must be exactly <strong>@size</strong> pixels.', ['@size' => $max]);
60 elseif ($min && $max) {
61 $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]);
64 $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', ['@min' => $min]);
67 $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', ['@max' => $max]);
71 $variables['descriptions'] = $descriptions;
73 if ($descriptions && $this->theme->getSetting('popover_enabled')) {
75 $id = Html::getUniqueId('upload-instructions');
78 '#title' => t('Upload requirements'),
79 '#url' => Url::fromUserInput("#$id"),
80 '#icon' => Bootstrap::glyphicon('question-sign'),
82 'class' => ['icon-before'],
83 'data-toggle' => 'popover',
84 'data-html' => 'true',
85 'data-placement' => 'bottom',
86 'data-title' => t('Upload requirements'),
89 $build['requirements'] = [
90 '#type' => 'container',
91 '#theme_wrappers' => ['container__file_upload_help'],
94 'class' => ['hidden', 'help-block'],
95 'aria-hidden' => 'true',
98 $build['requirements']['descriptions'] = [
99 '#theme' => 'item_list__file_upload_help',
100 '#items' => $descriptions,
102 $variables['popover'] = $build;