Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\ppdx_editor\Plugin\Filter;
  4.  
  5. use Drupal\Component\Utility\Html;
  6. use Drupal\Core\Entity\EntityRepositoryInterface;
  7. use Drupal\Core\Image\ImageFactory;
  8. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  9. use Drupal\file\FileInterface;
  10. use Drupal\filter\FilterProcessResult;
  11. use Drupal\filter\Plugin\FilterBase;
  12. use Drupal\image\Entity\ImageStyle;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14.  
  15. /**
  16. * Use an image style for uploads.
  17. *
  18. * Based on \Drupal\editor\Plugin\Filter\EditorFileReference
  19. *
  20. * @Filter(
  21. * id = "ppdx_image_style",
  22. * title = @Translation("Use an image style for uploads"),
  23. * description = @Translation("Uses a hard-coded image style for uploads."),
  24. * type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
  25. * )
  26. */
  27. class EditorImageStyle extends FilterBase implements ContainerFactoryPluginInterface {
  28.  
  29. /**
  30. * The entity repository.
  31. *
  32. * @var \Drupal\Core\Entity\EntityRepositoryInterface
  33. */
  34. protected $entityRepository;
  35.  
  36. /**
  37. * The image factory.
  38. *
  39. * @var \Drupal\Core\Image\ImageFactory
  40. */
  41. protected $imageFactory;
  42.  
  43. /**
  44. * Constructs an EditorImageStyle object.
  45. *
  46. * @param array $configuration
  47. * A configuration array containing information about the plugin instance.
  48. * @param string $plugin_id
  49. * The plugin_id for the plugin instance.
  50. * @param mixed $plugin_definition
  51. * The plugin implementation definition.
  52. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
  53. * The entity repository.
  54. * @param \Drupal\Core\Image\ImageFactory $image_factory
  55. * The image factory.
  56. */
  57. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, ImageFactory $image_factory) {
  58. $this->entityRepository = $entity_repository;
  59. $this->imageFactory = $image_factory;
  60. parent::__construct($configuration, $plugin_id, $plugin_definition);
  61. }
  62.  
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  67. return new static(
  68. $configuration,
  69. $plugin_id,
  70. $plugin_definition,
  71. $container->get('entity.repository'),
  72. $container->get('image.factory')
  73. );
  74. }
  75.  
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function process($text, $langcode) {
  80. $result = new FilterProcessResult($text);
  81. // Replace "review_image" with the name of your image style.
  82. $style = ImageStyle::load('review_image');
  83. $used_style = FALSE;
  84.  
  85. if (stristr($text, 'data-entity-type="file"') !== FALSE) {
  86. $dom = Html::load($text);
  87. $xpath = new \DOMXPath($dom);
  88. foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
  89. $uuid = $node->getAttribute('data-entity-uuid');
  90.  
  91. if ($node->hasAttribute('src')) {
  92. $file = $this->entityRepository->loadEntityByUuid('file', $uuid);
  93. if ($file instanceof FileInterface) {
  94. $uri = $file->getFileUri();
  95. $image = $this->imageFactory->get($uri);
  96. if ($image->isValid() && $style->supportsUri($uri)) {
  97. $dimensions = [
  98. 'width' => $image->getWidth(),
  99. 'height' => $image->getHeight(),
  100. ];
  101. $style->transformDimensions($dimensions, $uri);
  102. $node->setAttribute('src', $style->buildUrl($uri));
  103. $node->setAttribute('width', $dimensions['width']);
  104. $node->setAttribute('height', $dimensions['height']);
  105. $used_style = TRUE;
  106. }
  107. }
  108. }
  109. }
  110. $result->setProcessedText(Html::serialize($dom));
  111. }
  112.  
  113. if ($used_style) {
  114. $result->addCacheTags($style->getCacheTags());
  115. $result->addCacheContexts($style->getCacheContexts());
  116. }
  117.  
  118. return $result;
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement