diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 956abf0..ed33313 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -86,8 +86,9 @@ public function getToolkitId(); * @param string $operation * The operation to be performed against the image. * @param array $arguments - * An associative array with required arguments to be passed to the toolkit - * operation. E.g. array('width' => 50, 'height' => 100, 'upscale' => TRUE). + * An associative array of arguments to be passed to the toolkit + * operation, e.g. array('width' => 50, 'height' => 100, + * 'upscale' => TRUE). * * @return bool * TRUE on success, FALSE on failure. diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index a317705..c17a61f 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -7,8 +7,8 @@ namespace Drupal\Core\ImageToolkit; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Image\ImageInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface; use Drupal\Core\Plugin\PluginBase; abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { @@ -45,24 +45,18 @@ public function getRequirements() { } /** - * Return a toolkit operation plugin instance. - * - * @param string $operation - * The toolkit operation requested. - * - * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface - * An instance of the requested toolkit operation plugin. - */ - protected function getToolkitOperation($operation) { - $plugin_id = $this->operationManager->getToolkitOperationPluginId($this->getPluginId(), $operation); - return $this->operationManager->createInstance($plugin_id, array(), $this); - } - - /** - * @inheritdoc + * {@inheritdoc} */ public function apply($operation, ImageInterface $image, array $arguments = array()) { - $plugin = $this->getToolkitOperation($operation); + // Get the plugin to use for the operation. If there are no plugins + // available, log an error message and return. + try { + $plugin = $this->operationManager->getToolkitOperation($this, $operation); + } + catch (PluginNotFoundException $e) { + watchdog('image', 'The selected image handling toolkit %toolkit can not process operation %operation.', array('%toolkit' => $this->getPluginId(), '%operation' => $operation), WATCHDOG_ERROR); + return FALSE; + } // Validate and prepare arguments before applying the operation. The // operation might be cancelled if prepare() decides that the final image diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 94a0463..0ff2128 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -148,15 +148,16 @@ public static function isAvailable(); public static function getSupportedExtensions(); /** - * Applies a toolkit specific operation to an image. + * Applies a toolkit operation to an image. * * @param string $operation * The toolkit operation to be processed. * @param \Drupal\Core\Image\ImageInterface $image * An image object. * @param array $arguments - * An associative array with required arguments to be passed to the toolkit - * operation. E.g. array('width' => 50, 'height' => 100, 'upscale' => TRUE). + * An associative array of arguments to be passed to the toolkit + * operation, e.g. array('width' => 50, 'height' => 100, + * 'upscale' => TRUE). * * @return mixed * TRUE if the operation was performed successfully, FALSE otherwise. diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php index 4671042..0692184 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php @@ -10,7 +10,6 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Component\Plugin\Factory\DefaultFactory; diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php index f254fbf..2f38025 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php @@ -11,9 +11,8 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\Core\TypedData\TypedDataManager; -use Drupal\Component\Plugin\Exception\PluginException; -use Drupal\Component\Plugin\Exception\UnknownPluginException; +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Component\Utility\String; @@ -60,26 +59,37 @@ public function processDefinition(&$definition, $plugin_id) { // If the argument is optional a 'default' should be provided. if (!$argument['required'] && !array_key_exists('default', $argument)) { $message = String::format("Argument '@arg' in plugin '@plugin' is optional but doesn't provide a default value.", array('@arg' => $id, '@plugin' => $definition['id'])); - throw new PluginException($message); + throw new InvalidPluginDefinitionException($message); } } } /** - * {@inheritdoc} + * Returns the plugin ID for a given toolkit and operation. + * + * @param string $toolkit_id + * The toolkit plugin ID. + * @param string $operation + * The operation (e.g. "crop"). + * + * @return string + * The plugin ID. + * + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * When no plugin is available. */ - public function getToolkitOperationPluginId($toolkit, $operation) { + protected function getToolkitOperationPluginId($toolkit_id, $operation) { $definitions = $this->getDefinitions(); $definitions = array_filter($definitions, - function ($definition) use ($toolkit, $operation) { - return $definition['toolkit'] == $toolkit && $definition['operation'] == $operation; + function ($definition) use ($toolkit_id, $operation) { + return $definition['toolkit'] == $toolkit_id && $definition['operation'] == $operation; } ); if (!$definitions) { - $message = String::format("No image operation plugin for '@toolkit' toolkit and '@operation' operation.", array('@toolkit' => $toolkit, '@operation' => $operation)); - throw new UnknownPluginException('', $message); + $message = String::format("No image operation plugin for '@toolkit' toolkit and '@operation' operation.", array('@toolkit' => $toolkit_id, '@operation' => $operation)); + throw new PluginNotFoundException($toolkit_id . '.' . $operation, $message); } else { // Pickup the first plugin found. @@ -93,10 +103,18 @@ function ($definition) use ($toolkit, $operation) { /** * {@inheritdoc} */ - public function createInstance($plugin_id, array $configuration = array(), ImageToolkitInterface $toolkit = NULL, TypedDataManager $typed_data_manager = NULL) { + public function createInstance($plugin_id, array $configuration = array(), ImageToolkitInterface $toolkit = NULL) { $plugin_definition = $this->getDefinition($plugin_id); $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); return new $plugin_class($configuration, $plugin_id, $plugin_definition, $toolkit); } + /** + * {@inheritdoc} + */ + public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation) { + $plugin_id = $this->getToolkitOperationPluginId($toolkit->getPluginId(), $operation); + return $this->createInstance($plugin_id, array(), $toolkit); + } + } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php index 57f5a14..e632adc 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php @@ -13,18 +13,19 @@ interface ImageToolkitOperationManagerInterface { /** - * Returns the plugin ID for a given toolkit and operation. + * Returns a toolkit operation plugin instance. * - * @param string $toolkit - * The toolkit ID. + * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit + * The toolkit instance. * @param string $operation * The operation (e.g. "crop"). * - * @return string - * The plugin ID. + * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface + * An instance of the requested toolkit operation plugin. * - * @throws \Drupal\Component\Plugin\Exception\UnknownPluginException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * When no plugin is available. */ - public function getToolkitOperationPluginId($toolkit, $operation); + public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation); } diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php index 029872b..1ba8c19 100644 --- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php @@ -238,7 +238,7 @@ function testManipulations() { // Load up a fresh image. $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file); $toolkit = $image->getToolkit(); - if (!$image) { + if (!$image->isValid()) { $this->fail(String::format('Could not load image %file.', array('%file' => $file))); continue 2; } @@ -315,4 +315,25 @@ function testManipulations() { } } + /** + * Tests calling a missing image operation plugin. + */ + function testMissingOperation() { + + // Test that the image factory is set to use the GD toolkit. + $this->assertEqual($this->imageFactory->getToolkitId(), 'gd', 'The image factory is set to use the \'gd\' image toolkit.'); + + // An image file that will be tested. + $file = 'image-test.png'; + + // Load up a fresh image. + $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file); + if (!$image->isValid()) { + $this->fail(String::format('Could not load image %file.', array('%file' => $file))); + } + + // Try perform a missing toolkit operation. + $this->assertFalse($image->apply('missing_op', array()), 'Calling a missing image toolkit operation plugin fails.'); + } + }