From f4b4681f1c5dc6852811b45d9be1d74205ffa2e1 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Thu, 3 Oct 2013 20:02:32 +0300 Subject: [PATCH] Issue #2063403 by claudiu.cristea: Fixed Fatal when editing a nonexistent image effect. --- .../Drupal/Component/Plugin/DefaultPluginBag.php | 5 ++-- .../Plugin/Exception/UnknownPluginException.php | 31 ++++++++++++++++++++++ core/modules/image/image.module | 11 ++++++-- .../lib/Drupal/image/Form/ImageEffectFormBase.php | 11 +++++++- .../Drupal/image/Tests/ImageAdminStylesTest.php | 8 +++++- 5 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 core/lib/Drupal/Component/Plugin/Exception/UnknownPluginException.php diff --git a/core/lib/Drupal/Component/Plugin/DefaultPluginBag.php b/core/lib/Drupal/Component/Plugin/DefaultPluginBag.php index f966c33..963422c 100644 --- a/core/lib/Drupal/Component/Plugin/DefaultPluginBag.php +++ b/core/lib/Drupal/Component/Plugin/DefaultPluginBag.php @@ -7,9 +7,8 @@ namespace Drupal\Component\Plugin; -use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Component\Plugin\Exception\UnknownPluginException; use Drupal\Component\Utility\MapArray; -use Drupal\Component\Utility\String; /** * Provides a default plugin bag for a plugin type. @@ -76,7 +75,7 @@ public function __construct(PluginManagerInterface $manager, array $configuratio protected function initializePlugin($instance_id) { $configuration = isset($this->configurations[$instance_id]) ? $this->configurations[$instance_id] : array(); if (!isset($configuration[$this->pluginKey])) { - throw new PluginException(String::format("Unknown plugin ID '@instance'.", array('@instance' => $instance_id))); + throw new UnknownPluginException($instance_id); } $this->pluginInstances[$instance_id] = $this->manager->createInstance($configuration[$this->pluginKey], $configuration); $this->addInstanceID($instance_id); diff --git a/core/lib/Drupal/Component/Plugin/Exception/UnknownPluginException.php b/core/lib/Drupal/Component/Plugin/Exception/UnknownPluginException.php new file mode 100644 index 0000000..59586a5 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Exception/UnknownPluginException.php @@ -0,0 +1,31 @@ + $instance_id)); + } + parent::__construct($message, $code, $previous); + } + +} diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 137bf66..9b2a975 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityInterface; +use Drupal\Component\Plugin\Exception\UnknownPluginException; use Drupal\field\Entity\Field; use Drupal\field\Entity\FieldInstance; use Drupal\file\Entity\File; @@ -72,8 +73,14 @@ function image_help($path, $arg) { $effect = \Drupal::service('plugin.manager.image.effect')->getDefinition($arg[7]); return isset($effect['description']) ? ('

' . $effect['description'] . '

') : NULL; case 'admin/config/media/image-styles/manage/%/effects/%': - $effect = entity_load('image_style', $arg[5])->getEffect($arg[7])->getPluginDefinition(); - return isset($effect['description']) ? ('

' . $effect['description'] . '

') : NULL; + try { + $effect = entity_load('image_style', $arg[5])->getEffect($arg[7]); + } + catch (UnknownPluginException $e) { + return NULL; + } + $effect_definition = $effect->getPluginDefinition(); + return isset($effect_definition['description']) ? ('

' . $effect_definition['description'] . '

') : NULL; } } diff --git a/core/modules/image/lib/Drupal/image/Form/ImageEffectFormBase.php b/core/modules/image/lib/Drupal/image/Form/ImageEffectFormBase.php index 308331e..fc0f547 100644 --- a/core/modules/image/lib/Drupal/image/Form/ImageEffectFormBase.php +++ b/core/modules/image/lib/Drupal/image/Form/ImageEffectFormBase.php @@ -10,6 +10,10 @@ use Drupal\Core\Form\FormBase; use Drupal\image\ConfigurableImageEffectInterface; use Drupal\image\ImageStyleInterface; +use Drupal\Component\Plugin\Exception\UnknownPluginException; +use Drupal\Component\Utility\String; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -53,7 +57,12 @@ public function getFormID() { */ public function buildForm(array $form, array &$form_state, ImageStyleInterface $image_style = NULL, $image_effect = NULL) { $this->imageStyle = $image_style; - $this->imageEffect = $this->prepareImageEffect($image_effect); + try { + $this->imageEffect = $this->prepareImageEffect($image_effect); + } + catch (UnknownPluginException $e) { + throw new NotFoundHttpException(String::format("Invalid effect id: '@id'.", array('@id' => $image_effect))); + } $request = $this->getRequest(); if (!($this->imageEffect instanceof ConfigurableImageEffectInterface)) { diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php index 316b750..462b6e5 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php @@ -315,8 +315,9 @@ function testStyleReplacement() { */ function testEditEffect() { // Add a scale effect. + $style_name = 'test_style_effect_edit'; $this->drupalGet('admin/config/media/image-styles/add'); - $this->drupalPostForm(NULL, array('label' => 'Test style effect edit', 'name' => 'test_style_effect_edit'), t('Create new style')); + $this->drupalPostForm(NULL, array('label' => 'Test style effect edit', 'name' => $style_name), t('Create new style')); $this->drupalPostForm(NULL, array('new' => 'image_scale_and_crop'), t('Add')); $this->drupalPostForm(NULL, array('data[width]' => '300', 'data[height]' => '200'), t('Add effect')); $this->assertText(t('Scale and crop 300x200')); @@ -344,6 +345,11 @@ function testEditEffect() { $this->drupalPostForm(NULL, array('data[width]' => '12', 'data[height]' => '19'), t('Add effect')); $this->assertText(t('Scale 24x19')); $this->assertText(t('Scale 12x19')); + + // Try to edit a nonexistent effect. + $uuid = $this->container->get('uuid'); + $this->drupalGet('admin/config/media/image-styles/manage/' . $style_name . '/effects/' . $uuid->generate()); + $this->assertResponse(404); } /** -- 1.8.3.1