Skip to content

Commit

Permalink
SWPWA-516 - Magento 2.3.5 (#55)
Browse files Browse the repository at this point in the history
* SWPWA-516 - Magento upgrade 2.3.5

* SWPWA-516 - Magento 2.3.5. upgrade

* SWPWA-516 - Fix for layer filters

* SWPWA-516 - Added back variant collection and searchcriteria

* SWPWA-516 - Removed filter attribute file

* Performance - not loading items when not requested (#56)

* Performance - not loading items when not requested

* Adding isReturnItems flag to product data provider call

* Bringing back mandatory collection load for the sake of layered filters, but skipping post processing

* Removed throw on missing image, add M2.3.5 dependecy

Co-authored-by: Aivars <[email protected]>
Co-authored-by: aleksandrsm <[email protected]>
Co-authored-by: Alfreds Genkins <[email protected]>
  • Loading branch information
4 people authored May 18, 2020
1 parent 273cccf commit 5a000ba
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 364 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"email": "[email protected]"
},
"require": {
"magento/magento2-base": "^2.3.2",
"magento/magento2-base": "^2.3.5",
"scandipwa/performance": "^1.0.3"
},
"type": "magento2-module",
Expand Down
95 changes: 0 additions & 95 deletions src/Model/Config/ProductAttributeReader.php

This file was deleted.

85 changes: 85 additions & 0 deletions src/Model/Resolver/Category/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Category;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Catalog\Model\Category\FileInfo;

use Magento\CatalogGraphQl\Model\Resolver\Category\Image as CoreImage;

class Image extends CoreImage {
/** @var FileInfo */
protected $fileInfo;

/** @var DirectoryList */
protected $directoryList;

/**
* @param DirectoryList $directoryList
* @param FileInfo $fileInfo
*/
public function __construct(
DirectoryList $directoryList,
FileInfo $fileInfo
) {
parent::__construct(
$directoryList,
$fileInfo
);

$this->directoryList = $directoryList;
$this->fileInfo = $fileInfo;
}

public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

/** @var \Magento\Catalog\Model\Category $category */
$category = $value['model'];
$imagePath = $category->getData('image');
if (empty($imagePath)) {
return null;
}

/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$baseUrl = $store->getBaseUrl();

$filenameWithMedia = $this->fileInfo->isBeginsWithMediaDirectoryPath($imagePath)
? $imagePath : $this->formatFileNameWithMediaCategoryFolder($imagePath);

// return full url
return rtrim($baseUrl, '/') . $filenameWithMedia;
}

/**
* Format category media folder to filename
*
* @param string $fileName
* @return string
*/
protected function formatFileNameWithMediaCategoryFolder(string $fileName): string
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$baseFileName = basename($fileName);

return '/'
. $this->directoryList->getUrlPath('media')
. '/'
. ltrim(FileInfo::ENTITY_MEDIA_PATH, '/')
. '/'
. $baseFileName;
}
}
7 changes: 1 addition & 6 deletions src/Model/Resolver/ConfigurableVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\DataObject;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection as AttributeCollection;
Expand Down Expand Up @@ -72,18 +71,14 @@ class ConfigurableVariant implements ResolverInterface
*/
protected $storeManager;

/**
* @var DataObject[]
*/
protected $attributesVisibleOnFrontend;

/**
* @var DataPostProcessor
*/
protected $productPostProcessor;

/**
* ConfigurableVariant constructor.
*
* @param Collection $variantCollection
* @param OptionCollection $optionCollection
* @param ValueFactory $valueFactory
Expand Down
72 changes: 32 additions & 40 deletions src/Model/Resolver/Layer/DataProvider/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
* @category ScandiPWA
* @package ScandiPWA_CatalogGraphQl
* @author Artjoms Travkovs <[email protected]>
* @author Aivars Arbidans <[email protected]>
* @copyright Copyright (c) 2019 Scandiweb, Ltd (https://scandiweb.com)
*/
declare (strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Layer\DataProvider;

use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters as CoreFilters;
use Magento\CatalogGraphQl\Model\Resolver\Layer\FiltersProvider;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Framework\Exception\LocalizedException;

/**
* Layered navigation filters data provider.
*/
class Filters extends \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters
class Filters extends CoreFilters
{
/**
* @var FiltersProvider
Expand All @@ -29,73 +31,49 @@ class Filters extends \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\
/**
* @var array
*/
private $requiredAttributes = [];
private $mappings;

/**
* Filters constructor.
*
* @param FiltersProvider $filtersProvider
*/
public function __construct(
FiltersProvider $filtersProvider
) {
$this->filtersProvider = $filtersProvider;
}

/**
* Sets attributes that are being used by products
*
* @param array $items
* @return void
*/
public function setRequiredAttributesFromItems(array $items)
{
$attributes = ['cat'];

foreach ($items as $item) {
$model = $item['model'];

$itemAttributeCodes = [];
foreach ($model->getAttributes() as $attribute) {
if ($attribute->getIsFilterable()) {
$itemAttributeCodes[] = $attribute->getAttributeCode();
}
}

$attributes = array_unique(array_merge($attributes, $itemAttributeCodes));
}

$this->requiredAttributes = $attributes;
$this->mappings = [
'Category' => 'category'
];
}

/**
* Get layered navigation filters data
*
* @param string $layerType
* @param array|null $attributesToFilter
* @return array
* @throws LocalizedException
*/
public function getData(string $layerType): array
public function getData(string $layerType, array $attributesToFilter = null): array
{
$filtersData = [];

/** @var AbstractFilter $filter */
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
if ($filter->getItemsCount() && $this->hasFilterContents($filter)) {
if ($this->isNeedToAddFilter($filter, $attributesToFilter)) {
$filterGroup = [
'name' => (string) $filter->getName(),
'filter_items_count' => $filter->getItemsCount(),
'request_var' => $filter->getRequestVar(),
];

/** @var Item $filterItem */
foreach ($filter->getItems() ?? [] as $filterItem) {
foreach ($filter->getItems() as $filterItem) {
$filterGroup['filter_items'][] = [
'label' => (string) $filterItem->getLabel(),
'value_string' => $filterItem->getValueString(),
'items_count' => $filterItem->getCount(),
];
}

$filtersData[] = $filterGroup;
}
}
Expand All @@ -104,17 +82,31 @@ public function getData(string $layerType): array
}

/**
* Checks whether filter attribute is present in products
* Check for adding filter to the list
*
* @param AbstractFilter $filter
* @return boolean
* @param $attributesToFilter
* @return bool
* @throws LocalizedException
*/
protected function hasFilterContents(AbstractFilter $filter)
private function isNeedToAddFilter(AbstractFilter $filter, $attributesToFilter): bool
{
if (count($this->requiredAttributes) <= 0) {
return true;
if ($attributesToFilter === null) {
$result = (bool)$filter->getItemsCount();
} else {
if ($filter->hasAttributeModel()) {
$filterAttribute = $filter->getAttributeModel();
$result = in_array($filterAttribute->getAttributeCode(), $attributesToFilter);
} else {
$name = (string)$filter->getName();
if (array_key_exists($name, $this->mappings)) {
$result = in_array($this->mappings[$name], $attributesToFilter);
} else {
$result = true;
}
}
}

return in_array($filter->getRequestVar(), $this->requiredAttributes);
return $result;
}
}
Loading

0 comments on commit 5a000ba

Please sign in to comment.