Skip to content

Commit eda6fe8

Browse files
committed
added link widgets for blog post, category, tag, author
1 parent 29cf244 commit eda6fe8

File tree

6 files changed

+328
-0
lines changed

6 files changed

+328
-0
lines changed

Block/Widget/Link/AdstractLink.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Widget\Link;
9+
10+
use Magento\Framework\View\Element\Template;
11+
use Magefan\Blog\Model\Url;
12+
13+
/**
14+
* Class Link
15+
*/
16+
abstract class AdstractLink extends \Magento\Framework\View\Element\Html\Link implements \Magento\Widget\Block\BlockInterface
17+
{
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $_href;
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $_anchorText;
28+
29+
/**
30+
* @var Url
31+
*/
32+
protected $urlFinder;
33+
34+
/**
35+
* @var null
36+
*/
37+
protected $className;
38+
39+
/**
40+
* AdstractLink constructor.
41+
* @param Template\Context $context
42+
* @param Url $urlFinder
43+
* @param null $className
44+
* @param array $data
45+
*/
46+
public function __construct(
47+
Template\Context $context,
48+
Url $urlFinder,
49+
$className = null,
50+
array $data = []
51+
) {
52+
parent::__construct($context, $data);
53+
$this->urlFinder = $urlFinder;
54+
$this->className = $className;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getHref()
61+
{
62+
if ($this->_href === null) {
63+
if (!$this->getData('identifier')) {
64+
throw new \RuntimeException('Parameter post_identifier is not set.');
65+
}
66+
$href = false;
67+
68+
$href = $this->urlFinder->getUrlPath($this->getData('identifier'), $this->className);
69+
70+
$this->_href = $href;
71+
}
72+
return $this->_href;
73+
}
74+
75+
/**
76+
* @return mixed|string
77+
*/
78+
public function getLabel()
79+
{
80+
if (!$this->_anchorText) {
81+
if ($this->getData('anchor_text')) {
82+
$this->_anchorText = $this->getData('anchor_text');
83+
} else {
84+
$this->_anchorText = 'Link';
85+
}
86+
}
87+
88+
return $this->_anchorText;
89+
}
90+
91+
/**
92+
* @return string
93+
*/
94+
protected function _toHtml()
95+
{
96+
if($this->getHref()) {
97+
return parent::_toHtml();
98+
}
99+
return '';
100+
}
101+
}

Block/Widget/Link/AuthorLink.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Widget\Link;
9+
10+
use Magento\Framework\View\Element\Template;
11+
use Magefan\Blog\Model\Url;
12+
13+
/**
14+
* Class Link for Author
15+
*/
16+
class AuthorLink extends \Magefan\Blog\Block\Widget\Link\AdstractLink
17+
{
18+
19+
/**
20+
* AuthorLink constructor.
21+
* @param Template\Context $context
22+
* @param Url $urlFinder
23+
* @param string $className
24+
* @param array $data
25+
*/
26+
public function __construct(
27+
Template\Context $context,
28+
Url $urlFinder,
29+
$className = 'author',
30+
array $data = []
31+
) {
32+
parent::__construct(
33+
$context,
34+
$urlFinder,
35+
$className,
36+
$data
37+
);
38+
}
39+
}

Block/Widget/Link/CategoryLink.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Widget\Link;
9+
10+
use Magento\Framework\View\Element\Template;
11+
use Magefan\Blog\Model\Url;
12+
13+
/**
14+
* Class Link for Category
15+
*/
16+
class CategoryLink extends \Magefan\Blog\Block\Widget\Link\AdstractLink
17+
{
18+
19+
/**
20+
* CategoryLink constructor.
21+
* @param Template\Context $context
22+
* @param Url $urlFinder
23+
* @param string $className
24+
* @param array $data
25+
*/
26+
public function __construct(
27+
Template\Context $context,
28+
Url $urlFinder,
29+
$className = 'category',
30+
array $data = []
31+
) {
32+
parent::__construct(
33+
$context,
34+
$urlFinder,
35+
$className,
36+
$data
37+
);
38+
}
39+
}

Block/Widget/Link/PostLink.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Widget\Link;
9+
10+
use Magento\Framework\View\Element\Template;
11+
use Magefan\Blog\Model\Url;
12+
13+
/**
14+
* Class Link for post
15+
*/
16+
class PostLink extends \Magefan\Blog\Block\Widget\Link\AdstractLink
17+
{
18+
19+
/**
20+
* PostLink constructor.
21+
* @param Template\Context $context
22+
* @param Url $urlFinder
23+
* @param string $className
24+
* @param array $data
25+
*/
26+
public function __construct(
27+
Template\Context $context,
28+
Url $urlFinder,
29+
$className = 'post',
30+
array $data = []
31+
) {
32+
parent::__construct(
33+
$context,
34+
$urlFinder,
35+
$className,
36+
$data
37+
);
38+
}
39+
}

Block/Widget/Link/TagLink.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Widget\Link;
9+
10+
use Magento\Framework\View\Element\Template;
11+
use Magefan\Blog\Model\Url;
12+
13+
/**
14+
* Class Link for Tag
15+
*/
16+
class TagLink extends \Magefan\Blog\Block\Widget\Link\AdstractLink
17+
{
18+
19+
/**
20+
* TagLink constructor.
21+
* @param Template\Context $context
22+
* @param Url $urlFinder
23+
* @param string $className
24+
* @param array $data
25+
*/
26+
public function __construct(
27+
Template\Context $context,
28+
Url $urlFinder,
29+
$className = 'tag',
30+
array $data = []
31+
) {
32+
parent::__construct(
33+
$context,
34+
$urlFinder,
35+
$className,
36+
$data
37+
);
38+
}
39+
}

etc/widget.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,75 @@
7070
</parameter>
7171
</parameters>
7272
</widget>
73+
74+
<widget id="blog_post_link"
75+
class="Magefan\Blog\Block\Widget\Link\PostLink">
76+
<label translate="true">Blog Post Link</label>
77+
<description translate="true">Link to a Specified Post</description>
78+
<parameters>
79+
<parameter name="anchor_text" xsi:type="text" visible="true">
80+
<label translate="true">Anchor Custom Text</label>
81+
</parameter>
82+
<parameter name="title" xsi:type="text" visible="true">
83+
<label translate="true">Anchor Custom Title</label>
84+
</parameter>
85+
<parameter name="identifier" xsi:type="text" visible="true">
86+
<label translate="true">Post Identifier</label>
87+
</parameter>
88+
</parameters>
89+
</widget>
90+
91+
<widget id="blog_category_link"
92+
class="Magefan\Blog\Block\Widget\Link\CategoryLink">
93+
<label translate="true">Blog Category Link</label>
94+
<description translate="true">Link to a Specified Category</description>
95+
<parameters>
96+
<parameter name="anchor_text" xsi:type="text" visible="true">
97+
<label translate="true">Anchor Custom Text</label>
98+
</parameter>
99+
<parameter name="title" xsi:type="text" visible="true">
100+
<label translate="true">Anchor Custom Title</label>
101+
</parameter>
102+
<parameter name="identifier" xsi:type="text" visible="true">
103+
<label translate="true">Category Identifier</label>
104+
</parameter>
105+
</parameters>
106+
</widget>
107+
108+
<widget id="blog_tag_link"
109+
class="Magefan\Blog\Block\Widget\Link\TagLink">
110+
<label translate="true">Blog Tag Link</label>
111+
<description translate="true">Link to a Specified Tag</description>
112+
<parameters>
113+
<parameter name="anchor_text" xsi:type="text" visible="true">
114+
<label translate="true">Anchor Custom Text</label>
115+
</parameter>
116+
<parameter name="title" xsi:type="text" visible="true">
117+
<label translate="true">Anchor Custom Title</label>
118+
</parameter>
119+
<parameter name="identifier" xsi:type="text" visible="true">
120+
<label translate="true">Tag Identifier</label>
121+
</parameter>
122+
</parameters>
123+
</widget>
124+
125+
<widget id="blog_author_link"
126+
class="Magefan\Blog\Block\Widget\Link\AuthorLink">
127+
<label translate="true">Blog Author Link</label>
128+
<description translate="true">Link to a Specified Author</description>
129+
<parameters>
130+
<parameter name="anchor_text" xsi:type="text" visible="true">
131+
<label translate="true">Anchor Custom Text</label>
132+
</parameter>
133+
<parameter name="title" xsi:type="text" visible="true">
134+
<label translate="true">Anchor Custom Title</label>
135+
</parameter>
136+
<parameter name="identifier" xsi:type="text" visible="true">
137+
<label translate="true">Author Identifier</label>
138+
<description translate="true"><![CDATA[
139+
Enter author name. Exp: "firstname-lastname".
140+
]]></description>
141+
</parameter>
142+
</parameters>
143+
</widget>
73144
</widgets>

0 commit comments

Comments
 (0)