Skip to content

Commit

Permalink
ENGCOM-4258: [BugFix] Fix area for image placeholder in graphql respo…
Browse files Browse the repository at this point in the history
…nse. #364
  • Loading branch information
naydav authored Feb 21, 2019
2 parents 87d662e + 9f46ab4 commit c2dd26d
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\ImageFactory;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image\Placeholder as PlaceholderProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand All @@ -23,14 +24,21 @@ class Url implements ResolverInterface
* @var ImageFactory
*/
private $productImageFactory;
/**
* @var PlaceholderProvider
*/
private $placeholderProvider;

/**
* @param ImageFactory $productImageFactory
* @param PlaceholderProvider $placeholderProvider
*/
public function __construct(
ImageFactory $productImageFactory
ImageFactory $productImageFactory,
PlaceholderProvider $placeholderProvider
) {
$this->productImageFactory = $productImageFactory;
$this->placeholderProvider = $placeholderProvider;
}

/**
Expand All @@ -55,23 +63,27 @@ public function resolve(
$product = $value['model'];
$imagePath = $product->getData($value['image_type']);

$imageUrl = $this->getImageUrl($value['image_type'], $imagePath);
return $imageUrl;
return $this->getImageUrl($value['image_type'], $imagePath);
}

/**
* Get image url
* Get image URL
*
* @param string $imageType
* @param string|null $imagePath Null if image is not set
* @param string|null $imagePath
* @return string
* @throws \Exception
*/
private function getImageUrl(string $imageType, ?string $imagePath): string
{
$image = $this->productImageFactory->create();
$image->setDestinationSubdir($imageType)
->setBaseFile($imagePath);
$imageUrl = $image->getUrl();
return $imageUrl;

if ($image->isBaseFilePlaceholder()) {
return $this->placeholderProvider->getPlaceholder($imageType);
}

return $image->getUrl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image;

use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
use Magento\Framework\View\Asset\Repository as AssetRepository;

/**
* Image Placeholder provider
*/
class Placeholder
{
/**
* @var PlaceholderFactory
*/
private $placeholderFactory;

/**
* @var AssetRepository
*/
private $assetRepository;

/**
* @param PlaceholderFactory $placeholderFactory
* @param AssetRepository $assetRepository
*/
public function __construct(
PlaceholderFactory $placeholderFactory,
AssetRepository $assetRepository
) {
$this->placeholderFactory = $placeholderFactory;
$this->assetRepository = $assetRepository;
}

/**
* Get placeholder
*
* @param string $imageType
* @return string
*/
public function getPlaceholder(string $imageType): string
{
$imageAsset = $this->placeholderFactory->create(['type' => $imageType]);

// check if placeholder defined in config
if ($imageAsset->getFilePath()) {
return $imageAsset->getUrl();
}

return $this->assetRepository->getUrl(
"Magento_Catalog::images/product/placeholder/{$imageType}.jpg"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image\Placeholder;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Theme provider
*/
class Theme
{
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var ThemeProviderInterface
*/
private $themeProvider;

/**
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param ThemeProviderInterface $themeProvider
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
ThemeProviderInterface $themeProvider
) {
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->themeProvider = $themeProvider;
}

/**
* Get theme model
*
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getThemeData(): array
{
$themeId = $this->scopeConfig->getValue(
\Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getId()
);

/** @var $theme \Magento\Framework\View\Design\ThemeInterface */
$theme = $this->themeProvider->getThemeById($themeId);

$data = $theme->getData();
$data['themeModel'] = $theme;

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function testProductWithBaseImage()
*/
public function testProductWithoutBaseImage()
{
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/239');
$productSku = 'simple';
$query = <<<QUERY
{
Expand All @@ -61,12 +60,25 @@ public function testProductWithoutBaseImage()
url
label
}
small_image {
url
label
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
self::assertEquals('Simple Product', $response['products']['items'][0]['image']['label']);
self::assertStringEndsWith(
'images/product/placeholder/image.jpg',
$response['products']['items'][0]['image']['url']
);
self::assertEquals('Simple Product', $response['products']['items'][0]['small_image']['label']);
self::assertStringEndsWith(
'images/product/placeholder/small_image.jpg',
$response['products']['items'][0]['small_image']['url']
);
}

/**
Expand Down

0 comments on commit c2dd26d

Please sign in to comment.