Skip to content

Instantly share code, notes, and snippets.

@tdgroot
Created April 10, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdgroot/7b98c44ce2585fa12496b16085d754ce to your computer and use it in GitHub Desktop.
Save tdgroot/7b98c44ce2585fa12496b16085d754ce to your computer and use it in GitHub Desktop.
Add size to Magento_CatalogGraphQl MediaGallery Url field
--- etc/schema.orig.graphqls 2020-01-10 06:20:38.000000000 +0100
+++ etc/schema.graphqls 2020-04-10 18:07:05.630098596 +0200
@@ -197,7 +197,7 @@
}
interface MediaGalleryInterface @doc(description: "Contains basic information about a product image or video.") @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\MediaGalleryTypeResolver") {
- url: String @doc(description: "The URL of the product image or video.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGallery\\Url")
+ url(width: Int, height: Int): String @doc(description: "The URL of the product image or video.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGallery\\Url")
label: String @doc(description: "The label of the product image or video.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGallery\\Label")
}
--- Model/Resolver/Product/MediaGallery/Url.orig.php 2020-01-10 06:20:38.000000000 +0100
+++ Model/Resolver/Product/MediaGallery/Url.php 2020-04-10 18:06:55.281998491 +0200
@@ -14,6 +14,7 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
+use Magento\Framework\View\ConfigInterface;
/**
* Returns media url
@@ -63,7 +64,7 @@
$product = $value['model'];
if (isset($value['image_type'])) {
$imagePath = $product->getData($value['image_type']);
- return $this->getImageUrl($value['image_type'], $imagePath);
+ return $this->getImageUrl($value['image_type'], $imagePath, $args);
}
if (isset($value['file'])) {
$image = $this->productImageFactory->create();
@@ -79,13 +80,16 @@
*
* @param string $imageType
* @param string|null $imagePath
+ * @param array $imageArgs
* @return string
* @throws \Exception
*/
- private function getImageUrl(string $imageType, ?string $imagePath): string
+ private function getImageUrl(string $imageType, ?string $imagePath, array $imageArgs): string
{
$image = $this->productImageFactory->create();
$image->setDestinationSubdir($imageType)
+ ->setWidth($imageArgs['width'] ?? null)
+ ->setHeight($imageArgs['height'] ?? null)
->setBaseFile($imagePath);
if ($image->isBaseFilePlaceholder()) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment