Skip to content

Commit fbacb0b

Browse files
committed
Refactor plugin to use 'diffbase' handle and templates
Updated plugin handle from '_diffbase' to 'diffbase' in composer.json and throughout the codebase. Refactored template paths and URL rules to use 'diffbase'. Improved Plugin and CpController initialization, settings management, and API key generation logic. Simplified settings.twig to redirect to the main plugin page.
1 parent fd8ed3b commit fbacb0b

File tree

7 files changed

+126
-160
lines changed

7 files changed

+126
-160
lines changed

README.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
# diff. base module
22

33
## Installation
4-
- Copy module to ```/modules```
5-
- Open ```config/app.php``` and initialize module by adding
64

7-
```php
8-
'modules' => [
9-
'diffbase' => modules\diffbase\Module::class,
10-
],
11-
'bootstrap' => ['diffbase'],
5+
```
6+
composer require "digitaldiff/diffbase" -w && php craft plugin/install diffbase
127
```
138

14-
- Go to Settings/diff. base module and generate a API key
9+
- Go to plugin settings and generate a API key
1510
- Test the API Key by going to ```yourdomain.com/api/info?key=```
1611
- Copy the API key to flow.diff.ch/admin and make a new entry with the API key and the site url
1712

1813
## Compatibility
19-
Only works/tested with Craft CMS 5.x
20-
21-
## Troubleshooting
22-
If you get a error at the Login screen, make sure that in the root composer.json the modules are initialized by adding:
23-
24-
```json
25-
"autoload": {
26-
"psr-4": {
27-
"modules\\": "modules/"
28-
}
29-
},
30-
```
14+
Only works/tested with Craft CMS 5.x

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"extra": {
2626
"name": "diff. base module",
27-
"handle": "_diffbase",
27+
"handle": "diffbase",
2828
"developer": "diff. Kommunikation AG",
2929
"documentationUrl": "https://github.com/digitaldiff/diffbase/blob/main/README.md",
3030
"changelogUrl": "https://raw.githubusercontent.com/digitaldiff/diffbase/main/CHANGELOG.md",

src/Plugin.php

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace digitaldiff\diffbase;
34

45
use Craft;
@@ -9,101 +10,132 @@
910
use craft\events\RegisterCpNavItemsEvent;
1011
use craft\events\RegisterTemplateRootsEvent;
1112
use craft\web\View;
13+
use Twig\Error\LoaderError;
14+
use Twig\Error\RuntimeError;
15+
use Twig\Error\SyntaxError;
1216
use yii\base\Event;
1317
use digitaldiff\diffbase\models\Settings;
1418
use digitaldiff\diffbase\services\ApiService;
19+
use yii\base\Exception;
1520

21+
/**
22+
* Main plugin class for the "diff. base plugin".
23+
*
24+
* This class handles the initialization of the plugin, including:
25+
* - Registering template paths
26+
* - Defining custom URL rules for the site and control panel
27+
* - Adding a custom navigation item to the control panel
28+
* - Providing a settings model and rendering the settings page
29+
*/
1630
class Plugin extends BasePlugin
1731
{
32+
/**
33+
* @var string The schema version of the plugin.
34+
*/
1835
public string $schemaVersion = '1.0.0';
19-
// public bool $hasCpSettings = true;
20-
// public bool $hasCpSection = true;
2136

37+
/**
38+
* @var bool Indicates whether the plugin has a control panel settings page.
39+
*/
40+
public bool $hasCpSettings = true;
41+
42+
/**
43+
* Configures the plugin's components.
44+
*
45+
* @return array The configuration array for the plugin's components.
46+
*/
2247
public static function config(): array
2348
{
2449
return [
2550
'components' => [
26-
'apiService' => ApiService::class,
51+
'apiService' => ApiService::class, // Registers the ApiService for business logic
2752
],
2853
];
2954
}
3055

31-
public function init()
56+
/**
57+
* Initializes the plugin.
58+
*
59+
* This method is called automatically when the plugin is loaded. It sets up
60+
* aliases, registers template paths, defines URL rules, and adds a control panel
61+
* navigation item.
62+
*/
63+
public function init(): void
3264
{
3365
parent::init();
3466

67+
// Set an alias for the plugin's base path
3568
Craft::setAlias('@digitaldiff/diffbase', $this->getBasePath());
3669

37-
// Template-Pfad registrieren
70+
// Register the template path for the plugin
3871
Event::on(
3972
View::class,
40-
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
73+
View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
4174
function(RegisterTemplateRootsEvent $event) {
42-
$event->roots['_diffbase'] = $this->getBasePath() . '/templates';
75+
$event->roots['diffbase'] = __DIR__ . 'src/templates'; // Registers the template directory
4376
}
4477
);
4578

46-
// Site URL-Regeln für API
79+
// Register site URL rules for the plugin's API
4780
Event::on(
4881
UrlManager::class,
4982
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
5083
function (RegisterUrlRulesEvent $event) {
51-
$event->rules['api/info'] = '_diffbase/api/info';
84+
$event->rules['api/info'] = 'diffbase/api/info'; // Maps 'api/info' to the ApiController
5285
}
5386
);
5487

55-
// CP URL-Regeln
88+
// Register control panel URL rules for the plugin
5689
Event::on(
5790
UrlManager::class,
5891
UrlManager::EVENT_REGISTER_CP_URL_RULES,
5992
function (RegisterUrlRulesEvent $event) {
60-
$event->rules['_diffbase'] = '_diffbase/cp/index';
61-
$event->rules['_diffbase/settings'] = '_diffbase/cp/settings';
93+
$event->rules['diffbase'] = 'diffbase/cp/index'; // Maps 'diffbase' to CpController::actionIndex
94+
$event->rules['diffbase/test'] = 'diffbase/cp/test'; // Maps 'diffbase/test' to CpController::actionTest
95+
$event->rules['diffbase/<action:\w+>'] = 'diffbase/cp/<action>'; // Maps dynamic actions
6296
}
6397
);
6498

99+
// Add a custom navigation item to the control panel
65100
Event::on(
66101
Cp::class,
67102
Cp::EVENT_REGISTER_CP_NAV_ITEMS,
68-
function (RegisterCpNavItemsEvent $event) {
103+
function(RegisterCpNavItemsEvent $event) {
69104
$event->navItems[] = [
70-
'url' => '_diffbase',
71-
'label' => 'diff. base plugin',
72-
'icon' => '@digitaldiff/diffbase/icon-mask.svg',
73-
'weight' => 2, // Position im Hauptmenü
105+
'label' => 'diff. base plugin', // Label for the navigation item
106+
'url' => 'diffbase', // URL for the navigation item
107+
'icon' => '@digitaldiff/diffbase/icon-mask.svg', // Icon for the navigation item
108+
'weight' => 3, // Position in the navigation menu
74109
];
75110
}
76111
);
77-
78-
/* // CP Navigation
79-
Event::on(
80-
Cp::class,
81-
Cp::EVENT_REGISTER_CP_NAV_ITEMS,
82-
function (RegisterCpNavItemsEvent $event) {
83-
foreach ($event->navItems as &$item) {
84-
if ($item['url'] === 'settings') {
85-
$item['subnav']['_diffbase'] = [
86-
'label' => 'DiffBase',
87-
'url' => '_diffbase',
88-
'selected' => Craft::$app->getRequest()->getSegment(1) === '_diffbase'
89-
];
90-
break;
91-
}
92-
}
93-
}
94-
);*/
95112
}
96113

114+
/**
115+
* Creates the settings model for the plugin.
116+
*
117+
* @return Settings The settings model instance.
118+
*/
97119
protected function createSettingsModel(): Settings
98120
{
99121
return new Settings();
100122
}
101123

124+
/**
125+
* Renders the settings page for the plugin.
126+
*
127+
* @return string|null The rendered HTML for the settings page.
128+
*
129+
* @throws SyntaxError If there is a syntax error in the Twig template.
130+
* @throws Exception If an error occurs during rendering.
131+
* @throws RuntimeError If a runtime error occurs in the Twig template.
132+
* @throws LoaderError If the Twig template cannot be loaded.
133+
*/
102134
protected function settingsHtml(): ?string
103135
{
104-
return Craft::$app->getView()->renderTemplate('_diffbase/settings', [
105-
'plugin' => $this,
106-
'settings' => $this->getSettings(),
136+
return Craft::$app->getView()->renderTemplate('diffbase/settings', [
137+
'plugin' => $this, // Passes the plugin instance to the template
138+
'settings' => $this->getSettings(), // Passes the settings model to the template
107139
]);
108140
}
109141
}

src/controllers/CpController.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
class CpController extends Controller
2020
{
21+
2122
/**
2223
* @throws NotSupportedException
2324
* @throws InvalidConfigException
@@ -30,23 +31,34 @@ class CpController extends Controller
3031
*/
3132
public function actionIndex(): \yii\web\Response
3233
{
33-
$settings = $this->getSettings();
34+
$plugin = Plugin::getInstance();
35+
if (!$plugin) {
36+
throw new \Exception('Plugin nicht initialisiert');
37+
}
38+
39+
$settings = $plugin->getSettings();
40+
if (!$settings) {
41+
// Fallback: neue Settings erstellen
42+
$settings = new Settings();
43+
}
3444

3545
// API-Key automatisch generieren wenn keiner vorhanden
3646
if (!$settings->apiKey) {
3747
$settings->apiKey = $settings->generateApiKey();
38-
Craft::$app->getProjectConfig()->set('_diffbase.apiKey', $settings->apiKey);
48+
Craft::$app->plugins->savePluginSettings($plugin, ['apiKey' => $settings->apiKey]);
3949
Craft::$app->getSession()->setNotice('API-Key wurde automatisch generiert.');
4050
}
4151

42-
return $this->renderTemplate('_diffbase/index', [
52+
return $this->renderTemplate('diffbase/index', [
4353
'settings' => $settings
4454
]);
4555
}
4656

4757
private function getSettings(): Settings
4858
{
49-
return Plugin::getInstance()->getSettings();
59+
/** @var Settings $settings */
60+
$settings = Plugin::getInstance()->getSettings();
61+
return $settings;
5062
}
5163

5264
/**
@@ -84,10 +96,32 @@ public function actionDeleteApiKey(): \yii\web\Response
8496
{
8597
$this->requirePostRequest();
8698

87-
// Settings über Plugin speichern
99+
// Settings ber Plugin speichern
88100
Craft::$app->plugins->savePluginSettings(Plugin::getInstance(), ['apiKey' => null]);
89-
Craft::$app->getSession()->setNotice('API-Key wurde gelöscht.');
101+
Craft::$app->getSession()->setNotice('API-Key wurde gelscht.');
90102

91103
return $this->redirectToPostedUrl();
92104
}
105+
106+
/**
107+
* @throws \Exception
108+
*/
109+
public function actionSettings(): \yii\web\Response
110+
{
111+
$plugin = Plugin::getInstance();
112+
if (!$plugin) {
113+
throw new \Exception('Plugin nicht initialisiert');
114+
}
115+
116+
$settings = $plugin->getSettings();
117+
if (!$settings) {
118+
// Fallback: neue Settings erstellen
119+
$settings = new Settings();
120+
}
121+
122+
return $this->renderTemplate('diffbase/index', [
123+
'plugin' => $plugin,
124+
'settings' => $settings,
125+
]);
126+
}
93127
}

src/models/Settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class Settings extends Model
1010
public function rules(): array
1111
{
1212
return [
13-
[['apiKey'], 'string', 'max' => 255],
13+
['apiKey', 'string'],
14+
['apiKey', 'default', 'value' => null],
1415
];
1516
}
1617

1718
public function generateApiKey(): string
1819
{
19-
$this->apiKey = bin2hex(random_bytes(32));
20-
return $this->apiKey;
20+
return bin2hex(random_bytes(32));
2121
}
2222
}

src/templates/index.twig

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,12 @@
5555
</div>
5656

5757
<script>
58-
function copyApiKey() {
59-
const field = document.getElementById('apiKeyField');
60-
field.select();
61-
document.execCommand('copy');
58+
function copyApiKey() {
59+
const field = document.getElementById('apiKeyField');
60+
field.select();
61+
document.execCommand('copy');
6262
63-
Craft.cp.displayNotice('API-Key in Zwischenablage kopiert');
64-
}
63+
Craft.cp.displayNotice('API-Key in Zwischenablage kopiert');
64+
}
6565
</script>
66-
{% endblock %}
67-
68-
{# <form method="post" action="{{ actionUrl('diffbase/cp/delete-api-key') }}" style="display: inline; margin: 0;">
69-
{{ csrfInput() }}
70-
{{ redirectInput('diffbase') }}
71-
<button type="submit" class="btn secondary" onclick="return confirm('API-Key wirklich löschen?')">
72-
Löschen
73-
</button>
74-
</form>#}
66+
{% endblock %}

0 commit comments

Comments
 (0)