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.

', array('!image-style-machine-name-list' => $list)); } diff --git a/core/modules/image/src/Tests/FilterImageStyleTest.php b/core/modules/image/src/Tests/FilterImageStyleTest.php deleted file mode 100644 index bc95f33..0000000 --- a/core/modules/image/src/Tests/FilterImageStyleTest.php +++ /dev/null @@ -1,97 +0,0 @@ - 'filtered_text', - 'name' => 'Filtered Text', - 'filters' => array( - 'filter_imagestyle' => array( - 'status' => 1, - ), - ), - )); - $filtered_text_format->save(); - - // Setup users. - $this->webUser = $this->drupalCreateUser(array( - 'access content', - 'access comments', - 'post comments', - 'skip comment approval', - $filtered_text_format->getPermissionName(), - )); - $this->drupalLogin($this->webUser); - - // Setup a node to comment and test on. - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - // Add a comment field. - $this->addDefaultCommentField('node', 'page'); - $this->node = $this->drupalCreateNode(); - } - - /** - * Test the image style filter. - */ - function testImageStyleFilter() { - // Create a url for a test image. - $files = $this->drupalGetTestFiles('image'); - $file = reset($files); - $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); - $original_url = file_create_url($original_uri); - - // Create an image element with a data-image-style attribute. - $input = ''; - - // Create a comment on a page with an image inside. - $comment = $input; - $edit = array( - 'comment_body[0][value]' => $comment, - ); - $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Save')); - - // Create a url for a test image processed by the medium image style. - $generated_uri = 'public://styles/medium/public/' . \Drupal::service('file_system')->basename($original_uri); - $generated_url = file_create_url($generated_uri); - - // Look for converted img element. - $found = FALSE; - if ($element = $this->xpath('//img[@src="' . $generated_url . '"]')) { - $found = TRUE; - } - - $this->assertTrue($found, format_string('@image was found with medium image style.', array('@image' => $original_url))); - } -} diff --git a/core/modules/image/tests/src/Unit/FilterImageStyleTest.php b/core/modules/image/tests/src/Unit/FilterImageStyleTest.php index c729ee3..810c5e9 100644 --- a/core/modules/image/tests/src/Unit/FilterImageStyleTest.php +++ b/core/modules/image/tests/src/Unit/FilterImageStyleTest.php @@ -44,7 +44,20 @@ protected function setUp() { $this->imageFactory = $this->prophesize(ImageFactory::class); $this->renderer = $this->prophesize(Renderer::class); - $this->filterImageStyle = new FilterImageStyle($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager->reveal(), $this->entityRepository->reveal(), $this->imageFactory->reveal(), $this->renderer->reveal()); + $this->filterImageStyle = $this->getMockBuilder('Drupal\image\Plugin\Filter\FilterImageStyle') + ->setConstructorArgs([ + $configuration, + $plugin_id, + $plugin_definition, + $this->entityTypeManager->reveal(), + $this->entityRepository->reveal(), + $this->imageFactory->reveal(),$this->renderer->reveal() + ]) + ->setMethods([ + 'loadImageStyles', + 'getImageInfo' + ]) + ->getMock(); } public function testProcessWithoutImage() { @@ -53,9 +66,9 @@ public function testProcessWithoutImage() { } public function testProcessWithImage() { - $image_style_storage = $this->prophesize(EntityStorageInterface::class); - $this->entityTypeManager->getStorage('image_style')->willReturn($image_style_storage->reveal()); - $output = $this->filterImageStyle->process('', 'en'); + $this->filterImageStyle->method('loadImageStyles')->willReturn(['thumbnail', 'medium', 'large']); + $this->filterImageStyle->method('getImageInfo')->with($this->equalTo('abcd-1234-ghij-5678'))->willReturn(['uri' => 'styles/medium/public/image.png', 'width' => '200', 'height'=> '150']); + $output = $this->filterImageStyle->process('', 'en'); $this->assertEquals('', $output); } }