Verified Commit 58ecf6d4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420996 by mstrelan, kim.pepper, larowlan: Convert ImageEffect plugin...

Issue #3420996 by mstrelan, kim.pepper, larowlan: Convert ImageEffect plugin discovery to attributes
parent 1366a3ae
Loading
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\image\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines an ImageEffect attribute for plugin discovery.
 *
 * Plugin Namespace: Plugin\ImageEffect
 *
 * For a working example, see
 * \Drupal\image\Plugin\ImageEffect\ResizeImageEffect
 *
 * @see hook_image_effect_info_alter()
 * @see \Drupal\image\ConfigurableImageEffectInterface
 * @see \Drupal\image\ConfigurableImageEffectBase
 * @see \Drupal\image\ImageEffectInterface
 * @see \Drupal\image\ImageEffectBase
 * @see \Drupal\image\ImageEffectManager
 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class ImageEffect extends Plugin {

  /**
   * Constructs an ImageEffect attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable name of the image effect.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) A brief description of the image effect. This will be shown
   *   when adding or configuring this image effect.
   * @param class-string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly ?string $deriver = NULL,
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\image\Attribute\ImageEffect;

/**
 * Manages image effect plugins.
@@ -31,7 +32,7 @@ class ImageEffectManager extends DefaultPluginManager {
   *   The module handler.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/ImageEffect', $namespaces, $module_handler, 'Drupal\image\ImageEffectInterface', 'Drupal\image\Annotation\ImageEffect');
    parent::__construct('Plugin/ImageEffect', $namespaces, $module_handler, 'Drupal\image\ImageEffectInterface', ImageEffect::class, 'Drupal\image\Annotation\ImageEffect');

    $this->alterInfo('image_effect_info');
    $this->setCacheBackend($cache_backend, 'image_effect_plugins');
+7 −6
Original line number Diff line number Diff line
@@ -4,17 +4,18 @@

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Attribute\ImageEffect;
use Drupal\image\ConfigurableImageEffectBase;

/**
 * Converts an image resource.
 *
 * @ImageEffect(
 *   id = "image_convert",
 *   label = @Translation("Convert"),
 *   description = @Translation("Converts an image to a format (such as JPEG).")
 * )
 */
#[ImageEffect(
  id: "image_convert",
  label: new TranslatableMarkup("Convert"),
  description: new TranslatableMarkup("Converts an image to a format (such as JPEG)."),
)]
class ConvertImageEffect extends ConfigurableImageEffectBase {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -4,16 +4,17 @@

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Attribute\ImageEffect;

/**
 * Crops an image resource.
 *
 * @ImageEffect(
 *   id = "image_crop",
 *   label = @Translation("Crop"),
 *   description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
 * )
 */
#[ImageEffect(
  id: "image_crop",
  label: new TranslatableMarkup("Crop"),
  description: new TranslatableMarkup("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately."),
)]
class CropImageEffect extends ResizeImageEffect {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -3,17 +3,18 @@
namespace Drupal\image\Plugin\ImageEffect;

use Drupal\Core\Image\ImageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Attribute\ImageEffect;
use Drupal\image\ImageEffectBase;

/**
 * Desaturates (grayscale) an image resource.
 *
 * @ImageEffect(
 *   id = "image_desaturate",
 *   label = @Translation("Desaturate"),
 *   description = @Translation("Desaturate converts an image to grayscale.")
 * )
 */
#[ImageEffect(
  id: "image_desaturate",
  label: new TranslatableMarkup("Desaturate"),
  description: new TranslatableMarkup("Desaturate converts an image to grayscale."),
)]
class DesaturateImageEffect extends ImageEffectBase {

  /**
Loading