5 * Allows to flexibly create, browse and select entities.
8 use \Drupal\Core\Form\FormStateInterface;
9 use \Drupal\Core\Render\Element;
11 use \Drupal\file\FileInterface;
14 * Implements hook_theme().
16 * Overrides the core html theme to use a custom template for iframes.
18 function entity_browser_theme() {
20 'html__entity_browser__iframe' => [
21 'template' => 'html--entity-browser--iframe',
22 'render element' => 'html',
23 'preprocess functions' => ['template_preprocess_html'],
25 'html__entity_browser__modal' => [
26 'template' => 'html--entity-browser--iframe',
27 'render element' => 'html',
28 'preprocess functions' => ['template_preprocess_html'],
30 'page__entity_browser__iframe' => [
31 'template' => 'page--entity-browser--iframe',
32 'render element' => 'html',
33 'preprocess functions' => ['template_preprocess_page'],
35 'page__entity_browser__modal' => [
36 'template' => 'page--entity-browser--iframe',
37 'render element' => 'html',
38 'preprocess functions' => ['template_preprocess_page'],
44 * Implements hook_form_alter().
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';
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()]),
58 'details_id' => \Drupal::request()->query->get('details_id'),
66 * Implements hook_preprocess_page__entity_browser__iframe().
68 * Tries to figure out where messages block lives and display it separately.
70 function entity_browser_preprocess_page__entity_browser__iframe(&$variables) {
71 if (!\Drupal::moduleHandler()->moduleExists('block')) {
74 $variables['messages'] = '';
75 $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties([
76 'theme' => \Drupal::theme()->getActiveTheme()->getName(),
77 'plugin' => 'system_messages_block',
80 if (($messages = current($blocks)) && !empty($variables['page'][$messages->getRegion()][$messages->id()])) {
81 $variables['messages'] = $variables['page'][$messages->getRegion()][$messages->id()];
86 * Implements hook_preprocess_page__entity_browser__modal().
88 * Tries to figure out where messages block lives and display it separately.
90 function entity_browser_preprocess_page__entity_browser__modal(&$variables) {
91 entity_browser_preprocess_page__entity_browser__iframe($variables);
95 * Validates image resolution for the given File.
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.
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.
110 * See file_validate_image_resolution()
112 function entity_browser_file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
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)) {
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)));
132 $errors[] = t('The image exceeds the maximum allowed dimensions.');
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));