diff --git a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php
index b4c338c..d80eb53 100644
--- a/core/modules/image/src/Plugin/Filter/FilterImageStyle.php
+++ b/core/modules/image/src/Plugin/Filter/FilterImageStyle.php
@@ -12,6 +12,7 @@
use Drupal\filter\Annotation\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
+use Drupal\image\ImageStyleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\RendererInterface;
@@ -87,11 +88,8 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function process($text, $langcode) {
- $search = array();
- $replace = array();
-
if (stristr($text, 'data-image-style') !== FALSE) {
- $image_styles = $this->entityTypeManager->getStorage('image_style')->loadMultiple();
+ $image_styles = $this->loadImageStyles();
$dom = HTML::load($text);
$xpath = new \DOMXPath($dom);
@@ -102,22 +100,14 @@ public function process($text, $langcode) {
$dom_node->removeAttribute('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, array_keys($image_styles))) {
+ if (empty($file_uuid) || !in_array($image_style_id, $image_styles)) {
continue;
}
- /**
- * @var \Drupal\file\FileInterface;
- */
- $file = $this->entityRepository->loadEntityByUuid('file', $file_uuid);
-
- // Determine width/height of the source image.
- $width = $height = NULL;
- $image = $this->imageFactory->get($file->getFileUri());
- if ($image->isValid()) {
- $width = $image->getWidth();
- $height = $image->getHeight();
- }
+ $image_info = $this->getImageInfo($file_uuid);
+ $image_uri = $image_info['uri'];
+ $image_width = $image_info['width'];
+ $image_height = $image_info['height'];
// Make sure all non-regenerated attributes are retained.
$dom_node->removeAttribute('width');
@@ -133,9 +123,9 @@ public function process($text, $langcode) {
$image = array(
'#theme' => 'image_style',
'#style_name' => $image_style_id,
- '#uri' => $file->getFileUri(),
- '#width' => $width,
- '#height' => $height,
+ '#uri' => $image_uri,
+ '#width' => $image_width,
+ '#height' => $image_height,
'#attributes' => $attributes,
);
$altered_html = $this->renderer->render($image);
@@ -160,12 +150,50 @@ public function process($text, $langcode) {
}
/**
+ * Loads the image styles.
+ *
+ * @return string[]
+ */
+ protected function loadImageStyles() {
+ return array_keys($this->entityTypeManager->getStorage('image_style')->loadMultiple());
+ }
+
+ /**
+ * Get the the width and height of an image based on the file UUID.
+ *
+ * @param $file_uuid
+ *
+ * @return array
+ */
+ protected function getImageInfo($file_uuid) {
+ /**
+ * @var \Drupal\file\FileInterface;
+ */
+ $file = $this->entityRepository->loadEntityByUuid('file', $file_uuid);
+
+ // Determine width/height of the source image.
+ $image_width = $image_height = NULL;
+ $image = $this->imageFactory->get($file->getFileUri());
+ if ($image->isValid()) {
+ $image_uri = $file->getFileUri();
+ $image_width = $image->getWidth();
+ $image_height = $image->getHeight();
+ }
+
+ return [
+ 'uri' => $image_uri,
+ 'width' => $image_width,
+ 'height' => $image_height
+ ];
+ }
+
+ /**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
if ($long) {
- $image_styles = $this->entityTypeManager->getStorage('image_style')->loadMultiple();
- $list = '' . implode('
, ', array_keys($image_styles)) . '
';
+ $image_styles = $this->loadImageStyles();
+ $list = '' . implode('
, ', $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.