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

#104299 - Updated snowdog-menu block id #309

Open
wants to merge 18 commits into
base: develop
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
19 changes: 18 additions & 1 deletion Block/Adminhtml/Edit/Tab/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Registry;
use Snowdog\Menu\Api\NodeRepositoryInterface;
use Snowdog\Menu\Controller\Adminhtml\Menu\Edit;
use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\Menu\Node\Image\File as ImageFile;
use Snowdog\Menu\Model\NodeTypeProvider;
use Snowdog\Menu\Model\VueProvider;
Expand Down Expand Up @@ -45,7 +46,13 @@ class Nodes extends Template implements TabInterface
*/
private $vueProvider;

/**
* @var MenuHelper
*/
private $menuHelper;

public function __construct(
MenuHelper $menuHelper,
Template\Context $context,
NodeRepositoryInterface $nodeRepository,
ImageFile $imageFile,
Expand All @@ -60,14 +67,15 @@ public function __construct(
$this->nodeTypeProvider = $nodeTypeProvider;
$this->imageFile = $imageFile;
$this->vueProvider = $vueProvider;
$this->menuHelper = $menuHelper;
}

public function renderNodes()
{
$menu = $this->registry->registry(Edit::REGISTRY_CODE);
$data = [];
if ($menu) {
$nodes = $this->nodeRepository->getByMenu($menu->getId());
$nodes = $this->nodeRepository->getByMenu($this->menuHelper->getLinkValue($menu));
if (!empty($nodes)) {
foreach ($nodes as $node) {
$level = $node->getLevel();
Expand Down Expand Up @@ -206,4 +214,13 @@ public function getVueComponents(): array
{
return $this->vueProvider->getComponents();
}

/**
* @throws LocalizedException
*/
public function getBlockHtmlId(): string
{
$uuid = $this->mathRandom->getRandomString(5);
return '"snowdog-menu-' . $uuid . '"';
}
}
13 changes: 11 additions & 2 deletions Controller/Adminhtml/Menu/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NotFoundException;
use Snowdog\Menu\Controller\Adminhtml\MenuAction;
use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\MenuFactory;

/**
Expand Down Expand Up @@ -46,6 +47,12 @@ class Delete extends MenuAction
private $searchCriteriaBuilderFactory;

/**
* @var MenuHelper
*/
private $menuHelper;

/**
* @param MenuHelper $menuHelper
* @param Action\Context $context
* @param MenuRepositoryInterface $menuRepository
* @param NodeRepositoryInterface $nodeRepository
Expand All @@ -55,6 +62,7 @@ class Delete extends MenuAction
* @param MenuFactory $menuFactory
*/
public function __construct(
MenuHelper $menuHelper,
Action\Context $context,
MenuRepositoryInterface $menuRepository,
NodeRepositoryInterface $nodeRepository,
Expand All @@ -64,6 +72,7 @@ public function __construct(
MenuFactory $menuFactory
) {
parent::__construct($context, $menuRepository, $menuFactory);
$this->menuHelper = $menuHelper;
$this->nodeRepository = $nodeRepository;
$this->filterBuilderFactory = $filterBuilderFactory;
$this->filterGroupBuilderFactory = $filterGroupBuilderFactory;
Expand All @@ -87,8 +96,8 @@ public function execute()
$this->menuRepository->delete($menu);

$filterBuilder = $this->filterBuilderFactory->create();
$filter = $filterBuilder->setField('menu_id')
->setValue($menu->getMenuId())
$filter = $filterBuilder->setField($this->menuHelper->getLinkField())
->setValue($this->menuHelper->getLinkValue($menu))
->setConditionType('eq')
->create();

Expand Down
32 changes: 32 additions & 0 deletions Helper/MenuHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Snowdog\Menu\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\EntityManager\MetadataPool;
use Snowdog\Menu\Api\Data\MenuInterface;

class MenuHelper extends AbstractHelper
{
private MetadataPool $metadataPool;

public function __construct(
MetadataPool $metadataPool,
Context $context
) {
$this->metadataPool = $metadataPool;
parent::__construct($context);
}

public function getLinkField(): string
{
$metadata = $this->metadataPool->getMetadata(MenuInterface::class);
return $metadata->getLinkField();
}

public function getLinkValue(MenuInterface $menu): string
{
return (string) $menu->getData($this->getLinkField());
}
}
11 changes: 9 additions & 2 deletions Model/ImportExport/Processor/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Snowdog\Menu\Model\ImportExport\Processor;

use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\ImportExport\Processor\Export\Menu;
use Snowdog\Menu\Model\ImportExport\Processor\Export\Node;
use Snowdog\Menu\Model\ImportExport\Processor\ExtendedFields;
Expand All @@ -20,16 +21,22 @@ class Export
*/
private $node;

public function __construct(Menu $menu, Node $node)
/**
* @var MenuHelper
*/
private $menuHelper;

public function __construct(Menu $menu, Node $node, MenuHelper $menuHelper)
{
$this->menu = $menu;
$this->node = $node;
$this->menuHelper = $menuHelper;
}

public function getExportData(int $menuId): array
{
$data = $this->menu->getData($menuId);
$nodes = $this->node->getList($menuId);
$nodes = $this->node->getList((int) $data[$this->menuHelper->getLinkField()]);

if ($nodes) {
$data[ExtendedFields::NODES] = $nodes;
Expand Down
11 changes: 10 additions & 1 deletion Model/ImportExport/Processor/Export/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Snowdog\Menu\Api\Data\MenuInterface;
use Snowdog\Menu\Api\Data\NodeInterface;
use Snowdog\Menu\Api\NodeRepositoryInterface;
use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\ImportExport\Processor\Export\Node\Tree as NodeTree;

class Node
Expand Down Expand Up @@ -40,7 +42,13 @@ class Node
*/
private $nodeTree;

/**
* @var MenuHelper
*/
private $menuHelper;

public function __construct(
MenuHelper $menuHelper,
SearchCriteriaBuilder $searchCriteriaBuilder,
SortOrderBuilder $sortOrderBuilder,
NodeRepositoryInterface $nodeRepository,
Expand All @@ -50,6 +58,7 @@ public function __construct(
$this->sortOrderBuilder = $sortOrderBuilder;
$this->nodeRepository = $nodeRepository;
$this->nodeTree = $nodeTree;
$this->menuHelper = $menuHelper;
}

public function getList(int $menuId): array
Expand All @@ -60,7 +69,7 @@ public function getList(int $menuId): array
->create();

$searchCriteria = $this->searchCriteriaBuilder
->addFilter(NodeInterface::MENU_ID, $menuId)
->addFilter($this->menuHelper->getLinkField(), $menuId)
->setSortOrders([$sortOrder])
->create();

Expand Down
15 changes: 12 additions & 3 deletions Model/ImportExport/Processor/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Framework\App\Config\ScopeConfigInterface;
use Snowdog\Menu\Api\Data\MenuInterface;
use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\ImportExport\Processor\Import\InvalidNodes as InvalidNodesProcessor;
use Snowdog\Menu\Model\ImportExport\Processor\Import\Menu as MenuProcessor;
use Snowdog\Menu\Model\ImportExport\Processor\Import\Node as NodeProcessor;
Expand Down Expand Up @@ -33,23 +34,31 @@ class Import
* @var InvalidNodesProcessor
*/
private $invalidNodesProcessor;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var MenuHelper
*/
private $menuHelper;

public function __construct(
MenuProcessor $menuProcessor,
NodeProcessor $nodeProcessor,
ValidationAggregateError $validationAggregateError,
InvalidNodesProcessor $invalidNodesProcessor,
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
MenuHelper $menuHelper
) {
$this->menuProcessor = $menuProcessor;
$this->nodeProcessor = $nodeProcessor;
$this->validationAggregateError = $validationAggregateError;
$this->invalidNodesProcessor = $invalidNodesProcessor;
$this->scopeConfig = $scopeConfig;
$this->menuHelper = $menuHelper;
}

/**
Expand All @@ -60,9 +69,9 @@ public function importData(array $data): string
$this->validateData($data);

$menu = $this->createMenu($data);

$menuId = $menu->getData($this->menuHelper->getLinkField());
if (isset($data[ExtendedFields::NODES])) {
$this->nodeProcessor->createNodes($data[ExtendedFields::NODES], (int) $menu->getId());
$this->nodeProcessor->createNodes($data[ExtendedFields::NODES], (int) $menuId);
}

return $menu->getIdentifier();
Expand Down
11 changes: 9 additions & 2 deletions Model/ImportExport/Processor/Import/Node/DataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Snowdog\Menu\Model\ImportExport\Processor\Import\Node;

use Snowdog\Menu\Api\Data\NodeInterface;
use Snowdog\Menu\Helper\MenuHelper;
use Snowdog\Menu\Model\ImportExport\Processor\ExtendedFields;
use Snowdog\Menu\Model\ImportExport\Processor\Import\FieldProcessor\Boolean as BooleanField;
use Snowdog\Menu\Model\ImportExport\Processor\Import\Node\TypeContent;
Expand All @@ -21,10 +22,16 @@ class DataProcessor
*/
private $typeContent;

public function __construct(BooleanField $booleanField, TypeContent $typeContent)
/**
* @var MenuHelper
*/
private $menuHelper;

public function __construct(BooleanField $booleanField, TypeContent $typeContent, MenuHelper $menuHelper)
{
$this->booleanField = $booleanField;
$this->typeContent = $typeContent;
$this->menuHelper = $menuHelper;
}

public function getData(
Expand All @@ -34,7 +41,7 @@ public function getData(
int $position = 0,
?int $parentId = null
): array {
$data[NodeInterface::MENU_ID] = $menuId;
$data[$this->menuHelper->getLinkField()] = $menuId;
$data[NodeInterface::PARENT_ID] = $parentId;
$data[NodeInterface::LEVEL] = $level;
$data[NodeInterface::POSITION] = $position;
Expand Down
31 changes: 27 additions & 4 deletions Model/Menu.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php
namespace Snowdog\Menu\Model;

use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Snowdog\Menu\Api\Data\MenuInterface;
use Snowdog\Menu\Helper\MenuHelper;

/**
* @method string getCssClass()
Expand All @@ -14,6 +20,23 @@ class Menu extends AbstractModel implements MenuInterface, IdentityInterface

protected $_cacheTag = self::CACHE_TAG;

/**
* @var MenuHelper
*/
public $menuHelper;

public function __construct(
MenuHelper $menuHelper,
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->menuHelper = $menuHelper;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

protected function _construct()
{
$this->_init(\Snowdog\Menu\Model\ResourceModel\Menu::class);
Expand All @@ -28,8 +51,8 @@ public function getStores()
{
$connection = $this->getResource()->getConnection();
$select = $connection->select()->from($this->getResource()->getTable('snowmenu_store'), ['store_id'])->where(
'menu_id = ?',
$this->getId()
$this->menuHelper->getLinkField() . ' = ?',
$this->menuHelper->getLinkValue($this)
);
return $connection->fetchCol($select);
}
Expand All @@ -46,9 +69,9 @@ public function saveStores(array $stores)
$connection = $this->getResource()->getConnection();
$connection->beginTransaction();
$table = $this->getResource()->getTable('snowmenu_store');
$connection->delete($table, ['menu_id = ?' => $this->getId()]);
$connection->delete($table, [$this->menuHelper->getLinkField() . ' = ?' => $this->menuHelper->getLinkValue($this)]);
foreach ($stores as $store) {
$connection->insert($table, ['menu_id' => $this->getId(), 'store_id' => $store]);
$connection->insert($table, [$this->menuHelper->getLinkField() => $this->menuHelper->getLinkValue($this), 'store_id' => $store]);
}
$connection->commit();

Expand Down
Loading
Loading