526f2259eca4b1b943efc34ce3c699f0146da150
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / FileUploadHelp.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Preprocess\FileUploadHelp.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Preprocess;
8
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;
16 use Drupal\Core\Url;
17
18 /**
19  * Pre-processes variables for the "file_upload_help" theme hook.
20  *
21  * @ingroup plugins_preprocess
22  *
23  * @BootstrapPreprocess("file_upload_help",
24  *   replace = "template_preprocess_file_upload_help"
25  * )
26  */
27 class FileUploadHelp extends PreprocessBase implements PreprocessInterface {
28
29   /**
30    * {@inheritdoc}
31    */
32   public function preprocessVariables(Variables $variables) {
33     if (!empty($variables['description'])) {
34       $variables['description'] = FieldFilteredMarkup::create($variables['description']);
35     }
36
37     $descriptions = [];
38
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.');
43       }
44       else {
45         $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
46       }
47     }
48
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])]);
52     }
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])),
56       ]);
57       $descriptions[] = t('Allowed types: @extensions.', ['@extensions' => $extensions]);
58     }
59
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]);
65       }
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]);
68       }
69       elseif ($min) {
70         $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', ['@min' => $min]);
71       }
72       elseif ($max) {
73         $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', ['@max' => $max]);
74       }
75     }
76
77     $variables['descriptions'] = $descriptions;
78
79     if ($descriptions && $this->theme->getSetting('popover_enabled')) {
80       $build = array();
81       $id = Html::getUniqueId('upload-instructions');
82       $build['toggle'] = [
83         '#type' => 'link',
84         '#title' => t('Upload requirements'),
85         '#url' => Url::fromUserInput("#$id"),
86         '#icon' => Bootstrap::glyphicon('question-sign'),
87         '#attributes' => [
88           'class' => ['icon-before'],
89           'data-toggle' => 'popover',
90           'data-html' => 'true',
91           'data-placement' => 'bottom',
92           'data-title' => t('Upload requirements'),
93         ],
94       ];
95       $build['requirements'] = [
96         '#type' => 'container',
97         '#theme_wrappers' => ['container__file_upload_help'],
98         '#attributes' => [
99           'id' => $id,
100           'class' => ['hidden', 'help-block'],
101           'aria-hidden' => 'true',
102         ],
103       ];
104       $build['requirements']['descriptions'] = [
105         '#theme' => 'item_list__file_upload_help',
106         '#items' => $descriptions,
107       ];
108       $variables['popover'] = $build;
109     }
110   }
111
112 }