Drupal investigation

ImageEffectBase.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Drupal\image;
  3. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  4. use Drupal\Core\Plugin\PluginBase;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. /**
  8. * Provides a base class for image effects.
  9. *
  10. * @see \Drupal\image\Annotation\ImageEffect
  11. * @see \Drupal\image\ImageEffectInterface
  12. * @see \Drupal\image\ConfigurableImageEffectInterface
  13. * @see \Drupal\image\ConfigurableImageEffectBase
  14. * @see \Drupal\image\ImageEffectManager
  15. * @see plugin_api
  16. */
  17. abstract class ImageEffectBase extends PluginBase implements ImageEffectInterface, ContainerFactoryPluginInterface {
  18. /**
  19. * The image effect ID.
  20. *
  21. * @var string
  22. */
  23. protected $uuid;
  24. /**
  25. * The weight of the image effect.
  26. *
  27. * @var int|string
  28. */
  29. protected $weight = '';
  30. /**
  31. * A logger instance.
  32. *
  33. * @var \Psr\Log\LoggerInterface
  34. */
  35. protected $logger;
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
  40. parent::__construct($configuration, $plugin_id, $plugin_definition);
  41. $this->setConfiguration($configuration);
  42. $this->logger = $logger;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  48. return new static(
  49. $configuration,
  50. $plugin_id,
  51. $plugin_definition,
  52. $container->get('logger.factory')->get('image')
  53. );
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function transformDimensions(array &$dimensions, $uri) {
  59. // Most image effects will not change the dimensions. This base
  60. // implementation represents this behavior. Override this method if your
  61. // image effect does change the dimensions.
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getDerivativeExtension($extension) {
  67. // Most image effects will not change the extension. This base
  68. // implementation represents this behavior. Override this method if your
  69. // image effect does change the extension.
  70. return $extension;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getSummary() {
  76. return [
  77. '#markup' => '',
  78. '#effect' => [
  79. 'id' => $this->pluginDefinition['id'],
  80. 'label' => $this->label(),
  81. 'description' => $this->pluginDefinition['description'],
  82. ],
  83. ];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function label() {
  89. return $this->pluginDefinition['label'];
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getUuid() {
  95. return $this->uuid;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function setWeight($weight) {
  101. $this->weight = $weight;
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getWeight() {
  108. return $this->weight;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getConfiguration() {
  114. return [
  115. 'uuid' => $this->getUuid(),
  116. 'id' => $this->getPluginId(),
  117. 'weight' => $this->getWeight(),
  118. 'data' => $this->configuration,
  119. ];
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function setConfiguration(array $configuration) {
  125. $configuration += [
  126. 'data' => [],
  127. 'uuid' => '',
  128. 'weight' => '',
  129. ];
  130. $this->configuration = $configuration['data'] + $this->defaultConfiguration();
  131. $this->uuid = $configuration['uuid'];
  132. $this->weight = $configuration['weight'];
  133. return $this;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function defaultConfiguration() {
  139. return [];
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function calculateDependencies() {
  145. return [];
  146. }
  147. }