b3b7dd00b855411f825f698f429076cf1aa9bab6
[yaffs-website] / web / modules / contrib / entity_browser / entity_browser.module
1 <?php
2
3 /**
4  * @file
5  * Allows to flexibly create, browse and select entities.
6  */
7
8 use \Drupal\Core\Form\FormStateInterface;
9 use \Drupal\Core\Render\Element;
10 use Drupal\Core\Url;
11 use \Drupal\file\FileInterface;
12
13 /**
14  * Implements hook_theme().
15  *
16  * Overrides the core html theme to use a custom template for iframes.
17  */
18 function entity_browser_theme() {
19   return [
20     'html__entity_browser__iframe' => [
21       'template' => 'html--entity-browser--iframe',
22       'render element' => 'html',
23       'preprocess functions' => ['template_preprocess_html'],
24     ],
25     'html__entity_browser__modal' => [
26       'template' => 'html--entity-browser--iframe',
27       'render element' => 'html',
28       'preprocess functions' => ['template_preprocess_html'],
29     ],
30     'page__entity_browser__iframe' => [
31       'template' => 'page--entity-browser--iframe',
32       'render element' => 'html',
33       'preprocess functions' => ['template_preprocess_page'],
34     ],
35     'page__entity_browser__modal' => [
36       'template' => 'page--entity-browser--iframe',
37       'render element' => 'html',
38       'preprocess functions' => ['template_preprocess_page'],
39     ],
40   ];
41 }
42
43 /**
44  * Implements hook_form_alter().
45  */
46 function entity_browser_form_alter(&$form, FormStateInterface &$form_state) {
47   $entity_browser_dialog_edit = \Drupal::service('request_stack')->getCurrentRequest()->get('_route');
48   if ($entity_browser_dialog_edit == 'entity_browser.edit_form') {
49     // Let's allow the save button only.
50     foreach (Element::children($form['actions']) as $key) {
51       $form['actions'][$key]['#access'] = $key == 'submit';
52     }
53     // Use Ajax.
54     $form['actions']['submit']['#ajax'] = [
55       'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $form_state->getFormObject()->getEntity()->getEntityTypeId(), 'entity' => $form_state->getFormObject()->getEntity()->id()]),
56       'options' => [
57         'query' => [
58           'details_id' => \Drupal::request()->query->get('details_id'),
59         ],
60       ],
61     ];
62   }
63 }
64
65 /**
66  * Implements hook_preprocess_page__entity_browser__iframe().
67  *
68  * Tries to figure out where messages block lives and display it separately.
69  */
70 function entity_browser_preprocess_page__entity_browser__iframe(&$variables) {
71   if (!\Drupal::moduleHandler()->moduleExists('block')) {
72     return;
73   }
74   $variables['messages'] = '';
75   $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties([
76     'theme' => \Drupal::theme()->getActiveTheme()->getName(),
77     'plugin' => 'system_messages_block',
78   ]);
79
80   if (($messages = current($blocks)) && !empty($variables['page'][$messages->getRegion()][$messages->id()])) {
81     $variables['messages'] = $variables['page'][$messages->getRegion()][$messages->id()];
82   }
83 }
84
85 /**
86  * Implements hook_preprocess_page__entity_browser__modal().
87  *
88  * Tries to figure out where messages block lives and display it separately.
89  */
90 function entity_browser_preprocess_page__entity_browser__modal(&$variables) {
91   entity_browser_preprocess_page__entity_browser__iframe($variables);
92 }
93
94 /**
95  * Validates image resolution for the given File.
96  *
97  * Drupal core does not allow users to use existing images. As a result,
98  * calling the normal file_validate_image_resolution() function on a file that
99  * may be used elsewhere would resize it for all of its uses. We copy the
100  * normal validation here so that we can stop this from occurring.
101  *
102  * @param \Drupal\file\FileInterface $file
103  *   The file being evaluated.
104  * @param int $maximum_dimensions
105  *   The maximum dimensions.
106  * @param int $minimum_dimensions
107  *   The minimum dimensions.
108  *
109  * @return array
110  *   See file_validate_image_resolution()
111  */
112 function entity_browser_file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
113   $errors = array();
114
115   // Check first that the file is an image.
116   $image_factory = \Drupal::service('image.factory');
117   $image = $image_factory->get($file->getFileUri());
118   if ($image->isValid()) {
119     if ($maximum_dimensions) {
120       // Check that it is smaller than the given dimensions.
121       list($width, $height) = explode('x', $maximum_dimensions);
122       if ($image->getWidth() > $width || $image->getHeight() > $height) {
123         // Try to resize the image to fit the dimensions.
124         // This $file->isPermanent() check is the only part of the function
125         // body that is significantly different.
126         if (!$file->isPermanent() && $image->scale($width, $height)) {
127           $image->save();
128           $file->filesize = $image->getFileSize();
129           drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
130         }
131         else {
132           $errors[] = t('The image exceeds the maximum allowed dimensions.');
133         }
134       }
135     }
136
137     if ($minimum_dimensions) {
138       // Check that it is larger than the given dimensions.
139       list($width, $height) = explode('x', $minimum_dimensions);
140       if ($image->getWidth() < $width || $image->getHeight() < $height) {
141         $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
142       }
143     }
144   }
145
146   return $errors;
147 }