diff --git a/focal_point.install b/focal_point.install index 1826ae8..809832e 100644 --- a/focal_point.install +++ b/focal_point.install @@ -25,6 +25,16 @@ function _focal_point_check_crop_version() { } /** + * Implements hook_uninstall(). + */ +function focal_point_uninstall() { + // Remove the focal point configurations. + \Drupal::configFactory()->getEditable('focal_point.settings')->delete(); + \Drupal::configFactory()->getEditable('focal_point.preview')->delete(); + \Drupal::configFactory()->getEditable('crop.type.focal_point')->delete(); +} + +/** * Implements hook_requirements(). */ function focal_point_requirements($phase) { @@ -126,7 +136,8 @@ function focal_point_update_8002(&$sandbox) { */ function focal_point_update_8003() { // Get all current configs for Focal Point. - $existing_config = \Drupal::config('focal_point.settings'); + $existing_config = \Drupal::configFactory()->getEditable('focal_point.settings'); + // Install the new configuration. \Drupal::service('config.installer') diff --git a/src/Controller/FocalPointPreviewController.php b/src/Controller/FocalPointPreviewController.php index c5c7416..c99adbe 100644 --- a/src/Controller/FocalPointPreviewController.php +++ b/src/Controller/FocalPointPreviewController.php @@ -6,7 +6,6 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\image\Entity\ImageStyle; use Drupal\file\Entity\File; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\Validator\Exception\InvalidArgumentException; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityStorageInterface; @@ -18,8 +17,6 @@ use Drupal\Core\Entity\EntityStorageInterface; */ class FocalPointPreviewController extends ControllerBase { - use StringTranslationTrait; - /** * {@inheritdoc} * @@ -53,22 +50,30 @@ class FocalPointPreviewController extends ControllerBase { ], ]; - foreach ($styles as $style) { - $style_label = $style->get('label'); - $url = $this->buildUrl($style, $file, $focal_point_value); - - $derivative_images[$style->getName()] = [ - 'style' => $style_label, - 'url' => $url, - 'image' => [ - '#theme' => 'image', - '#uri' => $url, - '#alt' => $this->t('Focal Point Preview: %label', array('%label' => $style_label)), - '#attributes' => [ - 'class' => ['focal-point-derivative-preview-image'], + if (!empty($styles)) { + foreach ($styles as $style) { + $style_label = $style->get('label'); + $url = $this->buildUrl($style, $file, $focal_point_value); + + $derivative_images[$style->getName()] = [ + 'style' => $style_label, + 'url' => $url, + 'image' => [ + '#theme' => 'image', + '#uri' => $url, + '#alt' => $this->t('Focal Point Preview: %label', array('%label' => $style_label)), + '#attributes' => [ + 'class' => ['focal-point-derivative-preview-image'], + ], ], - ], - ]; + ]; + } + $derivative_image_note = $this->t('Click an image to see a larger preview. You may need to scroll horizontally for more image styles.'); + } + else { + // There are no styles that use a focal point effect to preview. + drupal_set_message($this->t('You must have at least one image styles defined that uses a focal point effect in order to preview.', array(':url' => '/admin/config/media/image-styles')), 'warning'); + $derivative_image_note = ''; } $output['focal_point_preview_page'] = array( @@ -77,7 +82,7 @@ class FocalPointPreviewController extends ControllerBase { '#derivative_images' => $derivative_images, '#focal_point' => $focal_point_value, '#preview_image_note' => $this->t('This preview image above may have been scaled to fit on the page.'), - '#derivative_image_note' => $this->t('Click an image to see a larger preview. You may need to scroll horizontally for more image styles.'), + '#derivative_image_note' => $derivative_image_note, ); return $output; diff --git a/src/FocalPointEffectBase.php b/src/FocalPointEffectBase.php index be4d479..4184a3b 100644 --- a/src/FocalPointEffectBase.php +++ b/src/FocalPointEffectBase.php @@ -11,6 +11,7 @@ use Drupal\crop\Entity\Crop; use Drupal\image\Plugin\ImageEffect\ResizeImageEffect; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Provides a base class for image effects. @@ -46,6 +47,13 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain protected $focalPointManager; /** + * The current request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + public $request; + + /** * Constructs a \Drupal\focal_point\FocalPointEffectBase object. * * @param array $configuration @@ -62,12 +70,15 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain * Crop storage. * @param \Drupal\Core\Config\ImmutableConfig $focal_point_config * Focal point configuration object. + * @param \Symfony\Component\HttpFoundation\Request + * Current request object. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, FocalPointManager $focal_point_manager, CropStorageInterface $crop_storage, ImmutableConfig $focal_point_config) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, FocalPointManager $focal_point_manager, CropStorageInterface $crop_storage, ImmutableConfig $focal_point_config, Request $request) { parent::__construct($configuration, $plugin_id, $plugin_definition, $logger); $this->focalPointManager = $focal_point_manager; $this->cropStorage = $crop_storage; $this->focalPointConfig = $focal_point_config; + $this->request = $request; } /** @@ -83,7 +94,8 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain $container->get('logger.factory')->get('image'), new FocalPointManager($container->get('entity_type.manager')), $container->get('entity_type.manager')->getStorage('crop'), - $container->get('config.factory')->get('focal_point.settings') + $container->get('config.factory')->get('focal_point.settings'), + \Drupal::request() ); } @@ -270,6 +282,6 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain * @codeCoverageIgnore */ protected function getPreviewValue() { - return \Drupal::request()->query->get('focal_point_preview_value'); + return $this->request->query->get('focal_point_preview_value'); } } diff --git a/tests/src/Unit/Effects/FocalPointEffectsTest.php b/tests/src/Unit/Effects/FocalPointEffectsTest.php index 50e9f83..4c2e498 100644 --- a/tests/src/Unit/Effects/FocalPointEffectsTest.php +++ b/tests/src/Unit/Effects/FocalPointEffectsTest.php @@ -6,11 +6,11 @@ use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Image\ImageInterface; use Drupal\crop\CropInterface; use Drupal\crop\CropStorageInterface; -use Drupal\focal_point\FocalPointManager; use Drupal\focal_point\Plugin\ImageEffect\FocalPointCropImageEffect; use Drupal\focal_point\FocalPointEffectBase; use Psr\Log\LoggerInterface; use Drupal\Tests\focal_point\Unit\FocalPointUnitTestCase; +use Symfony\Component\HttpFoundation\Request; /** * Tests the Focal Point image effects. @@ -35,8 +35,9 @@ class FocalPointEffectsTest extends FocalPointUnitTestCase { $logger = $this->prophesize(LoggerInterface::class); $crop_storage = $this->prophesize(CropStorageInterface::class); $focal_point_config = $this->prophesize(ImmutableConfig::class); + $request = $this->prophesize(Request::class); - $effect = new FocalPointCropImageEffect([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $focal_point_config->reveal()); + $effect = new FocalPointCropImageEffect([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $focal_point_config->reveal(), $request->reveal()); $this->assertAttributeEquals($crop_storage->reveal(), 'cropStorage', $effect); $this->assertAttributeEquals($focal_point_config->reveal(), 'focalPointConfig', $effect); } @@ -75,11 +76,12 @@ class FocalPointEffectsTest extends FocalPointUnitTestCase { $logger = $this->prophesize(LoggerInterface::class); $crop_storage = $this->prophesize(CropStorageInterface::class); $immutable_config = $this->prophesize(ImmutableConfig::class); + $request = $this->prophesize(Request::class); $original_image = $this->prophesize(ImageInterface::class); $original_image = $original_image->reveal(); - $effect = new FocalPointCropImageEffect([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $immutable_config->reveal()); + $effect = new FocalPointCropImageEffect([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $immutable_config->reveal(), $request->reveal()); $effect->setOriginalImage($original_image); $this->assertEquals($original_image, $effect->getOriginalImage()); @@ -94,6 +96,7 @@ class FocalPointEffectsTest extends FocalPointUnitTestCase { $logger = $this->prophesize(LoggerInterface::class); $crop_storage = $this->prophesize(CropStorageInterface::class); $immutable_config = $this->prophesize(ImmutableConfig::class); + $request = $this->prophesize(Request::class); $original_image = $this->prophesize(ImageInterface::class); $original_image->getWidth()->willReturn($original_image_size['width']); @@ -114,7 +117,7 @@ class FocalPointEffectsTest extends FocalPointUnitTestCase { ]); // Use reflection to test a private/protected method. - $effect = new TestFocalPointEffectBase([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $immutable_config->reveal()); + $effect = new TestFocalPointEffectBase([], 'plugin_id', [], $logger->reveal(), $this->focalPointManager, $crop_storage->reveal(), $immutable_config->reveal(), $request->reveal()); $effect->setOriginalImage($original_image->reveal()); $effect_reflection = new \ReflectionClass(TestFocalPointEffectBase::class); $method = $effect_reflection->getMethod('calculateAnchor'); diff --git a/tests/src/Unit/FocalPointUnitTestCase.php b/tests/src/Unit/FocalPointUnitTestCase.php index be1d0cc..98d4dd8 100644 --- a/tests/src/Unit/FocalPointUnitTestCase.php +++ b/tests/src/Unit/FocalPointUnitTestCase.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManager; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\focal_point\FocalPointManager; use Drupal\Tests\UnitTestCase; +use Symfony\Component\HttpFoundation\Request; /** * @group Focal Point @@ -40,6 +41,9 @@ abstract class FocalPointUnitTestCase extends UnitTestCase { $this->container = $this->prophesize(ContainerInterface::class); $this->container->get('entity_type.manager')->willReturn($entity_type_manager); + $request = $this->prophesize(Request::class); + $this->container->get('request')->willReturn($request); + $this->focalPointManager = new FocalPointManager($entity_type_manager->reveal()); $this->container->get('focal_point.manager')->willReturn($this->focalPointManager);