|
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\craft\link; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Plugin; |
|
12
|
|
|
use craft\events\RegisterComponentTypesEvent; |
|
13
|
|
|
use craft\helpers\ArrayHelper; |
|
14
|
|
|
use craft\services\Fields; |
|
15
|
|
|
use flipbox\craft\link\events\RegisterLinkTypes; |
|
16
|
|
|
use flipbox\craft\link\fields\Link as LinkField; |
|
17
|
|
|
use flipbox\craft\link\types\Asset; |
|
18
|
|
|
use flipbox\craft\link\types\Category; |
|
19
|
|
|
use flipbox\craft\link\types\Entry; |
|
20
|
|
|
use flipbox\craft\link\types\TypeInterface; |
|
21
|
|
|
use flipbox\craft\link\types\Url; |
|
22
|
|
|
use flipbox\craft\link\types\User; |
|
23
|
|
|
use yii\base\Event; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Flipbox Factory <[email protected]> |
|
27
|
|
|
* @since 1.0.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Link extends Plugin |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* The event name |
|
33
|
|
|
*/ |
|
34
|
|
|
const EVENT_REGISTER_TYPES = 'registerTypes'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The first party link types |
|
38
|
|
|
*/ |
|
39
|
|
|
const FIRST_PARTY_TYPES = [ |
|
40
|
|
|
Asset::class, |
|
41
|
|
|
Category::class, |
|
42
|
|
|
Entry::class, |
|
43
|
|
|
Url::class, |
|
44
|
|
|
User::class |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var TypeInterface[] |
|
49
|
|
|
*/ |
|
50
|
|
|
private $types; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function init() |
|
56
|
|
|
{ |
|
57
|
|
|
parent::init(); |
|
58
|
|
|
|
|
59
|
|
|
// Register our fields |
|
60
|
|
|
Event::on( |
|
61
|
|
|
Fields::class, |
|
62
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
|
63
|
|
|
function (RegisterComponentTypesEvent $event) { |
|
64
|
|
|
$event->types[] = LinkField::class; |
|
65
|
|
|
} |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function findAllTypes() |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->types === null) { |
|
75
|
|
|
$this->types = $this->registerTypes(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->types; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $class |
|
83
|
|
|
* @return TypeInterface|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function findType(string $class) |
|
86
|
|
|
{ |
|
87
|
|
|
return ArrayHelper::getValue( |
|
88
|
|
|
$this->findAllTypes(), |
|
89
|
|
|
$class |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function registerTypes() |
|
97
|
|
|
{ |
|
98
|
|
|
$event = new RegisterLinkTypes( |
|
99
|
|
|
[ |
|
100
|
|
|
'types' => static::FIRST_PARTY_TYPES |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$this->trigger( |
|
105
|
|
|
self::EVENT_REGISTER_TYPES, |
|
106
|
|
|
$event |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return $this->resolveTypes($event->types); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $types |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
private function resolveTypes(array $types) |
|
117
|
|
|
{ |
|
118
|
|
|
$validTypes = []; |
|
119
|
|
|
foreach ($types as $type) { |
|
120
|
|
|
if (!$type instanceof TypeInterface) { |
|
121
|
|
|
$type = new $type(); |
|
122
|
|
|
} |
|
123
|
|
|
$validTypes[get_class($type)] = $type; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $validTypes; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Translates a message to the specified language. |
|
132
|
|
|
* |
|
133
|
|
|
* This is a shortcut method of [[\Craft::t()]]. |
|
134
|
|
|
* |
|
135
|
|
|
* The translation will be conducted according to the message category and the target language will be used. |
|
136
|
|
|
* |
|
137
|
|
|
* You can add parameters to a translation message that will be substituted with the corresponding value after |
|
138
|
|
|
* translation. The format for this is to use curly brackets around the parameter name as you can see in the |
|
139
|
|
|
* following example: |
|
140
|
|
|
* |
|
141
|
|
|
* ```php |
|
142
|
|
|
* $username = 'Alexander'; |
|
143
|
|
|
* echo \flipbox\craft\link\Link::t('Hello, {username}!', ['username' => $username]); |
|
144
|
|
|
* ``` |
|
145
|
|
|
* |
|
146
|
|
|
* Further formatting of message parameters is supported using the [PHP |
|
147
|
|
|
* intl...craft-link/src/web/assets/settings/dist/LinkConfiguration.min.jsgit add * |
|
148
|
|
|
* extensions](http://www.php.net/manual/en/intro.intl.php) |
|
149
|
|
|
* message formatter. See [[\Craft::t()]] for more details. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $message the message to be translated. |
|
152
|
|
|
* @param array $params the parameters that will be used to replace the corresponding |
|
153
|
|
|
* placeholders in...craft-link/src/web/assets/settings/dist/LinkConfiguration.min.js |
|
154
|
|
|
* the message. |
|
155
|
|
|
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current |
|
156
|
|
|
* [[\yii\base\Application::language|application language]] will be used. |
|
157
|
|
|
* @return string the translated message. |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function t($message, $params = [], $language = null) |
|
160
|
|
|
{ |
|
161
|
|
|
return \Craft::t('link', $message, $params, $language); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|