Skip to content

Commit

Permalink
Added support for URL-rewrites, instead of url-keys / url-paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredsgenkins committed Jun 26, 2020
1 parent 692488a commit fae040b
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions src/Model/Resolver/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

namespace ScandiPWA\MenuOrganizer\Model\Resolver;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\Category;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use ScandiPWA\MenuOrganizer\Api\Data\ItemInterface;
use ScandiPWA\MenuOrganizer\Model\MenuFactory;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CatCollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory as ItemCollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Menu as MenuResourceModel;

Expand All @@ -33,6 +36,8 @@ class Menu implements ResolverInterface
{
public const CATEGORY_ID_KEY = 'category_id';

public const CMS_PAGE_IDENTIFIER = 'cms_page_identifier';

/** @var MenuFactory */
protected $menuFactory;

Expand All @@ -45,29 +50,41 @@ class Menu implements ResolverInterface
/** @var StoreManagerInterface */
protected $storeManager;

/** @var CollectionFactory */
protected $collectionFactory;
/** @var CatCollectionFactory */
protected $catCollectionFactory;

/** @var PageCollectionFactory */
protected $pageCollectionFactory;

/** @var UrlInterface */
protected $urlBuilder;

/**
* Menu constructor.
* @param StoreManagerInterface $storeManager
* @param MenuFactory $menuFactory
* @param MenuResourceModel $menuResourceModel
* @param ItemCollectionFactory $itemCollectionFactory
* @param CollectionFactory $collectionFactory
* @param CatCollectionFactory $catCollectionFactory
* @param PageCollectionFactory $pageCollectionFactory
* @param UrlInterface $urlBuilder
*/
public function __construct(
StoreManagerInterface $storeManager,
MenuFactory $menuFactory,
MenuResourceModel $menuResourceModel,
ItemCollectionFactory $itemCollectionFactory,
CollectionFactory $collectionFactory
CatCollectionFactory $catCollectionFactory,
PageCollectionFactory $pageCollectionFactory,
UrlInterface $urlBuilder
) {
$this->storeManager = $storeManager;
$this->menuFactory = $menuFactory;
$this->menuResourceModel = $menuResourceModel;
$this->itemCollectionFactory = $itemCollectionFactory;
$this->collectionFactory = $collectionFactory;
$this->catCollectionFactory = $catCollectionFactory;
$this->pageCollectionFactory = $pageCollectionFactory;
$this->urlBuilder = $urlBuilder;
}

/**
Expand Down Expand Up @@ -126,6 +143,7 @@ private function getMenuItems(string $menuId): array
->getData();

$categoryIds = [];
$pageIdentifiers = [];
$itemsMap = [];

foreach ($menuItems as $item) {
Expand All @@ -139,14 +157,22 @@ private function getMenuItems(string $menuId): array
} else {
$categoryIds[$catId] = [$itemId];
}
} else if (isset($item[self::CMS_PAGE_IDENTIFIER])) {
$pageIdentifier = $item[self::CMS_PAGE_IDENTIFIER];

if (isset($pageIdentifiers[$pageIdentifier])) {
$pageIdentifiers[$pageIdentifier][] = $itemId;
} else {
$pageIdentifiers[$pageIdentifier] = [$itemId];
}
}

$itemsMap[$itemId] = $item;
}

$collection = $this->collectionFactory->create();
$categories = $collection
->addAttributeToSelect('url_path')
$catCollection = $this->catCollectionFactory->create();
$categories = $catCollection
->addAttributeToSelect('*')
->addFieldToFilter('entity_id', ['in' => array_keys($categoryIds)])
->addFieldToFilter('is_active', 1)
->getItems();
Expand All @@ -156,15 +182,31 @@ private function getMenuItems(string $menuId): array
$itemIds = $categoryIds[$catId];

foreach ($itemIds as $itemId) {
$itemsMap[$itemId]['url'] = DIRECTORY_SEPARATOR . $category->getUrlPath();
/** @var $category Category */
$itemsMap[$itemId]['url'] = parse_url($category->getUrl(), PHP_URL_PATH);
}
}

unset($categoryIds[$catId]);
$pageCollection = $this->pageCollectionFactory->create();
$pages = $pageCollection
->addFieldToFilter('identifier', ['in' => array_keys($pageIdentifiers)])
->addFieldToFilter('is_active', 1)
->addStoreFilter($this->storeManager->getStore()->getId())
->getItems();

foreach ($pages as $page) {
$pageIdentifier = $page->getIdentifier();
$itemIds = $pageIdentifiers[$pageIdentifier];

foreach ($itemIds as $itemId) {
$url = $this->urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]);
$itemsMap[$itemId]['url'] = parse_url($url, PHP_URL_PATH);
}
}

foreach ($itemsMap as $itemId => $item) {
// do not include items which URL can not be handled URL
if (!$item['cms_page_identifier'] && !$item['url']) {
if (!$item['url']) {
unset($itemsMap[$itemId]);
}
}
Expand Down

0 comments on commit fae040b

Please sign in to comment.