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; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use craft\elements\Asset as AssetElement; |
14
|
|
|
use craft\fields\Assets; |
15
|
|
|
use craft\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
* |
21
|
|
|
* @method AssetElement findElement() |
22
|
|
|
*/ |
23
|
|
|
class Asset extends Assets implements TypeInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
use traits\Element; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function init() |
32
|
|
|
{ |
33
|
|
|
parent::init(); |
34
|
|
|
$this->identifier = 'asset'; |
35
|
|
|
$this->applyDefaultProperties(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public static function displayName(): string |
42
|
|
|
{ |
43
|
|
|
return Craft::t('link', 'Asset'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function getElementText(): string |
50
|
|
|
{ |
51
|
|
|
if (!$element = $this->findElement()) { |
52
|
|
|
return ''; |
53
|
|
|
} |
54
|
|
|
return $element->title; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function getUrl(): string |
61
|
|
|
{ |
62
|
|
|
if (!$element = $this->findElement()) { |
63
|
|
|
return ''; |
64
|
|
|
} |
65
|
|
|
return (string) $element->getUrl(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
protected function lookupElementById(int $id) |
72
|
|
|
{ |
73
|
|
|
return Craft::$app->getAssets()->getAssetById($id); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function settings(): array |
80
|
|
|
{ |
81
|
|
|
// Get setting attributes from component |
82
|
|
|
$settings = $this->settingsAttributes(); |
83
|
|
|
|
84
|
|
|
// Remove the public 'text' attribute (it's not a setting) |
85
|
|
|
ArrayHelper::removeValue($settings, 'text'); |
86
|
|
|
|
87
|
|
|
return $settings; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
protected function inputTemplateVariables($value = null, ElementInterface $element = null): array |
94
|
|
|
{ |
95
|
|
|
return parent::inputTemplateVariables( |
96
|
|
|
$this->findElement() ? [$this->findElement()] : null, |
97
|
|
|
$element |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|