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;
12 use Drupal\Core\Routing\RouteMatchInterface;
15 * Implements hook_help().
17 function entity_browser_help($route_name, RouteMatchInterface $arg) {
18 switch ($route_name) {
19 case 'help.page.entity_browser':
21 $output .= '<h3>' . t('About') . '</h3>';
22 $output .= '<p>' . t('The Entity Browser module provides a generic entity browser/picker/selector. It can be used in any context where one needs to select a few entities and do something with them. For more information, see the online documentation for <a href=":entity_browser-documentation">Entity Browser</a>.', [':entity_browser-documentation' => 'https://drupal-media.gitbooks.io/drupal8-guide/content/modules/entity_browser/intro.html']) . '</p>';
23 $output .= '<h3>' . t('Uses') . '</h3>';
25 $output .= '<dt>' . t('General') . '</dt>';
26 $output .= '<dd>' . t('Entity browser comes with an example module that can be used as a starting point.') . '</dd>';
27 $output .= '<dt>' . t('Example use cases') . '</dt>';
28 $output .= '<dd>' . t('Powerfull entity reference widget') . '</dd>';
29 $output .= '<dd>' . t('Embedding entities into wysiwyg') . '</dd>';
37 * Implements hook_theme().
39 * Overrides the core html theme to use a custom template for iframes.
41 function entity_browser_theme() {
43 'html__entity_browser__iframe' => [
44 'template' => 'html--entity-browser--iframe',
45 'render element' => 'html',
46 'preprocess functions' => ['template_preprocess_html'],
48 'html__entity_browser__modal' => [
49 'template' => 'html--entity-browser--iframe',
50 'render element' => 'html',
51 'preprocess functions' => ['template_preprocess_html'],
53 'page__entity_browser__iframe' => [
54 'template' => 'page--entity-browser--iframe',
55 'render element' => 'html',
56 'preprocess functions' => ['template_preprocess_page'],
58 'page__entity_browser__modal' => [
59 'template' => 'page--entity-browser--iframe',
60 'render element' => 'html',
61 'preprocess functions' => ['template_preprocess_page'],
67 * Implements hook_form_alter().
69 function entity_browser_form_alter(&$form, FormStateInterface &$form_state) {
70 $entity_browser_dialog_edit = \Drupal::service('request_stack')->getCurrentRequest()->get('_route');
71 if ($entity_browser_dialog_edit == 'entity_browser.edit_form') {
72 // Let's allow the save button only.
73 foreach (Element::children($form['actions']) as $key) {
74 $form['actions'][$key]['#access'] = $key == 'submit';
77 $form['actions']['submit']['#ajax'] = [
78 'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $form_state->getFormObject()->getEntity()->getEntityTypeId(), 'entity' => $form_state->getFormObject()->getEntity()->id()]),
81 'details_id' => \Drupal::request()->query->get('details_id'),
89 * Implements hook_preprocess_page__entity_browser__iframe().
91 * Tries to figure out where messages block lives and display it separately.
93 function entity_browser_preprocess_page__entity_browser__iframe(&$variables) {
94 if (!\Drupal::moduleHandler()->moduleExists('block')) {
97 $variables['messages'] = '';
98 $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties([
99 'theme' => \Drupal::theme()->getActiveTheme()->getName(),
100 'plugin' => 'system_messages_block',
103 if (($messages = current($blocks)) && !empty($variables['page'][$messages->getRegion()][$messages->id()])) {
104 $variables['messages'] = $variables['page'][$messages->getRegion()][$messages->id()];
109 * Implements hook_preprocess_page__entity_browser__modal().
111 * Tries to figure out where messages block lives and display it separately.
113 function entity_browser_preprocess_page__entity_browser__modal(&$variables) {
114 entity_browser_preprocess_page__entity_browser__iframe($variables);
118 * Validates image resolution for the given File.
120 * Drupal core does not allow users to use existing images. As a result,
121 * calling the normal file_validate_image_resolution() function on a file that
122 * may be used elsewhere would resize it for all of its uses. We copy the
123 * normal validation here so that we can stop this from occurring.
125 * @param \Drupal\file\FileInterface $file
126 * The file being evaluated.
127 * @param int $maximum_dimensions
128 * The maximum dimensions.
129 * @param int $minimum_dimensions
130 * The minimum dimensions.
133 * See file_validate_image_resolution()
135 function entity_browser_file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
138 // Check first that the file is an image.
139 $image_factory = \Drupal::service('image.factory');
140 $image = $image_factory->get($file->getFileUri());
141 if ($image->isValid()) {
142 if ($maximum_dimensions) {
143 // Check that it is smaller than the given dimensions.
144 list($width, $height) = explode('x', $maximum_dimensions);
145 if ($image->getWidth() > $width || $image->getHeight() > $height) {
146 // Try to resize the image to fit the dimensions.
147 // This $file->isPermanent() check is the only part of the function
148 // body that is significantly different.
149 if (!$file->isPermanent() && $image->scale($width, $height)) {
151 $file->filesize = $image->getFileSize();
152 drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $maximum_dimensions]));
155 $errors[] = t('The image exceeds the maximum allowed dimensions.');
160 if ($minimum_dimensions) {
161 // Check that it is larger than the given dimensions.
162 list($width, $height) = explode('x', $minimum_dimensions);
163 if ($image->getWidth() < $width || $image->getHeight() < $height) {
164 $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => $minimum_dimensions]);