1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
6
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
7
|
|
|
* @license https://nystudio107.com/license |
|
|
|
|
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace nystudio107\retour\fields; |
11
|
|
|
|
12
|
|
|
use Craft; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use craft\base\Field; |
15
|
|
|
use craft\base\PreviewableFieldInterface; |
16
|
|
|
use craft\helpers\Json; |
17
|
|
|
use nystudio107\retour\Retour as RetourPlugin; |
18
|
|
|
use yii\db\Schema; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package Retour |
|
|
|
|
23
|
|
|
* @since 3.1.74 |
|
|
|
|
24
|
|
|
* |
25
|
|
|
* @property-read string $contentColumnType |
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class ShortLink extends Field implements PreviewableFieldInterface |
28
|
|
|
{ |
29
|
|
|
public $redirectSrcMatch = 'pathonly'; |
30
|
|
|
public $redirectHttpCode = 301; |
31
|
|
|
|
32
|
|
|
// Static Methods |
33
|
|
|
|
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
public static function displayName(): string |
40
|
|
|
{ |
41
|
|
|
return Craft::t('retour', 'Short Link'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Public Methods |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
|
|
|
|
50
|
|
|
public function getContentColumnType(): string |
51
|
|
|
{ |
52
|
|
|
return Schema::TYPE_TEXT; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
|
|
|
|
58
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
59
|
|
|
{ |
60
|
|
|
// Render the input template |
61
|
|
|
return Craft::$app->getView()->renderTemplate( |
62
|
|
|
'retour/_components/fields/ShortLink_input', |
63
|
|
|
[ |
64
|
|
|
'name' => $this->handle, |
65
|
|
|
'value' => $value, |
66
|
|
|
'field' => $this, |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public function getSettingsHtml() |
75
|
|
|
{ |
76
|
|
|
return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings', |
|
|
|
|
77
|
|
|
[ |
78
|
|
|
'field' => $this, |
79
|
|
|
]); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
public function afterElementSave(ElementInterface $element, bool $isNew) |
86
|
|
|
{ |
87
|
|
|
if ($element->getIsDraft()) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$value = $element->{$this->handle}; |
92
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($element); |
93
|
|
|
if (!empty($value)) { |
94
|
|
|
RetourPlugin::$plugin->redirects->enableElementRedirect($element, $value, $this->redirectSrcMatch, $this->redirectHttpCode); |
95
|
|
|
} |
96
|
|
|
parent::afterElementSave($element, $isNew); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
|
|
|
|
102
|
|
|
public function afterElementDelete(ElementInterface $element) |
103
|
|
|
{ |
104
|
|
|
if ($element->getIsDraft()) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($element, true); |
109
|
|
|
parent::afterElementDelete($element); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
|
|
|
|
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
|
|
|
|
115
|
|
|
public function getTableAttributeHtml($value, ElementInterface $element): string |
116
|
|
|
{ |
117
|
|
|
$decoded = Json::decodeIfJson($value); |
118
|
|
|
if ($decoded) { |
119
|
|
|
return $decoded['legacyUrl'] ?? ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Render the input template |
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|