1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/link/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/link/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\link\types\traits; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use flipbox\link\fields\Link; |
14
|
|
|
use flipbox\link\types\TypeInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
trait Element |
21
|
|
|
{ |
22
|
|
|
use Base; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
protected function applyDefaultProperties() |
28
|
|
|
{ |
29
|
|
|
$this->handle = 'elementId'; |
|
|
|
|
30
|
|
|
$this->id = static::class; |
|
|
|
|
31
|
|
|
$this->allowLimit = false; |
|
|
|
|
32
|
|
|
$this->limit = 1; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
public $overrideText = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $elementId; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ElementInterface |
47
|
|
|
*/ |
48
|
|
|
protected $element; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
abstract protected function getElementText(): string; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $id |
57
|
|
|
* @return ElementInterface|null |
58
|
|
|
*/ |
59
|
|
|
abstract protected function lookupElementById(int $id); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public function getElementId() |
65
|
|
|
{ |
66
|
|
|
return $this->elementId; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return ElementInterface|null |
71
|
|
|
*/ |
72
|
|
|
protected function findElement() |
73
|
|
|
{ |
74
|
|
|
if ($this->element === null) { |
75
|
|
|
$this->element = $this->lookupElement() ?: false; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->element === false ? null : $this->element; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ElementInterface|null |
83
|
|
|
*/ |
84
|
|
|
protected function lookupElement() |
85
|
|
|
{ |
86
|
|
|
if ($this->elementId === null) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->lookupElementById($this->elementId); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function properties(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'elementId', |
100
|
|
|
'text' |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $elementId |
106
|
|
|
*/ |
107
|
|
|
public function setElementId($elementId) |
108
|
|
|
{ |
109
|
|
|
if (is_array($elementId)) { |
110
|
|
|
$elementId = reset($elementId); |
111
|
|
|
} |
112
|
|
|
$this->elementId = (int)$elementId; |
113
|
|
|
|
114
|
|
|
// Clear element cache on change |
115
|
|
|
if ($this->element === false || |
116
|
|
|
($this->element && $this->element->getId() !== $this->elementId) |
117
|
|
|
) { |
118
|
|
|
$this->element = null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
|
|
public function inputHtml(Link $field, TypeInterface $type = null, ElementInterface $element = null): string |
126
|
|
|
{ |
127
|
|
|
return Craft::$app->getView()->renderTemplate( |
128
|
|
|
'link/_components/fieldtypes/Link/types/element/input', |
129
|
|
|
[ |
130
|
|
|
'value' => $type, |
131
|
|
|
'field' => $field, |
132
|
|
|
'type' => $this, |
133
|
|
|
'elementSelectInput' => $this->getInputHtml($type, $element) |
|
|
|
|
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
|
|
public function settingsHtml(): string |
142
|
|
|
{ |
143
|
|
|
return Craft::$app->getView()->renderTemplate( |
144
|
|
|
'link/_components/fieldtypes/Link/types/element/settings', |
145
|
|
|
[ |
146
|
|
|
'type' => $this, |
147
|
|
|
'elementSelectSettings' => $this->getSettingsHtml() |
|
|
|
|
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritdoc |
154
|
|
|
*/ |
155
|
|
|
public function getText(): string |
156
|
|
|
{ |
157
|
|
|
if ($this->text) { |
158
|
|
|
return $this->text; |
159
|
|
|
} |
160
|
|
|
return $this->getElementText(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: