Skip to content

Commit

Permalink
Added binding to category - enable / disable + delete support./
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredsgenkins committed Jun 18, 2020
1 parent 58a11b9 commit 692488a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 19 deletions.
37 changes: 19 additions & 18 deletions src/Model/Resolver/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace ScandiPWA\MenuOrganizer\Model\Resolver;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Expand All @@ -33,29 +33,19 @@ class Menu implements ResolverInterface
{
public const CATEGORY_ID_KEY = 'category_id';

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

/**
* @var MenuResourceModel
*/
/** @var MenuResourceModel */
protected $menuResourceModel;

/**
* @var ItemCollectionFactory
*/
/** @var ItemCollectionFactory */
protected $itemCollectionFactory;

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

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

/**
Expand All @@ -64,6 +54,7 @@ class Menu implements ResolverInterface
* @param MenuFactory $menuFactory
* @param MenuResourceModel $menuResourceModel
* @param ItemCollectionFactory $itemCollectionFactory
* @param CollectionFactory $collectionFactory
*/
public function __construct(
StoreManagerInterface $storeManager,
Expand All @@ -90,7 +81,7 @@ public function __construct(
* @return array
*
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
public function resolve(
Field $field,
Expand Down Expand Up @@ -122,7 +113,7 @@ public function resolve(
/**
* @param string $menuId
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
private function getMenuItems(string $menuId): array
{
Expand Down Expand Up @@ -157,6 +148,7 @@ private function getMenuItems(string $menuId): array
$categories = $collection
->addAttributeToSelect('url_path')
->addFieldToFilter('entity_id', ['in' => array_keys($categoryIds)])
->addFieldToFilter('is_active', 1)
->getItems();

foreach ($categories as $category) {
Expand All @@ -166,6 +158,15 @@ private function getMenuItems(string $menuId): array
foreach ($itemIds as $itemId) {
$itemsMap[$itemId]['url'] = DIRECTORY_SEPARATOR . $category->getUrlPath();
}

unset($categoryIds[$catId]);
}

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

return array_values($itemsMap);
Expand Down
61 changes: 61 additions & 0 deletions src/Observer/DeleteCategoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace ScandiPWA\MenuOrganizer\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item;

/**
* Class DeleteCategoryItem
* @package ScandiPWA\MenuOrganizer\Observer
*/
class DeleteCategoryItem implements ObserverInterface
{
/**
* @var Item
*/
private $data;

/**
* @var CollectionFactory
*/
private $collection;

/**
* UpdatePageIdentifier constructor.
* @param Item $data
* @param CollectionFactory $collection
*/
public function __construct(
Item $data,
CollectionFactory $collection
)
{
$this->collection = $collection;
$this->data = $data;
}

/**
* @param Observer $observer
* @throws AlreadyExistsException
*/
public function execute(Observer $observer)
{
/** @var $category \Magento\Catalog\Model\Category */
$category = $observer->getEvent()->getCategory();

$items = $this->collection
->create()
->addFieldToFilter('category_id', ['eq' => $category->getId()])
->getItems();

foreach ($items as $item) {
// disable all menu items assigned to category
$item->setData('is_active', 0);
$this->data->save($item);
}
}
}
62 changes: 62 additions & 0 deletions src/Observer/UpdateCategoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace ScandiPWA\MenuOrganizer\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item;

/**
* Class DeleteCategoryItem
* @package ScandiPWA\MenuOrganizer\Observer
*/
class UpdateCategoryItem implements ObserverInterface
{
/**
* @var Item
*/
private $data;

/**
* @var CollectionFactory
*/
private $collection;

/**
* UpdatePageIdentifier constructor.
* @param Item $data
* @param CollectionFactory $collection
*/
public function __construct(
Item $data,
CollectionFactory $collection
)
{
$this->collection = $collection;
$this->data = $data;
}

/**
* @param Observer $observer
* @throws AlreadyExistsException
*/
public function execute(Observer $observer)
{
/** @var $category \Magento\Catalog\Model\Category */
$category = $observer->getEvent()->getCategory();
$isCatActive = $category->getIsActive();

$items = $this->collection
->create()
->addFieldToFilter('category_id', ['eq' => $category->getId()])
->getItems();

foreach ($items as $item) {
// ensure menu items bound to category respect category's "is_active" field
$item->setData('is_active', $isCatActive);
$this->data->save($item);
}
}
}
8 changes: 7 additions & 1 deletion src/etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="cms_page_save_after">
<observer name="cms" instance="ScandiPWA\MenuOrganizer\Observer\UpdatePageIdentifier" />
<observer name="update_menu" instance="ScandiPWA\MenuOrganizer\Observer\UpdatePageIdentifier" />
</event>
<event name="catalog_category_delete_after">
<observer name="update_menu" instance="ScandiPWA\MenuOrganizer\Observer\DeleteCategoryItem" />
</event>
<event name="catalog_category_save_after">
<observer name="update_menu" instance="ScandiPWA\MenuOrganizer\Observer\UpdateCategoryItem" />
</event>
</config>

0 comments on commit 692488a

Please sign in to comment.