Skip to content

Commit 7022bf2

Browse files
committed
Add Option for custom link to menu
1 parent aa9362c commit 7022bf2

File tree

8 files changed

+468
-13
lines changed

8 files changed

+468
-13
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* A Magento 2 module named DevBera/CmsLinkToMenu
5+
* Copyright (C) 2019 Copyright 2019 © amitbera.com. All Rights Reserved
6+
*
7+
* This file included in DevBera/CmsLinkToMenu is licensed under OSL 3.0
8+
*
9+
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10+
* Please see LICENSE.txt for the full text of the OSL 3.0 license
11+
*/
12+
namespace DevBera\CmsLinkToMenu\Block\Adminhtml\Form\Field;
13+
14+
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
15+
16+
class StaticLinks extends AbstractFieldArray
17+
{
18+
protected function _prepareToRender(): void
19+
{
20+
$this->addColumn('link_text', ['label' => __('Text')]);
21+
$this->addColumn('link_url', ['label' => __('Url')]);
22+
$this->addColumn('position', ['label' => __('Sort Order')]);
23+
$this->_addAfter = false;
24+
$this->_addButtonLabel = __('Add Custom Link');
25+
}
26+
}

Model/StaticLinkers.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/**
4+
* A Magento 2 module named DevBera/CmsLinkToMenu
5+
* Copyright (C) 2019 Copyright 2019 © amitbera.com. All Rights Reserved
6+
*
7+
* This file included in DevBera/CmsLinkToMenu is licensed under OSL 3.0
8+
*
9+
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10+
* Please see LICENSE.txt for the full text of the OSL 3.0 license
11+
*/
12+
namespace DevBera\CmsLinkToMenu\Model;
13+
14+
use Magento\Framework\Data\Tree\NodeFactory;
15+
use Magento\Store\Model\ScopeInterface;
16+
use DevBera\CmsLinkToMenu\Model\System\Config\Backend\CustomerLinks\LinkProcessor;
17+
18+
class StaticLinkers
19+
{
20+
21+
const XML_PATH_CMSLINKTOMENU_CUSTOM_LINKS = 'cmslinktomenu/custom_links';
22+
23+
/**
24+
* @var \DevBera\CmsLinkToMenu\Model\System\Config\Backend\CustomerLinks\LinkProcessor
25+
*/
26+
private $customLinkProcessor;
27+
28+
/**
29+
* @var \Psr\Log\LoggerInterface
30+
*/
31+
private $logger;
32+
33+
/**
34+
* @var \Magento\Store\Model\StoreManagerInterface
35+
*/
36+
private $storeManager;
37+
38+
/**
39+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
40+
*/
41+
private $scopeConfig;
42+
43+
/**
44+
* @var \Magento\Framework\UrlInterface
45+
*/
46+
private $urlBuilder;
47+
48+
/**
49+
* @var NodeFactory
50+
*/
51+
private $nodeFactory;
52+
53+
public function __construct(
54+
NodeFactory $nodeFactory,
55+
\Magento\Store\Model\StoreManagerInterface $storeManager,
56+
\Magento\Framework\UrlInterface $urlBuilder,
57+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
58+
LinkProcessor $customLinkProcessor,
59+
\Psr\Log\LoggerInterface $logger
60+
) {
61+
62+
$this->nodeFactory = $nodeFactory;
63+
$this->urlBuilder = $urlBuilder;
64+
$this->scopeConfig = $scopeConfig;
65+
$this->storeManager = $storeManager;
66+
$this->logger = $logger;
67+
$this->customLinkProcessor = $customLinkProcessor;
68+
}
69+
70+
public function addCustomLinks($subject, $position = 'left')
71+
{
72+
$linksList = $this->getCustomLinksWithSortOrder($position);
73+
74+
if ($linksList && !empty($linksList)) {
75+
$i = 0;
76+
foreach ($linksList as $link) {
77+
$subject->getMenu()->addChild(
78+
$this->buildMenuItem($link, $subject, $position, $i++)
79+
);
80+
}
81+
}
82+
}
83+
84+
private function getCustomLinksWithSortOrder($position = 'left')
85+
{
86+
$fieldValue = $this->scopeConfig->getValue(
87+
self::XML_PATH_CMSLINKTOMENU_CUSTOM_LINKS.'/'.$position,
88+
ScopeInterface::SCOPE_STORE
89+
);
90+
$fieldValue = $this->customLinkProcessor->getValueInArray($fieldValue);
91+
92+
if (is_array($fieldValue) && !empty($fieldValue)) {
93+
$menuItems = $this->buildArray($fieldValue);
94+
return $menuItems;
95+
}
96+
return false;
97+
}
98+
99+
private function buildArray($value)
100+
{
101+
$result = [];
102+
foreach ($value as $eachRow) {
103+
if (!is_array($eachRow) || !array_key_exists('link_text', $eachRow)
104+
|| !array_key_exists('link_url', $eachRow)
105+
|| !array_key_exists('position', $eachRow)
106+
) {
107+
continue;
108+
}
109+
110+
$result[] = [
111+
'link_text' => $eachRow['link_text'],
112+
'link_url' => $eachRow['link_url'],
113+
'position' => $eachRow['position']
114+
];
115+
}
116+
117+
if (!empty($result)) {
118+
usort($result, function ($a, $b) {
119+
return $a['position'] <=> $b['position'];
120+
});
121+
}
122+
123+
return $result;
124+
}
125+
126+
private function buildMenuItem($data, $subject, $position, $itemPosition)
127+
{
128+
$node = $this->nodeFactory->create(
129+
[
130+
'data' => [
131+
'name' => $this->getPageTitle($data['link_text']),
132+
'id' => 'static-links-'.$position.'-'.$itemPosition,
133+
'url' => $this->getLinkUrl($data),
134+
'has_active' => false,
135+
'is_active' => false
136+
],
137+
'idField' => 'id',
138+
'tree' => $subject->getMenu()->getTree()
139+
]
140+
);
141+
return $node;
142+
}
143+
144+
private function getPageTitle($title)
145+
{
146+
return __($title);
147+
}
148+
private function getLinkUrl($data)
149+
{
150+
return $this->urlBuilder->getUrl(null, ['_direct' => $data['link_url']]);
151+
}
152+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/**
4+
* A Magento 2 module named DevBera/CmsLinkToMenu
5+
* Copyright (C) 2019 Copyright 2019 © amitbera.com. All Rights Reserved
6+
*
7+
* This file included in DevBera/CmsLinkToMenu is licensed under OSL 3.0
8+
*
9+
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10+
* Please see LICENSE.txt for the full text of the OSL 3.0 license
11+
*/
12+
13+
namespace DevBera\CmsLinkToMenu\Model\System\Config\Backend;
14+
15+
class CustomLinks extends \Magento\Framework\App\Config\Value
16+
{
17+
18+
/**
19+
* @var \DevBera\CmsLinkToMenu\Model\System\Config\Backend\CustomerLinks\LinkProcessor
20+
*/
21+
private $linkProcessor;
22+
23+
public function __construct(
24+
\Magento\Framework\Model\Context $context,
25+
\Magento\Framework\Registry $registry,
26+
\Magento\Framework\App\Config\ScopeConfigInterface $config,
27+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
28+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
29+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
30+
\DevBera\CmsLinkToMenu\Model\System\Config\Backend\CustomerLinks\LinkProcessor $linkProcessor,
31+
array $data = []
32+
) {
33+
parent::__construct(
34+
$context,
35+
$registry,
36+
$config,
37+
$cacheTypeList,
38+
$resource,
39+
$resourceCollection,
40+
$data
41+
);
42+
$this->linkProcessor = $linkProcessor;
43+
}
44+
45+
/**
46+
* Make Value Json encode and save able
47+
*/
48+
public function beforeSave()
49+
{
50+
$value = $this->getValue();
51+
$value = $this->linkProcessor->buildValueForSave($value);
52+
$this->setValue($value);
53+
}
54+
55+
/**
56+
* Convert value to Array format from Json type
57+
*/
58+
protected function _afterLoad()
59+
{
60+
$value = $this->getValue();
61+
$value = $this->linkProcessor->buildFieldToArrayType($value);
62+
$this->setValue($value);
63+
}
64+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/**
4+
* A Magento 2 module named DevBera/CmsLinkToMenu
5+
* Copyright (C) 2019 Copyright 2019 © amitbera.com. All Rights Reserved
6+
*
7+
* This file included in DevBera/CmsLinkToMenu is licensed under OSL 3.0
8+
*
9+
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10+
* Please see LICENSE.txt for the full text of the OSL 3.0 license
11+
*/
12+
13+
namespace DevBera\CmsLinkToMenu\Model\System\Config\Backend\CustomerLinks;
14+
15+
class LinkProcessor
16+
{
17+
18+
/**
19+
* @var \Magento\Framework\Math\Random
20+
*/
21+
private $mathRandom;
22+
23+
/**
24+
* @var \Magento\Framework\Serialize\Serializer\Json
25+
*/
26+
private $jsonSerializer;
27+
28+
/**
29+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
30+
*/
31+
private $scopeConfig;
32+
33+
public function __construct(
34+
\Magento\Framework\Serialize\Serializer\Json $jsonSerializer,
35+
\Magento\Framework\Math\Random $mathRandom,
36+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
37+
) {
38+
39+
$this->scopeConfig = $scopeConfig;
40+
$this->jsonSerializer = $jsonSerializer;
41+
$this->mathRandom = $mathRandom;
42+
}
43+
44+
public function buildValueForSave($value)
45+
{
46+
if ($this->isFieldValueValidate($value)) {
47+
$value = $this->makeJsonSerializeAbleValue($value);
48+
}
49+
$value = $this->jsonSerializer->serialize($value);
50+
return $value;
51+
}
52+
/**
53+
* Make an array for during load of fields
54+
* @return type array
55+
*/
56+
public function buildFieldToArrayType($value)
57+
{
58+
$value = $this->makeJsonUnSerializeAbleValue($value);
59+
$value = $this->makeEncodeArrayForLoad($value);
60+
return $value;
61+
}
62+
63+
private function isFieldValueValidate($value)
64+
{
65+
if (!is_array($value)) {
66+
return false;
67+
}
68+
69+
unset($value['__empty']);
70+
71+
foreach ($value as $eachRow) {
72+
if (!is_array($eachRow) || !array_key_exists('link_text', $eachRow)
73+
|| !array_key_exists('link_url', $eachRow)
74+
) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
81+
private function makeJsonSerializeAbleValue($value)
82+
{
83+
84+
$result = [];
85+
unset($value['__empty']);
86+
87+
foreach ($value as $eachRow) {
88+
89+
if (!is_array($eachRow) || !array_key_exists('link_text', $eachRow)
90+
|| !array_key_exists('link_url', $eachRow)
91+
) {
92+
continue;
93+
}
94+
95+
$position = (array_key_exists('position', $eachRow))
96+
&& (empty($eachRow['position']) === false) ? $eachRow['position'] : 0;
97+
98+
$result[] = [
99+
'link_text' => $eachRow['link_text'],
100+
'link_url' => $eachRow['link_url'],
101+
'position' => $position
102+
];
103+
}
104+
return $result;
105+
}
106+
107+
private function makeJsonUnSerializeAbleValue($value)
108+
{
109+
if (is_string($value) && !empty($value)) {
110+
return $this->jsonSerializer->unserialize($value);
111+
}
112+
return [];
113+
}
114+
115+
private function makeEncodeArrayForLoad($value)
116+
{
117+
$result = [];
118+
if ($this->isFieldValueValidate($value)) {
119+
foreach ($value as $eachRow) {
120+
$uniqueHashId = $this->mathRandom->getUniqueHash('_');
121+
$result[$uniqueHashId] = [
122+
'link_text' => $eachRow['link_text'],
123+
'link_url' => $eachRow['link_url'],
124+
'position' => $eachRow['position']
125+
];
126+
}
127+
}
128+
129+
return $result;
130+
}
131+
public function getValueInArray($value)
132+
{
133+
$value = $this->makeJsonUnSerializeAbleValue($value);
134+
if (!$this->isFieldValueValidate($value)) {
135+
return [];
136+
}
137+
return $value;
138+
}
139+
}

0 commit comments

Comments
 (0)