Skip to content
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

PERF-31: updating category menu item data to be used by history API f… #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions src/Model/Resolver/MenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ScandiPWA\CatalogGraphQl\Model\Resolver;

use Magento\Catalog\Model\Category;
use Magento\Cms\Model\BlockFactory;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Helper\Category as CategoryHelper;
use Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory;
Expand Down Expand Up @@ -47,22 +48,30 @@ class MenuItems implements ResolverInterface
*/
protected $categoryRepository;

/**
* @var BlockFactory
*/
protected BlockFactory $blockFactory;

/**
* @param CategoryHelper $catalogCategory
* @param StateDependentCollectionFactory $categoryCollectionFactory
* @param StoreManagerInterface $storeManager
* @param CategoryRepositoryInterface $categoryRepository
* @param BlockFactory $blockFactory
*/
public function __construct(
CategoryHelper $catalogCategory,
StateDependentCollectionFactory $categoryCollectionFactory,
StoreManagerInterface $storeManager,
CategoryRepositoryInterface $categoryRepository
CategoryRepositoryInterface $categoryRepository,
BlockFactory $blockFactory,
) {
$this->catalogCategory = $catalogCategory;
$this->collectionFactory = $categoryCollectionFactory;
$this->storeManager = $storeManager;
$this->categoryRepository = $categoryRepository;
$this->blockFactory = $blockFactory;
}

/**
Expand Down Expand Up @@ -125,7 +134,7 @@ protected function getCategoryTree($storeId, $rootId)
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
$collection = $this->collectionFactory->create();
$collection->setStoreId($storeId);
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect(['name', 'description', 'landing_page']);
$collection->addFieldToFilter('path', ['like' => '1/' . $rootId . '/%']); //load only from store root
$collection->addAttributeToFilter('include_in_menu', 1);
$collection->addIsActiveFilter();
Expand All @@ -143,11 +152,18 @@ protected function getCategoryTree($storeId, $rootId)
* Convert category to array
*
* @param \Magento\Catalog\Model\Category $category
* @param int $itemId
* @return array
*/
protected function getCategoryAsArray($category)
{
$landingPageId = $category->getData('landing_page');
$landingPageContent = '';

if ($landingPageId) {
$landingPageBlock = $this->blockFactory->create()->load($landingPageId);
$landingPageContent = $landingPageBlock->getContent();
}

return [
'title' => $category->getName(),
'item_id' => $category->getId(),
Expand All @@ -158,7 +174,9 @@ protected function getCategoryAsArray($category)
// For correct placeholders on FE
// Default value is Products because if
// display mode wasn't changed it returns null
'display_mode' => $this->getCategoryDisplayMode($category) ?? Category::DM_PRODUCT
'display_mode' => $this->getCategoryDisplayMode($category) ?? Category::DM_PRODUCT,
'category_banner' => $landingPageContent,
'category_description' => $category->getDescription() ?? '',
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type MenuItem {
category_id: Int
position: Int
display_mode: String
category_banner: String
category_description: String
}

type CurrencyConfig {
Expand Down