Skip to content

Commit 5abf017

Browse files
authored
Merge pull request #1 from devamitbera/develop
Merge 1.3 to master
2 parents dbc3672 + fd04d43 commit 5abf017

27 files changed

+882
-767
lines changed

Api/MenuLinkManagementInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* A Magento 2 module named DevBera/CmsLinkToMenu
4+
* Copyright (C) 2019 Copyright 2019 © amitbera.com. All Rights Reserved
5+
*
6+
* This file included in DevBera/CmsLinkToMenu is licensed under OSL 3.0
7+
*
8+
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
* Please see LICENSE.txt for the full text of the OSL 3.0 license
10+
*/
11+
12+
13+
namespace DevBera\CmsLinkToMenu\Api;
14+
15+
interface MenuLinkManagementInterface
16+
{
17+
/**
18+
* @param \Magento\Theme\Block\Html\Topmenu $subject
19+
* @param string $position
20+
* @return void
21+
*/
22+
public function addLinks($subject, $position = 'left');
23+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 CmsPageCustomLinker extends AbstractFieldArray
17+
{
18+
19+
const FIELDS_EXTRA_CLASS_PREFIX = 'devbera-cpcl-';
20+
21+
const FIELD_TEMPLATE = 'DevBera_CmsLinkToMenu::system/config/form/field/array.phtml';
22+
23+
private $linkTypesRenderer;
24+
25+
private $cmsPagesRenderer;
26+
/**
27+
* @var \DevBera\CmsLinkToMenu\Model\Config\Source\Pages
28+
*/
29+
private $cmsPages;
30+
31+
public function __construct(
32+
\Magento\Backend\Block\Template\Context $context,
33+
\DevBera\CmsLinkToMenu\Model\Config\Source\Pages $cmsPages,
34+
array $data = []
35+
) {
36+
parent::__construct($context, $data);
37+
$this->cmsPages = $cmsPages;
38+
}
39+
40+
protected function _prepareToRender(): void
41+
{
42+
$this->addColumn(
43+
'link_type',
44+
['label' => __('Link Type'), 'renderer' => $this->getLinkTypesRenderer()]
45+
);
46+
$this->addColumn(
47+
'page_id',
48+
['label' => __('Cms Pages'), 'renderer' => $this->getCmsPagesRenderer()]
49+
);
50+
$this->addColumn('link_text', ['label' => __('Text')]);
51+
$this->addColumn('link_url', ['label' => __('Url'),'class' => $this->getLinkUrlExtraHtmlClass()]);
52+
$this->addColumn('position', ['label' => __('Sort Order')]);
53+
$this->_addAfter = false;
54+
$this->_addButtonLabel = __('Add Cms Page / Custom link');
55+
}
56+
57+
private function getCmsPagesRenderer()
58+
{
59+
if (!$this->cmsPagesRenderer) {
60+
$this->cmsPagesRenderer = $this->getLayout()->createBlock(
61+
\DevBera\CmsLinkToMenu\Block\Adminhtml\Form\Field\Pages::class,
62+
'',
63+
['data' => ['is_render_to_js_template' => true]]
64+
);
65+
$this->cmsPagesRenderer->setClass('customer_group_select '.$this->getPageIdExtraHtmlClass());
66+
}
67+
return $this->cmsPagesRenderer ;
68+
}
69+
70+
private function getLinkTypesRenderer()
71+
{
72+
if (!$this->linkTypesRenderer) {
73+
$this->linkTypesRenderer = $this->getLayout()->createBlock(
74+
\DevBera\CmsLinkToMenu\Block\Adminhtml\Form\Field\LinkTypes::class,
75+
'',
76+
['data' => ['is_render_to_js_template' => true]]
77+
);
78+
$this->linkTypesRenderer->setClass('customer_group_select '.$this->getLinkTypeExtraHtmlClass());
79+
}
80+
return $this->linkTypesRenderer;
81+
}
82+
83+
/**
84+
* Prepare existing row data object
85+
*
86+
* @param \Magento\Framework\DataObject $row
87+
* @return void
88+
*/
89+
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
90+
{
91+
$optionExtraAttr = [];
92+
93+
$optionExtraAttr['option_' . $this->linkTypesRenderer->calcOptionHash($row->getData('link_type'))] =
94+
'selected="selected"';
95+
$optionExtraAttr['option_' . $this->getCmsPagesRenderer()->calcOptionHash($row->getData('page_id'))] =
96+
'selected="selected"';
97+
98+
$row->setData(
99+
'option_extra_attrs',
100+
$optionExtraAttr
101+
);
102+
}
103+
104+
public function getTemplate()
105+
{
106+
return self::FIELD_TEMPLATE;
107+
}
108+
109+
public function getInputFieldHtmlId()
110+
{
111+
return $this->getHtmlId();
112+
}
113+
114+
private function getFieldExtraClassName($fieldName)
115+
{
116+
return self::FIELDS_EXTRA_CLASS_PREFIX.$fieldName;
117+
}
118+
119+
public function getLinkTypeExtraHtmlClass()
120+
{
121+
return $this->getFieldExtraClassName('link_type');
122+
}
123+
public function getPageIdExtraHtmlClass()
124+
{
125+
return $this->getFieldExtraClassName('page_id');
126+
}
127+
128+
public function getLinkUrlExtraHtmlClass()
129+
{
130+
return $this->getFieldExtraClassName('link_url');
131+
}
132+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* A Magento 2 module named DevBera/CmsLinkToMenu
5+
* Copyright (C) 2019 Copyright 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\Block\Adminhtml\Form\Field;
14+
15+
class LinkTypes extends \Magento\Framework\View\Element\Html\Select
16+
{
17+
18+
/**
19+
* @var \DevBera\CmsLinkToMenu\Model\Config\Source\LinkTypes
20+
*/
21+
private $configLinkTypes;
22+
23+
public function __construct(
24+
\DevBera\CmsLinkToMenu\Model\Config\Source\LinkTypes $configLinkTypes,
25+
\Magento\Framework\View\Element\Context $context,
26+
array $data = []
27+
) {
28+
parent::__construct($context, $data);
29+
$this->configLinkTypes = $configLinkTypes;
30+
}
31+
public function _toHtml()
32+
{
33+
// @codingStandardsIgnoreStart
34+
if (!$this->getOptions()) {
35+
foreach ($this->configLinkTypes->getOptions() as $linkValue => $linkType) {
36+
$this->addOption($linkValue, addslashes($linkType));
37+
}
38+
}
39+
// @codingStandardsIgnoreEnd
40+
return parent::_toHtml();
41+
}
42+
/**
43+
* @param string $value
44+
* @return $this
45+
*/
46+
public function setInputName($value)
47+
{
48+
return $this->setName($value);
49+
}
50+
}

Block/Adminhtml/Form/Field/StaticLinks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class StaticLinks extends AbstractFieldArray
1717
{
1818
protected function _prepareToRender(): void
1919
{
20+
2021
$this->addColumn('link_text', ['label' => __('Text')]);
2122
$this->addColumn('link_url', ['label' => __('Url')]);
2223
$this->addColumn('position', ['label' => __('Sort Order')]);

Model/Config/Source/LinkTypes.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Config\Source;
14+
15+
class LinkTypes implements \Magento\Framework\Option\ArrayInterface
16+
{
17+
18+
private $options;
19+
20+
public function toOptionArray(): array
21+
{
22+
if (!$this->options) {
23+
$this->options[] = ['value' => 1,'label'=> __('Cms Page')];
24+
$this->options[] = ['value' => 2,'label'=> __('Custom Link /Static Link')];
25+
}
26+
return $this->options;
27+
}
28+
29+
public function getOptions()
30+
{
31+
$result = [];
32+
$options = $this->toOptionArray();
33+
34+
foreach ($options as $option) {
35+
$result[$option['value']] = $option['label'];
36+
}
37+
return $result;
38+
}
39+
}

0 commit comments

Comments
 (0)