Skip to content

Only resize images for themes which are being used instead of all installed themes. #8142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Image/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Catalog\Helper\Image as ImageHelper;
use Magento\Catalog\Model\Product;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig;
use Magento\Framework\App\Area;
use Magento\Framework\View\ConfigInterface;

Expand All @@ -23,6 +24,11 @@ class Cache
*/
protected $themeCollection;

/**
* @var ThemeCustomizationConfig
*/
protected $themeCustomizationConfig;

/**
* @var ImageHelper
*/
Expand All @@ -37,14 +43,17 @@ class Cache
* @param ConfigInterface $viewConfig
* @param ThemeCollection $themeCollection
* @param ImageHelper $imageHelper
* @param ThemeCustomizationConfig $themeCustomizationConfig
*/
public function __construct(
ConfigInterface $viewConfig,
ThemeCollection $themeCollection,
ImageHelper $imageHelper
ImageHelper $imageHelper,
ThemeCustomizationConfig $themeCustomizationConfig
) {
$this->viewConfig = $viewConfig;
$this->themeCollection = $themeCollection;
$this->themeCustomizationConfig = $themeCustomizationConfig;
$this->imageHelper = $imageHelper;
}

Expand All @@ -58,8 +67,10 @@ public function __construct(
protected function getData()
{
if (!$this->data) {
$themes = $this->getThemesInUse();

/** @var \Magento\Theme\Model\Theme $theme */
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
foreach ($themes as $theme) {
$config = $this->viewConfig->getViewConfig([
'area' => Area::AREA_FRONTEND,
'themeModel' => $theme,
Expand Down Expand Up @@ -127,4 +138,25 @@ protected function processImageData(Product $product, array $imageData, $file)

return $this;
}

/**
* Retrieve themes which are currently active in the frontend
*
* @return array
*/
protected function getThemesInUse()
{
$themesInUse = [];

$registeredThemes = $this->themeCollection->loadRegisteredThemes();
$storesByThemes = $this->themeCustomizationConfig->getStoresByThemes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Themes can be set not only on store level, but on product/category/cms. Please addthe logic which would include these cases as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hostep please see comment above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vrann @hostep I've used this code and added themes checking for produt, category and pages.
denisristic@8308ae1#comments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vrann Is it actually necessary to check for product/category/page?
Even if any of them have assigned custom theme that is disabled it would not matter because it would not be displayed because theme is disabled?


foreach ($registeredThemes as $registeredTheme) {
if (array_key_exists($registeredTheme->getThemeId(), $storesByThemes)) {
$themesInUse[] = $registeredTheme;
}
}

return $themesInUse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class CacheTest extends \PHPUnit_Framework_TestCase
*/
protected $themeCollection;

/**
* @var \Magento\Theme\Model\Config\Customization|\PHPUnit_Framework_MockObject_MockObject
*/
protected $themeCustomizationConfig;

/**
* @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -70,6 +75,10 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->themeCustomizationConfig = $this->getMockBuilder(\Magento\Theme\Model\Config\Customization::class)
->disableOriginalConstructor()
->getMock();

$this->imageHelper = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -84,6 +93,7 @@ protected function setUp()
[
'viewConfig' => $this->viewConfig,
'themeCollection' => $this->themeCollection,
'themeCustomizationConfig' => $this->themeCustomizationConfig,
'imageHelper' => $this->imageHelper,
]
);
Expand Down Expand Up @@ -126,6 +136,12 @@ public function testGenerate()
->method('loadRegisteredThemes')
->willReturn([$themeMock]);

$this->themeCustomizationConfig->expects($this->once())
->method('getStoresByThemes')
->willReturn([
$themeMock->getThemeId() => [1]
]);

$this->viewConfig->expects($this->once())
->method('getViewConfig')
->with([
Expand Down