diff --git a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php index 3fae10a..30cb4ad 100644 --- a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -21,6 +21,12 @@ class ImageToolkitOperation extends Plugin { /** * The plugin ID. * + * For discovery to work, the id must be composed as {toolkit}.{operation}. + * + * If that will lead to an already existing id, you are apparently overruling + * an existing operation. In that case make up a unique id and use alterInfo() + * to swap your plugin into the place of the existing one. + * * @var string */ public $id; @@ -61,12 +67,4 @@ class ImageToolkitOperation extends Plugin { */ public $description; - /** - * The weight of the image toolkit operation plugin, used to prioritize - * plugins that implement the same operation for the same toolkit. - * - * @var number - */ - public $weight; - } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php index 4105188..e5e6592 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php @@ -20,8 +20,7 @@ * toolkit = "gd", * operation = "crop", * label = @Translation("Crop"), - * description = @Translation("GD2 toolkit crop"), - * weight = 0 + * description = @Translation("GD2 toolkit crop") * ) */ class GDCrop extends PluginBase implements ImageToolkitOperationInterface { @@ -44,7 +43,7 @@ class GDCrop extends PluginBase implements ImageToolkitOperationInterface { * - height: The height of the cropped area, in pixels. * * @return bool - * TRUE or FALSE, based on success. + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { $res = $toolkit->createTmp($image, $data['width'], $data['height']); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php index 6802d60..b4c7c50 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php @@ -20,8 +20,7 @@ * toolkit = "gd", * operation = "desaturate", * label = @Translation("Desaturate"), - * description = @Translation("GD2 toolkit desaturate"), - * weight = 0 + * description = @Translation("GD2 toolkit desaturate") * ) */ class GDDesaturate extends PluginBase implements ImageToolkitOperationInterface { @@ -40,7 +39,7 @@ class GDDesaturate extends PluginBase implements ImageToolkitOperationInterface * Not used, should be left empty. * * @return bool - * TRUE or FALSE, based on success. + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { // PHP installations using non-bundled GD do not have imagefilter. diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php index 802be9e..fbe490d 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php @@ -20,8 +20,7 @@ * toolkit = "gd", * operation = "resize", * label = @Translation("Resize"), - * description = @Translation("GD2 toolkit resize"), - * weight = 0 + * description = @Translation("GD2 toolkit resize") * ) */ class GDResize extends PluginBase implements ImageToolkitOperationInterface { @@ -42,7 +41,7 @@ class GDResize extends PluginBase implements ImageToolkitOperationInterface { * - height: The new height of the resized image, in pixels. * * @return bool - * TRUE or FALSE, based on success. + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { $res = $toolkit->createTmp($image, $data['width'], $data['height']); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php index 038393e..a6fe1d7 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php @@ -20,9 +20,7 @@ * toolkit = "gd", * operation = "rotate", * label = @Translation("Rotate"), - * description = @Translation("GD2 toolkit rotate"), - * toolkit = "gd", - * weight = 0 + * description = @Translation("GD2 toolkit rotate") * ) */ class GDRotate extends PluginBase implements ImageToolkitOperationInterface { @@ -47,7 +45,7 @@ class GDRotate extends PluginBase implements ImageToolkitOperationInterface { * default to transparent. Otherwise it will be white. * * @return bool - * TRUE or FALSE, based on success. + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { // Ensure background variable is set. diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php index 8e24074..76910fd 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php @@ -16,38 +16,13 @@ abstract class ImageToolkitBase extends PluginBase { /** - * Stores instantiated image toolkit operation plugins. - * - * @var array - */ - protected $operationPlugins = array(); - - /** - * Processes a toolkit operation. - * - * @param string $operation - * The toolkit operation to be processed. - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * @param array $data - * An array of variables to be used by the toolkit operation. - * - * @return mixed - * The return value of the image toolkit operation. + * @inheritdoc */ public function process($operation, ImageInterface $image, array $data = array()) { - // Check if operation plugin has been instantiated already. If not, - // create an instance. - if (!isset($this->operationPlugins[$operation])) { - $manager = \Drupal::service('image.toolkit.operation.manager'); - $plugin_id = $manager->getToolkitOperationPluginId($this->getPluginId(), $operation); - $this->operationPlugins[$operation] = $manager->createInstance($plugin_id); - } - // Return the the value returned by the operation process method. - if (isset($this->operationPlugins[$operation])) { - return $this->operationPlugins[$operation]->process($this, $image, $data); - } - return FALSE; + $manager = \Drupal::service('image.toolkit.operation.manager'); + $plugin_id = $manager->getToolkitOperationPluginId($this->getPluginId(), $operation); + $plugin = $manager->createInstance($plugin_id); + return $plugin->process($this, $image, $data); } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index cbc3bfa..535f24f 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -129,7 +129,7 @@ public static function supportedTypes(); * An array of variables to be used by the toolkit operation. * * @return mixed - * The return value of the image toolkit operation. + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process($operation, ImageInterface $image, array $data = array()); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php index b67347e..ae276bc 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php @@ -29,8 +29,8 @@ * @param array $data * An array of data to be used by the toolkit operation. * - * @return mixed - * The return value of the image toolkit operation. + * @return bool + * TRUE if the operation was performed successfully, FALSE otherwise. */ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php index 289adf0..0c721de 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php @@ -39,8 +39,10 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac } /** - * Returns the plugin ID for the plugin having the lowest weight, for a given - * toolkit and operation. + * Returns the plugin ID for the plugin for a given toolkit and operation. + * + * By convention, the id of an ImageToolkitOperation plugin must look like + * "{toolkit}.{operation}". * * @param string $toolkit * The toolkit ID. @@ -48,21 +50,10 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac * The operation (e.g. "crop"). * * @return string - * A full image toolkit operation plugin ID (e.g. "system.gd.crop"), or the - * empty string if no suitable plugin could be found. + * The plugin ID. */ public function getToolkitOperationPluginId($toolkit, $operation) { - $definitions = array_filter($this->getDefinitions(), function ($definition) use ($toolkit, $operation) { - return $definition['toolkit'] == $toolkit && $definition['operation'] == $operation; - }); - if ($definitions) { - if (count($definitions) > 1) { - uasort($definitions, '\Drupal\Component\Utility\SortArray::sortByWeightElement'); - } - $definition = reset($definitions); - return $definition['id']; - } - return ''; + return "$toolkit.$operation"; } }