Skip to content

fix: nav links missing, due to duplicate page identifiers #11

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 15 additions & 38 deletions Model/MenuLinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use DevBera\CmsLinkToMenu\Model\LinkTypes;
use Magento\Cms\Api\GetPageByIdentifierInterface;

class MenuLinkManagement implements MenuLinkManagementInterface
{
Expand Down Expand Up @@ -75,6 +76,8 @@ class MenuLinkManagement implements MenuLinkManagementInterface
*/
private $logger;

protected GetPageByIdentifierInterface $getPageByIdentifier;

public function __construct(
NodeFactory $nodeFactory,
PageRepositoryInterface $pageRepository,
Expand All @@ -84,7 +87,8 @@ public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
Processor $processor,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
GetPageByIdentifierInterface $getPageByIdentifier
) {
$this->logger = $logger;
$this->filterBuilder = $filterBuilder;
Expand All @@ -95,6 +99,7 @@ public function __construct(
$this->nodeFactory = $nodeFactory;
$this->pageRepository = $pageRepository;
$this->processor = $processor;
$this->getPageByIdentifier = $getPageByIdentifier;
}

public function addLinks($subject, $position = 'left')
Expand Down Expand Up @@ -209,45 +214,17 @@ private function addExternalLink($subject, $menuItem)
}
private function getFindCmsPageList($links)
{
$cmsPagesIdentifier = [];
$result = [];

foreach ($links as $link) {
if ($link['link_type'] == 1) {
$cmsPagesIdentifier[] = $link['page_id'];
}
}
array_unique($cmsPagesIdentifier);

$this->searchCriteriaBuilder->addFilters(
[
$this->filterBuilder
->setField('identifier')
->setConditionType('in')
->setValue($cmsPagesIdentifier)
->create(),
]
);

$this->searchCriteriaBuilder->addFilters(
[
$this->filterBuilder
->setField('is_active')
->setConditionType('eq')
->setValue(true)
->create(),
]
);

$this->searchCriteriaBuilder->setCurrentPage(1)->setPageSize(count($cmsPagesIdentifier));

$searchCriteria = $this->searchCriteriaBuilder->create();
$pages = $this->pageRepository->getList($searchCriteria);

if ($pages->getTotalCount() >0) {
$items = $pages->getItems();
foreach ($items as $page) {
$result[$page->getIdentifier()] = $page;
if ($link['link_type'] == 1 && !isset($result[$link['page_id']])) {
try{
$result[$link['page_id']] = $this->getPageByIdentifier->execute(
$link['page_id'],
$this->storeManager->getStore()->getId()
);
}catch(\Magento\Framework\Exception\NoSuchEntityException $e){
// fail silently
}
}
}
return $result;
Expand Down