|
11 | 11 |
|
12 | 12 | namespace fostercommerce\entrytyperules;
|
13 | 13 |
|
14 |
| -use fostercommerce\entrytyperules\services\EntryTypeRulesService as EntryTypeRulesServiceService; |
15 |
| -use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; |
16 |
| -use fostercommerce\entrytyperules\services\EntryTypeRulesService; |
17 |
| -use fostercommerce\entrytyperules\models\Settings; |
18 |
| - |
19 | 14 | use Craft;
|
| 15 | +use craft\base\Element; |
20 | 16 | use craft\base\Model;
|
| 17 | + |
21 | 18 | use craft\base\Plugin;
|
22 |
| -use craft\base\Element; |
| 19 | +use craft\events\DefineHtmlEvent; |
23 | 20 | use craft\services\Plugins;
|
24 |
| -use craft\events\PluginEvent; |
25 | 21 | use craft\web\View;
|
26 |
| -use craft\events\DefineHtmlEvent; |
| 22 | +use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; |
| 23 | +use fostercommerce\entrytyperules\models\Settings; |
| 24 | +use fostercommerce\entrytyperules\services\EntryTypeRulesService as EntryTypeRulesServiceService; |
27 | 25 | use yii\base\Event;
|
28 | 26 |
|
29 | 27 | /**
|
|
46 | 44 | */
|
47 | 45 | class EntryTypeRules extends Plugin
|
48 | 46 | {
|
49 |
| - // Static Properties |
50 |
| - // ========================================================================= |
51 |
| - |
52 |
| - /** |
53 |
| - * Static property that is an instance of this plugin class so that it can be accessed via |
54 |
| - * EntryTypeRules::$plugin |
55 |
| - * |
56 |
| - * @var EntryTypeRules |
57 |
| - */ |
58 |
| - public static $plugin; |
59 |
| - |
60 |
| - // Public Properties |
61 |
| - // ========================================================================= |
62 |
| - |
63 |
| - /** |
64 |
| - * To execute your plugin’s migrations, you’ll need to increase its schema version. |
65 |
| - * |
66 |
| - * @var string |
67 |
| - */ |
68 |
| - public string $schemaVersion = '1.0.0'; |
69 |
| - |
70 |
| - /** |
71 |
| - * Set to `true` if the plugin should have a settings view in the control panel. |
72 |
| - * |
73 |
| - * @var bool |
74 |
| - */ |
75 |
| - public bool $hasCpSettings = true; |
76 |
| - |
77 |
| - /** |
78 |
| - * Set to `true` if the plugin should have its own section (main nav item) in the control panel. |
79 |
| - * |
80 |
| - * @var bool |
81 |
| - */ |
82 |
| - public bool $hasCpSection = false; |
83 |
| - |
84 |
| - // Public Methods |
85 |
| - // ========================================================================= |
86 |
| - |
87 |
| - /** |
88 |
| - * Set our $plugin static property to this class so that it can be accessed via |
89 |
| - * EntryTypeRules::$plugin |
90 |
| - * |
91 |
| - * Called after the plugin class is instantiated; do any one-time initialization |
92 |
| - * here such as hooks and events. |
93 |
| - * |
94 |
| - * If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
95 |
| - * you do not need to load it in your init() method. |
96 |
| - * |
97 |
| - */ |
98 |
| - public function init(): void |
99 |
| - { |
100 |
| - parent::init(); |
101 |
| - self::$plugin = $this; |
102 |
| - |
103 |
| - Craft::setAlias('@plugin', $this->getBasePath()); |
104 |
| - |
105 |
| - // Let's put our own data regarding the section into the entry edit page in the CP |
106 |
| - Craft::$app->view->hook('cp.entries.edit.meta', function (array &$context) { |
107 |
| - $injectedHtml = ''; |
108 |
| - $entry = $context['entry']; |
109 |
| - if ($entry != null && $entry->section->type != 'single') { |
110 |
| - // Create the elements we are going to inject |
111 |
| - $injectedHtml = '<div id="entryTypeRulesSectionId" data-value="' . $entry->section->id . '"></div>'; |
112 |
| - } |
113 |
| - return $injectedHtml; |
114 |
| - }); |
115 |
| - |
116 |
| - // Watch the template rendering to see if we are in an entry edit form |
117 |
| - Event::on( |
118 |
| - View::class, |
119 |
| - View::EVENT_AFTER_RENDER_TEMPLATE, |
120 |
| - function () { |
121 |
| - // Check the segments to see if we are in an entry edit form, if so register the entry bundle |
122 |
| - if ( |
123 |
| - Craft::$app->getRequest()->isCpRequest && |
124 |
| - Craft::$app->getRequest()->getSegment(1) == 'entries' && |
125 |
| - Craft::$app->getRequest()->getSegment(3) != '' |
126 |
| - ) { |
127 |
| - // Inject our asset bundle, and start it up with some JS |
128 |
| - Craft::$app->getView()->registerAssetBundle(EntryTypeRulesAsset::class, View::POS_END); |
129 |
| - Craft::$app->getView()->registerJs('new Craft.EntryTypeRules();', View::POS_READY); |
130 |
| - } |
131 |
| - } |
132 |
| - ); |
133 |
| - |
134 |
| - // Watch the slide out element editor window to see if we are editing an entry in the slide-out |
135 |
| - Event::on( |
136 |
| - Element::class, |
137 |
| - Element::EVENT_DEFINE_SIDEBAR_HTML, |
138 |
| - function (DefineHtmlEvent $event) { |
139 |
| - $element = $event->sender; |
140 |
| - // If the element is a Craft Entry |
141 |
| - if (is_a($element, 'craft\elements\Entry')) { |
142 |
| - // Get the section ID and section type the entry belongs to |
143 |
| - $sectionId = $element->section->id; |
144 |
| - // If it is not a single, inject out fields and register the slideout bundle |
145 |
| - if ($element->section->type != 'single') { |
146 |
| - // Get the views namespace |
147 |
| - $viewNamespace = Craft::$app->getView()->namespace; |
148 |
| - // Create the elements we are going to inject (Note: the ID's will automatically be namespaced for the view by Craft) |
149 |
| - $injectedHtml = '<div id="entryTypeRulesSectionId" data-value="' . $sectionId . '"></div>'; |
150 |
| - // Inject the elements, our asset bundle, and start it up with some JS |
151 |
| - $event->html = $injectedHtml . $event->html; |
152 |
| - Craft::$app->getView()->registerAssetBundle(EntryTypeRulesAsset::class, View::POS_END); |
153 |
| - Craft::$app->getView()->registerJs('new Craft.EntryTypeRules("' . $viewNamespace . '");', View::POS_READY); |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - } |
158 |
| - ); |
159 |
| - |
160 |
| - /** |
161 |
| - * Logging in Craft involves using one of the following methods: |
162 |
| - * |
163 |
| - * Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. |
164 |
| - * Craft::info(): record a message that conveys some useful information. |
165 |
| - * Craft::warning(): record a warning message that indicates something unexpected has happened. |
166 |
| - * Craft::error(): record a fatal error that should be investigated as soon as possible. |
167 |
| - * |
168 |
| - * Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` |
169 |
| - * |
170 |
| - * It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets |
171 |
| - * the category to the method (prefixed with the fully qualified class name) where the constant appears. |
172 |
| - * |
173 |
| - * To enable the Yii debug toolbar, go to your user account in the AdminCP and check the |
174 |
| - * [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel |
175 |
| - * |
176 |
| - * http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html |
177 |
| - */ |
178 |
| - Craft::info( |
179 |
| - Craft::t( |
180 |
| - 'entry-type-rules', |
181 |
| - '{name} plugin loaded', |
182 |
| - ['name' => $this->name] |
183 |
| - ), |
184 |
| - __METHOD__ |
185 |
| - ); |
186 |
| - } |
187 |
| - |
188 |
| - /** |
189 |
| - * Intercepts the plugin settings page response so we can check the config override file |
190 |
| - * (if it exists) and so we can process the Post response in our own settings controller method |
191 |
| - * instead of using the general Craft settings HTML method to render the settings page. |
192 |
| - * @inheritdoc |
193 |
| - */ |
194 |
| - public function getSettingsResponse(): mixed |
195 |
| - { |
196 |
| - $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); |
197 |
| - |
198 |
| - return Craft::$app->controller->renderTemplate( |
199 |
| - 'entry-type-rules/settings', [ |
200 |
| - 'settings' => $this->getSettings(), |
201 |
| - 'overrides' => $overrides |
202 |
| - ] |
203 |
| - ); |
204 |
| - } |
205 |
| - |
206 |
| - |
207 |
| - // Protected Methods |
208 |
| - // ========================================================================= |
209 |
| - |
210 |
| - /** |
211 |
| - * Creates and returns the model used to store the plugin’s settings. |
212 |
| - * |
213 |
| - * @return Model|null |
214 |
| - */ |
215 |
| - protected function createSettingsModel(): ?Model |
216 |
| - { |
217 |
| - return new Settings(); |
218 |
| - } |
| 47 | + // Static Properties |
| 48 | + // ========================================================================= |
| 49 | + |
| 50 | + /** |
| 51 | + * Static property that is an instance of this plugin class so that it can be accessed via |
| 52 | + * EntryTypeRules::$plugin |
| 53 | + * |
| 54 | + * @var EntryTypeRules |
| 55 | + */ |
| 56 | + public static $plugin; |
| 57 | + |
| 58 | + // Public Properties |
| 59 | + // ========================================================================= |
| 60 | + |
| 61 | + /** |
| 62 | + * To execute your plugin’s migrations, you’ll need to increase its schema version. |
| 63 | + */ |
| 64 | + public string $schemaVersion = '1.0.0'; |
| 65 | + |
| 66 | + /** |
| 67 | + * Set to `true` if the plugin should have a settings view in the control panel. |
| 68 | + */ |
| 69 | + public bool $hasCpSettings = true; |
| 70 | + |
| 71 | + /** |
| 72 | + * Set to `true` if the plugin should have its own section (main nav item) in the control panel. |
| 73 | + */ |
| 74 | + public bool $hasCpSection = false; |
| 75 | + |
| 76 | + // Public Methods |
| 77 | + // ========================================================================= |
| 78 | + |
| 79 | + /** |
| 80 | + * Set our $plugin static property to this class so that it can be accessed via |
| 81 | + * EntryTypeRules::$plugin |
| 82 | + * |
| 83 | + * Called after the plugin class is instantiated; do any one-time initialization |
| 84 | + * here such as hooks and events. |
| 85 | + * |
| 86 | + * If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
| 87 | + * you do not need to load it in your init() method. |
| 88 | + */ |
| 89 | + public function init(): void |
| 90 | + { |
| 91 | + parent::init(); |
| 92 | + self::$plugin = $this; |
| 93 | + |
| 94 | + Craft::setAlias('@plugin', $this->getBasePath()); |
| 95 | + |
| 96 | + // Let's put our own data regarding the section into the entry edit page in the CP |
| 97 | + Craft::$app->view->hook('cp.entries.edit.meta', function (array &$context) { |
| 98 | + $injectedHtml = ''; |
| 99 | + $entry = $context['entry']; |
| 100 | + if ($entry !== null && $entry->section->type !== 'single') { |
| 101 | + // Create the elements we are going to inject |
| 102 | + $injectedHtml = '<div id="entryTypeRulesSectionId" data-value="' . $entry->section->id . '"></div>'; |
| 103 | + } |
| 104 | + return $injectedHtml; |
| 105 | + }); |
| 106 | + |
| 107 | + // Watch the template rendering to see if we are in an entry edit form |
| 108 | + Event::on( |
| 109 | + View::class, |
| 110 | + View::EVENT_AFTER_RENDER_TEMPLATE, |
| 111 | + function () { |
| 112 | + // Check the segments to see if we are in an entry edit form, if so register the entry bundle |
| 113 | + if ( |
| 114 | + Craft::$app->getRequest()->isCpRequest && |
| 115 | + Craft::$app->getRequest()->getSegment(1) === 'entries' && |
| 116 | + Craft::$app->getRequest()->getSegment(3) !== '' |
| 117 | + ) { |
| 118 | + // Inject our asset bundle, and start it up with some JS |
| 119 | + Craft::$app->getView()->registerAssetBundle(EntryTypeRulesAsset::class, View::POS_END); |
| 120 | + Craft::$app->getView()->registerJs('new Craft.EntryTypeRules();', View::POS_READY); |
| 121 | + } |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + // Watch the slide out element editor window to see if we are editing an entry in the slide-out |
| 126 | + Event::on( |
| 127 | + Element::class, |
| 128 | + Element::EVENT_DEFINE_SIDEBAR_HTML, |
| 129 | + function (DefineHtmlEvent $event) { |
| 130 | + $element = $event->sender; |
| 131 | + // If the element is a Craft Entry |
| 132 | + if (is_a($element, 'craft\elements\Entry')) { |
| 133 | + // Get the section ID and section type the entry belongs to |
| 134 | + $sectionId = $element->section->id; |
| 135 | + // If it is not a single, inject out fields and register the slideout bundle |
| 136 | + if ($element->section->type !== 'single') { |
| 137 | + // Get the views namespace |
| 138 | + $viewNamespace = Craft::$app->getView()->namespace; |
| 139 | + // Create the elements we are going to inject (Note: the ID's will automatically be namespaced for the view by Craft) |
| 140 | + $injectedHtml = '<div id="entryTypeRulesSectionId" data-value="' . $sectionId . '"></div>'; |
| 141 | + // Inject the elements, our asset bundle, and start it up with some JS |
| 142 | + $event->html = $injectedHtml . $event->html; |
| 143 | + Craft::$app->getView()->registerAssetBundle(EntryTypeRulesAsset::class, View::POS_END); |
| 144 | + Craft::$app->getView()->registerJs('new Craft.EntryTypeRules("' . $viewNamespace . '");', View::POS_READY); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + ); |
| 149 | + |
| 150 | + /** |
| 151 | + * Logging in Craft involves using one of the following methods: |
| 152 | + * |
| 153 | + * Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. |
| 154 | + * Craft::info(): record a message that conveys some useful information. |
| 155 | + * Craft::warning(): record a warning message that indicates something unexpected has happened. |
| 156 | + * Craft::error(): record a fatal error that should be investigated as soon as possible. |
| 157 | + * |
| 158 | + * Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` |
| 159 | + * |
| 160 | + * It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets |
| 161 | + * the category to the method (prefixed with the fully qualified class name) where the constant appears. |
| 162 | + * |
| 163 | + * To enable the Yii debug toolbar, go to your user account in the AdminCP and check the |
| 164 | + * [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel |
| 165 | + * |
| 166 | + * http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html |
| 167 | + */ |
| 168 | + Craft::info( |
| 169 | + Craft::t( |
| 170 | + 'entry-type-rules', |
| 171 | + '{name} plugin loaded', |
| 172 | + [ |
| 173 | + 'name' => $this->name, |
| 174 | + ] |
| 175 | + ), |
| 176 | + __METHOD__ |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Intercepts the plugin settings page response so we can check the config override file |
| 182 | + * (if it exists) and so we can process the Post response in our own settings controller method |
| 183 | + * instead of using the general Craft settings HTML method to render the settings page. |
| 184 | + * @inheritdoc |
| 185 | + */ |
| 186 | + public function getSettingsResponse(): mixed |
| 187 | + { |
| 188 | + $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); |
| 189 | + |
| 190 | + return Craft::$app->controller->renderTemplate( |
| 191 | + 'entry-type-rules/settings', |
| 192 | + [ |
| 193 | + 'settings' => $this->getSettings(), |
| 194 | + 'overrides' => $overrides, |
| 195 | + ] |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + // Protected Methods |
| 201 | + // ========================================================================= |
| 202 | + |
| 203 | + /** |
| 204 | + * Creates and returns the model used to store the plugin’s settings. |
| 205 | + */ |
| 206 | + protected function createSettingsModel(): ?Model |
| 207 | + { |
| 208 | + return new Settings(); |
| 209 | + } |
219 | 210 | }
|
0 commit comments