Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement