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