|
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
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
use craft\base\Plugin; |
|
16
|
|
|
use craft\services\Plugins; |
|
17
|
|
|
use craft\events\PluginEvent; |
|
18
|
|
|
|
|
19
|
|
|
use craft\helpers\UrlHelper; |
|
20
|
|
|
|
|
21
|
|
|
use yii\base\Event; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Craft plugins are very much like little applications in and of themselves. We’ve made |
|
25
|
|
|
* it as simple as we can, but the training wheels are off. A little prior knowledge is |
|
26
|
|
|
* going to be required to write a plugin. |
|
27
|
|
|
* |
|
28
|
|
|
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, |
|
29
|
|
|
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. |
|
30
|
|
|
* |
|
31
|
|
|
* https://craftcms.com/docs/plugins/introduction |
|
32
|
|
|
* |
|
33
|
|
|
* @author Mats Mikkel Rummelhoff |
|
34
|
|
|
* @package CpFieldInspect |
|
35
|
|
|
* @since 1.0.0 |
|
36
|
|
|
* |
|
37
|
|
|
* |
|
38
|
|
|
* Plugin icon credit: CUSTOMIZE SEARCH by creative outlet from the Noun Project |
|
39
|
|
|
* |
|
40
|
|
|
*/ |
|
41
|
|
|
class CpFieldInspect extends Plugin |
|
42
|
|
|
{ |
|
43
|
|
|
// Static Properties |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Static property that is an instance of this plugin class so that it can be accessed via |
|
48
|
|
|
* CpFieldInspect::$plugin |
|
49
|
|
|
* |
|
50
|
|
|
* @var CpFieldInspect |
|
51
|
|
|
*/ |
|
52
|
|
|
public static $plugin; |
|
53
|
|
|
|
|
54
|
|
|
// Public Methods |
|
55
|
|
|
// ========================================================================= |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set our $plugin static property to this class so that it can be accessed via |
|
59
|
|
|
* CpFieldInspect::$plugin |
|
60
|
|
|
* |
|
61
|
|
|
* Called after the plugin class is instantiated; do any one-time initialization |
|
62
|
|
|
* here such as hooks and events. |
|
63
|
|
|
* |
|
64
|
|
|
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
|
65
|
|
|
* you do not need to load it in your init() method. |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function init() |
|
69
|
|
|
{ |
|
70
|
|
|
parent::init(); |
|
71
|
|
|
self::$plugin = $this; |
|
72
|
|
|
|
|
73
|
|
|
$user = Craft::$app->getUser(); |
|
74
|
|
|
$request = Craft::$app->getRequest(); |
|
75
|
|
|
|
|
76
|
|
|
if (!$user->getIsAdmin() || !$request->getIsCpRequest() || $request->getIsConsoleRequest()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Handler: EVENT_AFTER_LOAD_PLUGINS |
|
81
|
|
|
Event::on( |
|
82
|
|
|
Plugins::class, |
|
83
|
|
|
Plugins::EVENT_AFTER_LOAD_PLUGINS, |
|
84
|
|
|
function () { |
|
85
|
|
|
$this->doIt(); |
|
86
|
|
|
} |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Logging in Craft involves using one of the following methods: |
|
91
|
|
|
* |
|
92
|
|
|
* Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. |
|
93
|
|
|
* Craft::info(): record a message that conveys some useful information. |
|
94
|
|
|
* Craft::warning(): record a warning message that indicates something unexpected has happened. |
|
95
|
|
|
* Craft::error(): record a fatal error that should be investigated as soon as possible. |
|
96
|
|
|
* |
|
97
|
|
|
* Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` |
|
98
|
|
|
* |
|
99
|
|
|
* It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets |
|
100
|
|
|
* the category to the method (prefixed with the fully qualified class name) where the constant appears. |
|
101
|
|
|
* |
|
102
|
|
|
* To enable the Yii debug toolbar, go to your user account in the AdminCP and check the |
|
103
|
|
|
* [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel |
|
104
|
|
|
* |
|
105
|
|
|
* http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html |
|
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
|
|
|
protected function doIt() |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
|
|
$request = Craft::$app->getRequest(); |
|
123
|
|
|
|
|
124
|
|
|
if ($request->getIsAjax()) { |
|
125
|
|
|
|
|
126
|
|
|
if (!$request->getIsPost()) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$segments = $request->segments; |
|
131
|
|
|
$actionSegment = $segments[count($segments) - 1]; |
|
132
|
|
|
|
|
133
|
|
|
if ($actionSegment !== 'get-editor-html') { |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.initElementEditor();'); |
|
138
|
|
|
|
|
139
|
|
|
} else { |
|
140
|
|
|
|
|
141
|
|
|
$data = array( |
|
142
|
|
|
'redirectUrl' => Craft::$app->getSecurity()->hashData(implode('/', $request->segments)), |
|
143
|
|
|
'fields' => array(), |
|
144
|
|
|
'entryTypeIds' => array(), |
|
145
|
|
|
'baseEditFieldUrl' => rtrim(UrlHelper::cpUrl('settings/fields/edit'), '/'), |
|
146
|
|
|
'baseEditEntryTypeUrl' => rtrim(UrlHelper::cpUrl('settings/sections/sectionId/entrytypes'), '/'), |
|
147
|
|
|
'baseEditGlobalSetUrl' => rtrim(UrlHelper::cpUrl('settings/globals'), '/'), |
|
148
|
|
|
'baseEditCategoryGroupUrl' => rtrim(UrlHelper::cpUrl('settings/categories'), '/'), |
|
149
|
|
|
'baseEditCommerceProductTypeUrl' => rtrim(UrlHelper::cpUrl('commerce/settings/producttypes'), '/'), |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
$sectionIds = Craft::$app->getSections()->getAllSectionIds(); |
|
153
|
|
|
foreach ($sectionIds as $sectionId) |
|
154
|
|
|
{ |
|
155
|
|
|
$entryTypes = Craft::$app->getSections()->getEntryTypesBySectionId($sectionId); |
|
156
|
|
|
$data['entryTypeIds']['' . $sectionId] = array(); |
|
157
|
|
|
foreach ($entryTypes as $entryType) |
|
158
|
|
|
{ |
|
159
|
|
|
$data['entryTypeIds']['' . $sectionId][] = $entryType->id; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$fields = Craft::$app->getFields()->getAllFields(); |
|
165
|
|
|
|
|
166
|
|
|
foreach ($fields as $field) |
|
167
|
|
|
{ |
|
168
|
|
|
|
|
169
|
|
|
$data['fields'][$field->handle] = array( |
|
170
|
|
|
'id' => $field->id, |
|
171
|
|
|
'handle' => $field->handle, |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
Craft::$app->getView()->registerAssetBundle(CpFieldInspectBundle::class); |
|
176
|
|
|
Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.init('.json_encode($data).');'); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|