|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CP Field Inspect plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Inspect field handles and easily edit field settings |
|
6
|
|
|
* |
|
7
|
|
|
* @link http://mmikkel.no |
|
8
|
|
|
* @copyright Copyright (c) 2017 Mats Mikkel Rummelhoff |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace mmikkel\cpfieldinspect; |
|
12
|
|
|
|
|
13
|
|
|
use Craft; |
|
14
|
|
|
use craft\base\Element; |
|
15
|
|
|
use craft\base\ElementInterface; |
|
16
|
|
|
use craft\base\Plugin; |
|
17
|
|
|
use craft\commerce\elements\Product; |
|
18
|
|
|
use craft\elements\Asset; |
|
19
|
|
|
use craft\elements\Category; |
|
20
|
|
|
use craft\elements\Entry; |
|
21
|
|
|
use craft\elements\GlobalSet; |
|
22
|
|
|
use craft\elements\User; |
|
23
|
|
|
use craft\events\DefineHtmlEvent; |
|
24
|
|
|
use craft\events\PluginEvent; |
|
25
|
|
|
use craft\helpers\UrlHelper; |
|
26
|
|
|
use craft\services\Plugins; |
|
27
|
|
|
|
|
28
|
|
|
use yii\base\Event; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Craft plugins are very much like little applications in and of themselves. We’ve made |
|
32
|
|
|
* it as simple as we can, but the training wheels are off. A little prior knowledge is |
|
33
|
|
|
* going to be required to write a plugin. |
|
34
|
|
|
* |
|
35
|
|
|
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, |
|
36
|
|
|
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. |
|
37
|
|
|
* |
|
38
|
|
|
* https://craftcms.com/docs/plugins/introduction |
|
39
|
|
|
* |
|
40
|
|
|
* @author Mats Mikkel Rummelhoff |
|
41
|
|
|
* @package CpFieldInspect |
|
42
|
|
|
* @since 1.0.0 |
|
43
|
|
|
* |
|
44
|
|
|
* |
|
45
|
|
|
* Plugin icon credit: CUSTOMIZE SEARCH by creative outlet from the Noun Project |
|
46
|
|
|
* |
|
47
|
|
|
*/ |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Class CpFieldInspect |
|
51
|
|
|
* @package mmikkel\cpfieldinspect |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
|
|
class CpFieldInspect extends Plugin |
|
55
|
|
|
{ |
|
56
|
|
|
// Static Properties |
|
57
|
|
|
// ========================================================================= |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Static property that is an instance of this plugin class so that it can be accessed via |
|
61
|
|
|
* CpFieldInspect::$plugin |
|
62
|
|
|
* |
|
63
|
|
|
* @var CpFieldInspect |
|
64
|
|
|
*/ |
|
65
|
|
|
public static $plugin; |
|
66
|
|
|
|
|
67
|
|
|
// Public Methods |
|
68
|
|
|
// ========================================================================= |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set our $plugin static property to this class so that it can be accessed via |
|
72
|
|
|
* CpFieldInspect::$plugin |
|
73
|
|
|
* |
|
74
|
|
|
* Called after the plugin class is instantiated; do any one-time initialization |
|
75
|
|
|
* here such as hooks and events. |
|
76
|
|
|
* |
|
77
|
|
|
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
|
78
|
|
|
* you do not need to load it in your init() method. |
|
79
|
|
|
* |
|
80
|
|
|
*/ |
|
81
|
|
|
public function init() |
|
82
|
|
|
{ |
|
83
|
|
|
parent::init(); |
|
84
|
|
|
self::$plugin = $this; |
|
85
|
|
|
|
|
86
|
|
|
$config = Craft::$app->getConfig()->getGeneral(); |
|
87
|
|
|
if (!$config->allowAdminChanges) { |
|
88
|
|
|
// Do nothing if admin changes aren't allowed |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$request = Craft::$app->getRequest(); |
|
93
|
|
|
if (!$request->getIsCpRequest() || $request->getIsConsoleRequest()) { |
|
94
|
|
|
// Also do nothing if this is a console or site request |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Handler: EVENT_AFTER_LOAD_PLUGINS |
|
99
|
|
|
Event::on( |
|
100
|
|
|
Plugins::class, |
|
101
|
|
|
Plugins::EVENT_AFTER_LOAD_PLUGINS, |
|
102
|
|
|
function () { |
|
103
|
|
|
$this->doIt(); |
|
104
|
|
|
} |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
Craft::info( |
|
108
|
|
|
Craft::t( |
|
109
|
|
|
'cp-field-inspect', |
|
110
|
|
|
'{name} plugin loaded', |
|
111
|
|
|
['name' => $this->name] |
|
112
|
|
|
), |
|
113
|
|
|
__METHOD__ |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Protected Methods |
|
118
|
|
|
// ========================================================================= |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @throws \yii\base\Exception |
|
122
|
|
|
* @throws \yii\base\InvalidConfigException |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function doIt() |
|
125
|
|
|
{ |
|
126
|
|
|
|
|
127
|
|
|
/** @var User|null $user */ |
|
128
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
|
129
|
|
|
if (!$user || !$user->admin || !$user->getPreference('showFieldHandles')) { |
|
|
|
|
|
|
130
|
|
|
// Do nothing if the user is not an admin, or if field handles aren't visible |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Render edit source links |
|
135
|
|
|
if (\version_compare(Craft::$app->getVersion(), '4.0', '<')) { |
|
136
|
|
|
|
|
137
|
|
|
// Legacy template hooks for entries, assets and categories on Craft 3.x |
|
138
|
|
|
Craft::$app->getView()->hook('cp.entries.edit.meta', function (array $context) { |
|
139
|
|
|
return $this->renderEditSourceLink($context); |
|
140
|
|
|
}); |
|
141
|
|
|
Craft::$app->getView()->hook('cp.assets.edit.meta', function (array $context) { |
|
142
|
|
|
return $this->renderEditSourceLink($context); |
|
143
|
|
|
}); |
|
144
|
|
|
Craft::$app->getView()->hook('cp.categories.edit.details', function (array $context) { |
|
145
|
|
|
return $this->renderEditSourceLink($context); |
|
146
|
|
|
}); |
|
147
|
|
|
|
|
148
|
|
|
} else { |
|
149
|
|
|
|
|
150
|
|
|
// Use the EVENT_DEFINE_META_FIELDS_HTML event to inject source buttons for Craft 4 |
|
151
|
|
|
Event::on( |
|
152
|
|
|
Element::class, |
|
153
|
|
|
Element::EVENT_DEFINE_META_FIELDS_HTML, |
|
154
|
|
|
function (DefineHtmlEvent $event) { |
|
155
|
|
|
if ($event->static) { |
|
156
|
|
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
$event->html .= $this->renderEditSourceLink(['element' => $event->sender]); |
|
159
|
|
|
} |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Hooks that work on Craft 3.x and 4.x |
|
164
|
|
|
Craft::$app->getView()->hook('cp.globals.edit.content', function (array $context) { |
|
165
|
|
|
return $this->renderEditSourceLink($context); |
|
166
|
|
|
}); |
|
167
|
|
|
Craft::$app->getView()->hook('cp.users.edit.details', function (array $context) { |
|
168
|
|
|
return $this->renderEditSourceLink($context); |
|
169
|
|
|
}); |
|
170
|
|
|
Craft::$app->getView()->hook('cp.commerce.product.edit.details', function (array $context) { |
|
171
|
|
|
return $this->renderEditSourceLink($context); |
|
172
|
|
|
}); |
|
173
|
|
|
|
|
174
|
|
|
$request = Craft::$app->getRequest(); |
|
175
|
|
|
$isAjax = $request->getIsAjax() || $request->getAcceptsJson(); |
|
176
|
|
|
|
|
177
|
|
|
if ($isAjax) { |
|
178
|
|
|
|
|
179
|
|
|
$segments = $request->getActionSegments(); |
|
180
|
|
|
if (empty($segments) || !\is_array($segments) || $segments[count($segments) - 1] !== 'get-editor-html') { |
|
181
|
|
|
return; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.initElementEditor();'); |
|
185
|
|
|
|
|
186
|
|
|
} else { |
|
187
|
|
|
|
|
188
|
|
|
$redirectUrl = \implode('?', \array_filter([\implode('/', $request->getSegments()), $request->getQueryStringWithoutPath()])); |
|
189
|
|
|
|
|
190
|
|
|
$data = [ |
|
191
|
|
|
'editFieldBtnLabel' => Craft::t('cp-field-inspect', 'Edit field settings'), |
|
192
|
|
|
'baseEditFieldUrl' => \rtrim(UrlHelper::cpUrl('settings/fields/edit'), '/'), |
|
193
|
|
|
'redirectUrl' => Craft::$app->getSecurity()->hashData($redirectUrl, Craft::$app->getConfig()->getGeneral()->securityKey), |
|
194
|
|
|
]; |
|
195
|
|
|
|
|
196
|
|
|
$fields = Craft::$app->getFields()->getAllFields('global'); |
|
197
|
|
|
foreach ($fields as $field) { |
|
198
|
|
|
$data['fields'][$field->handle] = (int)$field->id; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
Craft::$app->getView()->registerAssetBundle(CpFieldInspectBundle::class); |
|
202
|
|
|
Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.init(' . \json_encode($data) . ');'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return string |
|
208
|
|
|
* @throws \Twig\Error\LoaderError |
|
209
|
|
|
* @throws \Twig\Error\RuntimeError |
|
210
|
|
|
* @throws \Twig\Error\SyntaxError |
|
211
|
|
|
* @throws \Twig\Error\LoaderError |
|
212
|
|
|
* @throws \yii\base\Exception |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function renderEditSourceLink(array $context): string |
|
215
|
|
|
{ |
|
216
|
|
|
$element = $context['element'] ?? $context['entry'] ?? $context['asset'] ?? $context['globalSet'] ?? $context['user'] ?? $context['category'] ?? $context['product'] ?? null; |
|
217
|
|
|
if ($element instanceof Entry) { |
|
218
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-entry-type-link', ['entry' => $element]); |
|
219
|
|
|
} |
|
220
|
|
|
if ($element instanceof Asset) { |
|
221
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-volume-link', ['asset' => $element]); |
|
222
|
|
|
} |
|
223
|
|
|
if ($element instanceof GlobalSet) { |
|
224
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-globalset-link', ['globalSet' => $element]); |
|
225
|
|
|
} |
|
226
|
|
|
if ($element instanceof User) { |
|
227
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-users-link', ['user' => $element]); |
|
228
|
|
|
} |
|
229
|
|
|
if ($element instanceof Category) { |
|
230
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-category-group-link', ['category' => $element]); |
|
231
|
|
|
} |
|
232
|
|
|
if ($element instanceof Product) { |
|
233
|
|
|
return Craft::$app->getView()->renderTemplate('cp-field-inspect/edit-commerce-product-type-link', ['product' => $element]); |
|
234
|
|
|
} |
|
235
|
|
|
return ''; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|