1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-link |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\link\types; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Element; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use flipbox\craft\link\fields\Link; |
15
|
|
|
use yii\base\Model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractElement extends AbstractType implements TypeInterface |
22
|
|
|
{ |
23
|
|
|
use ElementTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The base template path to the field type templates |
27
|
|
|
*/ |
28
|
|
|
const BASE_TEMPLATE_PATH = AbstractType::BASE_TEMPLATE_PATH . '/element'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The settings template path |
32
|
|
|
*/ |
33
|
|
|
const SETTINGS_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/settings'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The input template path |
37
|
|
|
*/ |
38
|
|
|
const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $config = []) |
44
|
|
|
{ |
45
|
|
|
// If useTargetSite is in here, but empty, then disregard targetSiteId |
46
|
|
|
if (array_key_exists('useTargetSite', $config)) { |
47
|
|
|
if (empty($config['useTargetSite'])) { |
48
|
|
|
unset($config['targetSiteId']); |
49
|
|
|
} |
50
|
|
|
unset($config['useTargetSite']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
parent::__construct($config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function getElementUrl(): string |
60
|
|
|
{ |
61
|
|
|
if (!$element = $this->getElement()) { |
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
return (string)$element->getUrl(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function getElementText(): string |
71
|
|
|
{ |
72
|
|
|
/** @var Element $element */ |
73
|
|
|
if (!$element = $this->getElement()) { |
74
|
|
|
return ''; |
75
|
|
|
} |
76
|
|
|
return (string)$element->title; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
* @param ElementInterface|null $element |
82
|
|
|
* @throws \Twig_Error_Loader |
83
|
|
|
* @throws \yii\base\Exception |
84
|
|
|
*/ |
85
|
|
|
public function inputHtml(Link $field, ElementInterface $element = null): string |
86
|
|
|
{ |
87
|
|
|
return Craft::$app->getView()->renderTemplate( |
88
|
|
|
static::INPUT_TEMPLATE_PATH, |
89
|
|
|
[ |
90
|
|
|
'field' => $field, |
91
|
|
|
'type' => $this, |
92
|
|
|
'input' => $this->inputTemplateVariables($field, $element) |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the HTML for the Target Site setting. |
99
|
|
|
* |
100
|
|
|
* @return string|null |
101
|
|
|
*/ |
102
|
|
|
public function getTargetSiteFieldHtml() |
103
|
|
|
{ |
104
|
|
|
/** @var Element $class */ |
105
|
|
|
$class = static::elementType(); |
106
|
|
|
|
107
|
|
|
if (!Craft::$app->getIsMultiSite() || !$class::isLocalized()) { |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$type = mb_strtolower(static::displayName()); |
112
|
|
|
$showTargetSite = !empty($this->targetSiteId); |
113
|
|
|
|
114
|
|
|
$html = Craft::$app->getView()->renderTemplateMacro( |
115
|
|
|
'_includes/forms', |
116
|
|
|
'checkboxField', |
117
|
|
|
[ |
118
|
|
|
[ |
119
|
|
|
'label' => Craft::t('app', 'Relate {type} from a specific site?', ['type' => $type]), |
120
|
|
|
'name' => 'useTargetSite', |
121
|
|
|
'checked' => $showTargetSite, |
122
|
|
|
'toggle' => 'target-site-container' |
123
|
|
|
] |
124
|
|
|
] |
125
|
|
|
) . |
126
|
|
|
'<div id="target-site-container"' . (!$showTargetSite ? ' class="hidden"' : '') . '>'; |
127
|
|
|
|
128
|
|
|
$siteOptions = []; |
129
|
|
|
|
130
|
|
|
foreach (Craft::$app->getSites()->getAllSites() as $site) { |
131
|
|
|
$siteOptions[] = [ |
132
|
|
|
'label' => Craft::t('site', $site->name), |
133
|
|
|
'value' => $site->id |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$html .= Craft::$app->getView()->renderTemplateMacro( |
138
|
|
|
'_includes/forms', |
139
|
|
|
'selectField', |
140
|
|
|
[ |
141
|
|
|
[ |
142
|
|
|
'label' => Craft::t('app', 'Which site should {type} be related from?', ['type' => $type]), |
143
|
|
|
'id' => 'targetSiteId', |
144
|
|
|
'name' => 'targetSiteId', |
145
|
|
|
'options' => $siteOptions, |
146
|
|
|
'value' => $this->targetSiteId |
147
|
|
|
] |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$html .= '</div>'; |
152
|
|
|
|
153
|
|
|
return $html; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritdoc |
158
|
|
|
*/ |
159
|
|
|
public function rules() |
160
|
|
|
{ |
161
|
|
|
return array_merge( |
162
|
|
|
parent::rules(), |
163
|
|
|
[ |
164
|
|
|
[ |
165
|
|
|
[ |
166
|
|
|
'elementId' |
167
|
|
|
], |
168
|
|
|
'required', |
169
|
|
|
'on' => [ |
170
|
|
|
static::SCENARIO_INPUT |
171
|
|
|
] |
172
|
|
|
], |
173
|
|
|
[ |
174
|
|
|
[ |
175
|
|
|
'elementId' |
176
|
|
|
], |
177
|
|
|
'safe', |
178
|
|
|
'on' => [ |
179
|
|
|
Model::SCENARIO_DEFAULT |
180
|
|
|
] |
181
|
|
|
] |
182
|
|
|
] |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: