diff --git a/core/modules/responsive_image/responsive_image.post_update.php b/core/modules/responsive_image/responsive_image.post_update.php new file mode 100644 index 0000000..73265df --- /dev/null +++ b/core/modules/responsive_image/responsive_image.post_update.php @@ -0,0 +1,29 @@ +id()); + // Re-save to update dependencies. + $display->save(); + } +} + +/** + * @} End of "addtogroup updates-8.1.0". + */ diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index e66cb82..701388b 100644 --- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -15,6 +15,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase; +use Drupal\responsive_image\Entity\ResponsiveImageStyle; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Utility\LinkGeneratorInterface; @@ -251,4 +252,19 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } return $elements; } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + $style_id = $this->getSetting('responsive_image_style'); + /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $style */ + if ($style_id && $style = ResponsiveImageStyle::load($style_id)) { + // Add the responsive image style as dependency. + $dependencies[$style->getConfigDependencyKey()][] = $style->getConfigDependencyName(); + } + return $dependencies; + } + } diff --git a/core/modules/responsive_image/src/Tests/Update/ResponsiveImageUpdateTest.php b/core/modules/responsive_image/src/Tests/Update/ResponsiveImageUpdateTest.php new file mode 100644 index 0000000..1d3ff1c --- /dev/null +++ b/core/modules/responsive_image/src/Tests/Update/ResponsiveImageUpdateTest.php @@ -0,0 +1,60 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8-rc1.bare.standard.php.gz', + ]; + } + + /** + * Tests post-update responsive_image_post_update_dependency(). + * + * @see responsive_image_post_update_dependency() + */ + public function testPostUpdateDependency() { + $this->container->get('module_installer')->install(['responsive_image']); + // Change 'field_image' formatter to a responsive image formatter. + $options = [ + 'type' => 'responsive_image', + 'label' => 'hidden', + 'settings' => ['responsive_image_style' => 'wide', 'image_link' => ''], + 'third_party_settings' => [], + ]; + $display = $this->config('core.entity_view_display.node.article.default'); + $display->set('content.field_image', $options)->save(TRUE); + + // Check that there's no dependency to 'responsive_image.styles.wide'. + $dependencies = $display->get('dependencies.config') ?: []; + $this->assertFalse(in_array('responsive_image.styles.wide', $dependencies)); + + // Run updates. + $this->runUpdates(); + + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */ + $view_display = EntityViewDisplay::load('node.article.default'); + $dependencies = $view_display->getDependencies() + ['config' => []]; + // Check that post-update added a 'responsive_image.styles.wide' dependency. + $this->assertTrue(in_array('responsive_image.styles.wide', $dependencies['config'])); + } + +} diff --git a/core/modules/responsive_image/tests/src/Kernel/ResponsiveImageIntegrationTest.php b/core/modules/responsive_image/tests/src/Kernel/ResponsiveImageIntegrationTest.php new file mode 100644 index 0000000..6f2a7cb --- /dev/null +++ b/core/modules/responsive_image/tests/src/Kernel/ResponsiveImageIntegrationTest.php @@ -0,0 +1,77 @@ + 'foo', + 'label' => 'Foo', + 'breakpoint_group' => 'responsive_image_test_module', + ])->save(); + // Create an image field to be used with a responsive image formatter. + FieldStorageConfig::create([ + 'type' => 'image', + 'entity_type' => 'entity_test', + 'field_name' => 'bar', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + 'field_name' => 'bar', + ])->save(); + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ + $display = EntityViewDisplay::create([ + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'default', + ]); + $display->setComponent('bar', [ + 'type' => 'responsive_image', + 'label' => 'hidden', + 'settings' => ['responsive_image_style' => 'foo', 'image_link' => ''], + 'third_party_settings' => [], + ])->save(); + + // Check that the 'foo' field is on the display. + $this->assertNotNull($display = EntityViewDisplay::load('entity_test.entity_test.default')); + $this->assertTrue($display->getComponent('bar')); + $this->assertFalse(array_key_exists('bar', $display->get('hidden'))); + + // Delete the responsive image style. + ResponsiveImageStyle::load('foo')->delete(); + + // Check that the view display wa snot deleted. + $this->assertNotNull($display = EntityViewDisplay::load('entity_test.entity_test.default')); + // Check that the 'foo' field was disabled. + $this->assertNull($display->getComponent('bar')); + $this->assertTrue(array_key_exists('bar', $display->get('hidden'))); + } + +}