Skip to content

Instantly share code, notes, and snippets.

@mortenson
Last active June 26, 2019 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mortenson/7942e09cdc23a4637a729d746ca558c7 to your computer and use it in GitHub Desktop.
Save mortenson/7942e09cdc23a4637a729d746ca558c7 to your computer and use it in GitHub Desktop.
A custom Drupal 8 filter plugin to use an image style for image embedss custom Drupal 8 filter plugin to use an image style for image embeds. A custom Drupal 8 filter plugin to use an image style for image embeds.
<?php
namespace Drupal\ppdx_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;
use Drupal\filter\Plugin\FilterBase;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Use an image style for uploads.
*
* Based on \Drupal\editor\Plugin\Filter\EditorFileReference
*
* @Filter(
* id = "ppdx_image_style",
* title = @Translation("Use an image style for uploads"),
* description = @Translation("Uses a hard-coded image style for uploads."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
* )
*/
class EditorImageStyle extends FilterBase implements ContainerFactoryPluginInterface {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* Constructs an EditorImageStyle object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* 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, ImageFactory $image_factory) {
$this->entityRepository = $entity_repository;
$this->imageFactory = $image_factory;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.repository'),
$container->get('image.factory')
);
}
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
// Replace "review_image" with the name of your image style.
$style = ImageStyle::load('review_image');
$used_style = FALSE;
if (stristr($text, 'data-entity-type="file"') !== FALSE) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
$uuid = $node->getAttribute('data-entity-uuid');
if ($node->hasAttribute('src')) {
$file = $this->entityRepository->loadEntityByUuid('file', $uuid);
if ($file instanceof FileInterface) {
$uri = $file->getFileUri();
$image = $this->imageFactory->get($uri);
if ($image->isValid() && $style->supportsUri($uri)) {
$dimensions = [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
$style->transformDimensions($dimensions, $uri);
$node->setAttribute('src', $style->buildUrl($uri));
$node->setAttribute('width', $dimensions['width']);
$node->setAttribute('height', $dimensions['height']);
$used_style = TRUE;
}
}
}
}
$result->setProcessedText(Html::serialize($dom));
}
if ($used_style) {
$result->addCacheTags($style->getCacheTags());
$result->addCacheContexts($style->getCacheContexts());
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment