Skip to content

Commit be43fb1

Browse files
committed
widgets revrited on using virtual types
1 parent eda6fe8 commit be43fb1

File tree

10 files changed

+215
-270
lines changed

10 files changed

+215
-270
lines changed

Block/Widget/Link.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\View\Element\Template;
12+
use Magefan\Blog\Model\Url;
13+
14+
/**
15+
* Class Link
16+
*/
17+
class Link extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
18+
{
19+
20+
const CLASS_AUTHOR = 'author';
21+
/**
22+
* @var string
23+
*/
24+
protected $_href;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $_anchorText;
30+
31+
/**
32+
* @var Url
33+
*/
34+
protected $urlFinder;
35+
36+
/**
37+
* @var null
38+
*/
39+
protected $modelRepository;
40+
41+
/**
42+
* @var null
43+
*/
44+
protected $model = null;
45+
46+
/**
47+
* AdstractLink constructor.
48+
* @param Template\Context $context
49+
* @param Url $urlFinder
50+
* @param null $modelRepository
51+
* @param array $data
52+
*/
53+
public function __construct(
54+
Template\Context $context,
55+
Url $urlFinder,
56+
$modelRepository,
57+
array $data = []
58+
) {
59+
parent::__construct($context, $data);
60+
$this->urlFinder = $urlFinder;
61+
$this->modelRepository = $modelRepository;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getHref()
68+
{
69+
if ($this->_href === null && $this->model) {
70+
if ($this->model->getData('identifier') && $this->model->getControllerName() != self::CLASS_AUTHOR && $this->model->getControllerName()) {
71+
$this->_href = $this->urlFinder->getUrlPath($this->model->getData('identifier'), $this->model->getControllerName());
72+
} elseif ($this->model->getControllerName() == self::CLASS_AUTHOR) {
73+
$this->_href = $this->model->getUrl();
74+
}
75+
}
76+
return $this->_href;
77+
}
78+
79+
/**
80+
* @return mixed|string
81+
*/
82+
public function getLabel()
83+
{
84+
if (!$this->_anchorText) {
85+
if ($this->getData('anchor_text')) {
86+
$this->_anchorText = $this->getData('anchor_text');
87+
} elseif ($this->model && $this->model->getControllerName() != self::CLASS_AUTHOR && $this->model->getData('title')) {
88+
$this->_anchorText = $this->model->getData('title');
89+
} elseif ($this->model && $this->model->getData('meta-title')) {
90+
$this->_anchorText = $this->model->getData('meta-title');
91+
} elseif ($this->model->getControllerName() == self::CLASS_AUTHOR) {
92+
$this->_anchorText = $this->model->getTitle();
93+
}
94+
}
95+
return $this->_anchorText;
96+
}
97+
98+
/**
99+
* @return string
100+
*/
101+
protected function _toHtml()
102+
{
103+
try {
104+
if ($this->getData('entity_id')) {
105+
$this->model = $this->getRepository()->getbyId($this->getData('entity_id'));
106+
}
107+
if (!$this->getHref()) {
108+
return '<b>' . $this->escapeHtml($this->getLabel()) . '</b>';
109+
} else {
110+
return '<a href="' . $this->getHref() . '" title="' . $this->getData('anchor_title') . '">' . $this->escapeHtml($this->getLabel()) . '</a>';
111+
}
112+
return '';
113+
114+
} catch (NoSuchEntityException $e) {
115+
return '';
116+
// return '<b>There is no object with such link</b>';
117+
}
118+
}
119+
120+
/**
121+
* @return \Magento\Framework\App\ObjectManager
122+
*/
123+
private function ObjectManager()
124+
{
125+
return $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
126+
}
127+
128+
/**
129+
* @return mixed|null
130+
*/
131+
private function getRepository()
132+
{
133+
if (is_array($this->modelRepository)) {
134+
$this->modelRepository = $this->ObjectManager()->get($this->modelRepository['instance']);
135+
}
136+
return $this->modelRepository;
137+
}
138+
139+
}

Block/Widget/Link/AdstractLink.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

Block/Widget/Link/AuthorLink.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

Block/Widget/Link/CategoryLink.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

Block/Widget/Link/PostLink.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)