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