Issue 3167034: use image factory

merge-requests/25/head
Edys Meza 2020-09-24 16:50:31 -06:00
parent 36da7865ae
commit d051924051
1 changed files with 17 additions and 3 deletions

View File

@ -4,6 +4,7 @@ namespace Drupal\editor\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileInterface;
use Drupal\filter\FilterProcessResult;
@ -31,6 +32,13 @@ class EditorFileReference extends FilterBase implements ContainerFactoryPluginIn
*/
protected $entityRepository;
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* Constructs a \Drupal\editor\Plugin\Filter\EditorFileReference object.
*
@ -42,9 +50,12 @@ class EditorFileReference extends FilterBase implements ContainerFactoryPluginIn
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, ImageFactory $image_factory) {
$this->entityRepository = $entity_repository;
$this->imageFactory = $image_factory;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
@ -56,7 +67,8 @@ class EditorFileReference extends FilterBase implements ContainerFactoryPluginIn
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.repository')
$container->get('entity.repository'),
$container->get('image.factory')
);
}
@ -83,7 +95,9 @@ class EditorFileReference extends FilterBase implements ContainerFactoryPluginIn
// Without dimensions specified, layout shifts can occur,
// which are more noticeable on pages that take some time to load.
// As a result, only mark images as lazy load that have dimensions.
[$width, $height] = @getimagesize($file->getFileUri());
$image = $this->imageFactory->get($file->getFileUri());
$width = $image->getWidth();
$height = $image->getHeight();
if ($width !== NULL && $height !== NULL) {
if (!$node->hasAttribute('width')) {
$node->setAttribute('width', $width);