file UploadHelp.php

Namespace

Drupal\bootstrap\Plugin\Preprocess
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Preprocess;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Utility\Variables;
  5. use Drupal\Component\Render\FormattableMarkup;
  6. use Drupal\Component\Utility\Html;
  7. use Drupal\Core\Field\FieldFilteredMarkup;
  8. use Drupal\Core\Url;
  9. /**
  10. * Pre-processes variables for the "file_upload_help" theme hook.
  11. *
  12. * @ingroup plugins_preprocess
  13. *
  14. * @BootstrapPreprocess("file_upload_help",
  15. * replace = "template_preprocess_file_upload_help"
  16. * )
  17. */
  18. class FileUploadHelp extends PreprocessBase implements PreprocessInterface {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function preprocessVariables(Variables $variables) {
  23. if (!empty($variables['description'])) {
  24. $variables['description'] = FieldFilteredMarkup::create($variables['description']);
  25. }
  26. $descriptions = [];
  27. $cardinality = $variables['cardinality'];
  28. if (isset($cardinality)) {
  29. if ($cardinality == -1) {
  30. $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
  31. }
  32. else {
  33. $descriptions[] = \Drupal::translation()->formatPlural($cardinality, 'One file only.', 'Maximum @count files.');
  34. }
  35. }
  36. $upload_validators = $variables['upload_validators'];
  37. if (isset($upload_validators['file_validate_size'])) {
  38. $descriptions[] = t('@size limit.', ['@size' => format_size($upload_validators['file_validate_size'][0])]);
  39. }
  40. if (isset($upload_validators['file_validate_extensions'])) {
  41. $extensions = new FormattableMarkup('<code>@extensions</code>', [
  42. '@extensions' => implode(', ', explode(' ', $upload_validators['file_validate_extensions'][0])),
  43. ]);
  44. $descriptions[] = t('Allowed types: @extensions.', ['@extensions' => $extensions]);
  45. }
  46. if (isset($upload_validators['file_validate_image_resolution'])) {
  47. $max = $upload_validators['file_validate_image_resolution'][0];
  48. $min = $upload_validators['file_validate_image_resolution'][1];
  49. if ($min && $max && $min == $max) {
  50. $descriptions[] = t('Images must be exactly <strong>@size</strong> pixels.', ['@size' => $max]);
  51. }
  52. elseif ($min && $max) {
  53. $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]);
  54. }
  55. elseif ($min) {
  56. $descriptions[] = t('Images must be larger than <strong>@min</strong> pixels.', ['@min' => $min]);
  57. }
  58. elseif ($max) {
  59. $descriptions[] = t('Images larger than <strong>@max</strong> pixels will be resized.', ['@max' => $max]);
  60. }
  61. }
  62. $variables['descriptions'] = $descriptions;
  63. if ($descriptions && $this->theme->getSetting('popover_enabled')) {
  64. $build = [];
  65. $id = Html::getUniqueId('upload-instructions');
  66. $build['toggle'] = [
  67. '#type' => 'link',
  68. '#title' => t('Upload requirements'),
  69. '#url' => Url::fromUserInput("#$id"),
  70. '#icon' => Bootstrap::glyphicon('question-sign'),
  71. '#attributes' => [
  72. 'class' => ['icon-before'],
  73. 'data-toggle' => 'popover',
  74. 'data-html' => 'true',
  75. 'data-placement' => 'bottom',
  76. 'data-title' => t('Upload requirements'),
  77. ],
  78. ];
  79. $build['requirements'] = [
  80. '#type' => 'container',
  81. '#theme_wrappers' => ['container__file_upload_help'],
  82. '#attributes' => [
  83. 'id' => $id,
  84. 'class' => ['hidden', 'help-block'],
  85. 'aria-hidden' => 'true',
  86. ],
  87. ];
  88. $build['requirements']['descriptions'] = [
  89. '#theme' => 'item_list__file_upload_help',
  90. '#items' => $descriptions,
  91. ];
  92. $variables['popover'] = $build;
  93. }
  94. }
  95. }

Classes

Name Description
FileUploadHelp Pre-processes variables for the "file_upload_help" theme hook.