diff --git a/core/modules/image/image.module b/core/modules/image/image.module index a6d75bf350..53b15e03d2 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -8,13 +8,11 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; use Drupal\field\FieldStorageConfigInterface; use Drupal\file\Entity\File; use Drupal\file\FileInterface; use Drupal\image\Entity\ImageStyle; -use Drupal\image\ImageStyleInterface; /** * Image style constant for user presets in the database. @@ -539,32 +537,22 @@ function image_form_editor_image_dialog_alter(array &$form, FormStateInterface $ // Get the image () that is being edited on the client. $image_element = $form_state->get('image_element'); - // Get an array of image styles to present as options for selection. - $image_style_options = array_map(function (ImageStyleInterface $image_style) { - return $image_style->label(); - }, $filter->getAllowedImageStyles()); - // Add a select element to choose an image style. $form['image_style']['selection'] = [ '#title' => t('Use image style'), '#type' => 'select', - '#default_value' => isset($image_element['data-image-style']) ? $image_element['data-image-style'] : NULL, - '#options' => $image_style_options, + '#default_value' => isset($image_element['data-image-style']) ? $image_element['data-image-style'] : '', + '#options' => $filter->getAllowedImageStyleOptions(), '#empty_value' => '', '#parents' => ['attributes', 'data-image-style'], ]; // Check the access to link for configuring allowed image styles. - $route_parameters = ['filter_format' => $editor->getFilterFormat()->id()]; - $current_user = \Drupal::currentUser(); - $access_administer_styles = $current_user->hasPermission('administer image styles'); - $access_filter_form = \Drupal::service('access_manager')->checkNamedRoute('entity.filter_format.edit_form', $route_parameters, $current_user); - - if ($access_administer_styles && $access_filter_form) { + $url = $editor->getFilterFormat()->toUrl('edit-form'); + if ($url->access()) { // Add a link to the configuration of the image styles. - $url = Url::fromRoute('entity.filter_format.edit_form', $route_parameters); $url->setOptions(['fragment' => 'edit-filters-filter-image-style-settings']); - $form['image_style']['link'] = [ + $form['image_style']['configure_link'] = [ '#title' => t('Configure allowed image styles'), '#type' => 'link', '#url' => $url, @@ -597,10 +585,7 @@ function image_form_editor_image_dialog_validate(array &$form, FormStateInterfac return; } - /** @var \Drupal\file\FileInterface $file */ - $file = File::load($form_state->getValue('fid')[0]); - - $uri = $file->getFileUri(); + $uri = File::load($form_state->getValue('fid')[0])->getFileUri(); // Set the 'src' attribute to the image style URL. FilterImageStyle will // look at the 'data-editor-file-uuid' attribute, not the 'src' attribute diff --git a/core/modules/image/image.post_update.php b/core/modules/image/image.post_update.php index e4edfbdbcc..1c1cf91312 100644 --- a/core/modules/image/image.post_update.php +++ b/core/modules/image/image.post_update.php @@ -57,7 +57,7 @@ function image_post_update_enable_filter_image_style() { $format->setFilterConfig('filter_image_style', ['status' => TRUE, 'weight' => $highest_weight + 1]); $changed = TRUE; } - // Update the allowed html tags of filter_html filter if it's enabled. + // Update the allowed HTML tags of filter_html filter if it's enabled. if ($filter = $format->filters('filter_html')) { $config = $filter->getConfiguration(); $allowed_html = !empty($config['settings']['allowed_html']) ? $config['settings']['allowed_html'] : NULL; diff --git a/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php b/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php index ac92e8dfd6..6ed096360f 100644 --- a/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php +++ b/core/modules/image/src/Plugin/CKEditorPlugin/DrupalImageStyle.php @@ -22,7 +22,7 @@ class DrupalImageStyle extends CKEditorPluginBase implements CKEditorPluginConte * {@inheritdoc} */ public function getFile() { - return drupal_get_path('module', 'image') . '/js/plugins/drupalimagestyle/plugin.js'; + return 'core/modules/image/js/plugins/drupalimagestyle/plugin.js'; } /** diff --git a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php index 8bfbdf6710..603cb2bd31 100644 --- a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php +++ b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php @@ -106,90 +106,73 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function process($text, $langcode) { - if (stristr($text, 'data-image-style') !== FALSE) { - // Load all image styles so each image found in the text can be checked - // against a valid image style. - $image_styles = $this->getAllowedImageStyleIds(); - - $dom = Html::load($text); - $xpath = new \DOMXPath($dom); - - // Process each image element found with the necessary attributes. - /** @var \DOMElement $dom_element */ - foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid and @data-image-style]') as $dom_element) { - // Get the UUID and image style for the file. - $file_uuid = $dom_element->getAttribute('data-entity-uuid'); - $image_style_id = $dom_element->getAttribute('data-image-style'); - - // If the image style is not a valid one, then don't transform the HTML. - if (empty($file_uuid) || !in_array($image_style_id, $image_styles)) { - continue; - } - if (!$this->entityRepository->loadEntityByUuid('file', $file_uuid)) { - continue; - } - - // Transform the HTML for the img element by applying an image style. - $altered_img_markup = $this->getImageStyleHtml($file_uuid, $image_style_id, $dom_element); - $altered_img = $dom->createDocumentFragment(); - $altered_img->appendXML($altered_img_markup); - $dom_element->parentNode->replaceChild($altered_img, $dom_element); + // Process the filter if no image style img elements are found. + if (stristr($text, 'data-image-style') === FALSE) { + return new FilterProcessResult($text); + } + // Load all image styles so each image found in the text can be checked + // against a valid image style. + $image_styles = array_keys($this->getAllowedImageStyles()); + + $dom = Html::load($text); + $xpath = new \DOMXPath($dom); + + // Process each image element found with the necessary attributes. + /** @var \DOMElement $dom_element */ + foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid and @data-image-style]') as $dom_element) { + // Get the UUID and image style for the file. + $file_uuid = $dom_element->getAttribute('data-entity-uuid'); + $image_style_id = $dom_element->getAttribute('data-image-style'); + + // If the image style is not a valid one, then don't transform the HTML. + if (empty($file_uuid) || !in_array($image_style_id, $image_styles, TRUE)) { + continue; + } + if (!$this->entityRepository->loadEntityByUuid('file', $file_uuid)) { + continue; } - return new FilterProcessResult(Html::serialize($dom)); + // Transform the HTML for the img element by applying an image style. + $altered_img_markup = $this->getImageStyleHtml($file_uuid, $image_style_id, $dom_element); + $altered_img = $dom->createDocumentFragment(); + $altered_img->appendXML($altered_img_markup); + $dom_element->parentNode->replaceChild($altered_img, $dom_element); } - // Process the filter if no image style img elements are found. - return new FilterProcessResult($text); + return new FilterProcessResult(Html::serialize($dom)); } /** - * Loads image styles. - * - * @param array|null $ids - * Optional array of image style IDs to load. If omitted, all image styles - * will be returned. + * Returns the allowed image styles. * * @return \Drupal\image\ImageStyleInterface[] - * The image styles. + * The allowed image styles. */ - protected function loadImageStyles(array $ids = NULL) { + public function getAllowedImageStyles() { + $ids = !empty($this->settings['allowed_styles']) ? $this->settings['allowed_styles'] : NULL; return $this->entityTypeManager->getStorage('image_style')->loadMultiple($ids); } /** - * Returns the machine names of the allowed image styles. - * - * @return string[] - * The machine names of the allowed image styles. - */ - protected function getAllowedImageStyleIds() { - if (!empty($this->settings['allowed_styles'])) { - return $this->settings['allowed_styles']; - } - // If no image styles are selected, then all are allowed. - return $this->entityTypeManager->getStorage('image_style')->getQuery()->execute(); - } - - /** - * Returns the allowed image styles. + * Get an array of image styles to present as options for selection. * - * @return \Drupal\image\ImageStyleInterface[] - * The allowed image styles. + * @return array + * The image style labels. */ - public function getAllowedImageStyles() { - $ids = !empty($this->settings['allowed_styles']) ? $this->settings['allowed_styles'] : NULL; - return $this->loadImageStyles($ids); + public function getAllowedImageStyleOptions() { + return array_map(function (ImageStyleInterface $image_style) { + return $image_style->label(); + }, $this->getAllowedImageStyles()); } /** - * Gets the width and height of an image based on the file UUID. + * Gets the URI, width and height of an image based on the file UUID. * * @param string $file_uuid * The UUID for the file. * * @return array - * The image information. + * The image information as an array with keys of #uri, #width and #height. */ protected function getImageInfo($file_uuid) { $image_uri = $image_width = $image_height = NULL; @@ -229,6 +212,8 @@ protected function prepareImageAttributes(\DOMElement $dom_element) { $dom_element->removeAttribute('src'); // Make sure all non-regenerated attributes are retained. + // TODO: make into a utility method, see + // https://www.drupal.org/project/drupal/issues/3000715 $attributes = []; for ($i = 0; $i < $dom_element->attributes->length; $i++) { $attr = $dom_element->attributes->item($i); @@ -273,7 +258,7 @@ protected function getImageStyleHtml($file_uuid, $image_style_id, \DOMElement $d * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $image_styles = $this->loadImageStyles(); + $image_styles = $this->entityTypeManager->getStorage('image_style')->loadMultiple(); $options = array_map(function (ImageStyleInterface $image_style) { return $image_style->label(); }, $image_styles); @@ -296,7 +281,9 @@ public function settingsForm(array $form, FormStateInterface $form_state) { */ public function tips($long = FALSE) { if ($long) { - $image_styles = $this->getAllowedImageStyleIds(); + $image_styles = array_map(function (ImageStyleInterface $image_style) { + return $image_style->label(); + }, $this->getAllowedImageStyles()); $image_styles = implode(', ', $image_styles); $list = new TranslatableMarkup("$image_styles"); return t('

You can display images using site-wide styles by adding a data-image-style attribute, whose values is one of the image style machine names: @image-style-machine-name-list.

', ['@image-style-machine-name-list' => $list]);