Skip to content

Commit b1a04c1

Browse files
Switch structureID to structureUid
1 parent c85d608 commit b1a04c1

File tree

6 files changed

+109
-20
lines changed

6 files changed

+109
-20
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "dowleydeveloped/website-documentation",
33
"description": "Add Guides to your website to help both clients & agency",
44
"type": "craft-plugin",
5-
"version": "1.5.0",
5+
"version": "2.0.0",
66
"license": "MIT",
77
"authors": [
88
{

src/WebsiteDocumentation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function createConfig()
259259
$structure = Craft::$app->getStructures()->getStructureById($structure->id);
260260

261261
$settings = $this->getSettings()->getAttributes();
262-
$settings["structure"] = $structure->id;
262+
$settings['structureUid'] = $structure->uid;
263263

264264
// Save!
265265
Craft::$app->getPlugins()->savePluginSettings($this, $settings);

src/elements/GuideEntry.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ class GuideEntry extends Element
4343
public ?int $typeId = null;
4444

4545
/**
46-
* @var ?int The ID of the structure.
46+
* @var ?string The UID of the structure.
47+
*/
48+
public ?string $structureUid = null;
49+
50+
/**
51+
* @var ?string The ID of the structure.
4752
*/
4853
public ?int $structureId = null;
4954

@@ -221,7 +226,12 @@ public function init(): void
221226
$entryType = Craft::$app->getEntries()->getEntryTypeByHandle('websiteDocumentationContent');
222227
$this->_entryType = Craft::$app->getEntries()->getEntryTypeByHandle('websiteDocumentationContent');
223228
$this->typeId = $entryType->id;
224-
$this->structureId = WebsiteDocumentation::$settings->structure;
229+
$this->structureUid = WebsiteDocumentation::$settings->structureUid;
230+
231+
if ($this->structureUid) {
232+
$structure = Craft::$app->structures->getStructureByUid($this->structureUid);
233+
$this->structureId = $structure?->id;
234+
}
225235
}
226236

227237
/**
@@ -620,14 +630,15 @@ protected static function defineSources(string $context): array
620630
$sources = [];
621631

622632
$settings = WebsiteDocumentation::$plugin->getSettings();
633+
$structureId = Craft::$app->structures->getStructureByUid($settings->structureUid)?->id;
623634

624635
$sources[] = [
625636
'key' => 'guides',
626637
'label' => Craft::t('site', 'CMS Guides'),
627638
'data' => [
628639
'type' => 'structure',
629640
],
630-
'structureId' => $settings->structure,
641+
'structureId' => $structureId,
631642
'structureEditable' => true,
632643
];
633644

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace dowleydeveloped\websitedocumentation\migrations;
4+
5+
use Craft;
6+
use craft\db\Migration;
7+
use craft\db\Query;
8+
9+
use dowleydeveloped\websitedocumentation\WebsiteDocumentation;
10+
11+
/**
12+
* m260202_132947_fix_structure migration.
13+
*/
14+
class m260202_132947_fix_structure extends Migration
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function safeUp(): bool
20+
{
21+
$plugin = WebsiteDocumentation::getInstance();
22+
$handle = $plugin->id;
23+
24+
$pc = Craft::$app->projectConfig;
25+
$settingsPath = "plugins.$handle.settings";
26+
$settings = $pc->get($settingsPath) ?? [];
27+
28+
// If already set, don’t touch it
29+
if (!empty($settings['structureUid'])) {
30+
return true;
31+
}
32+
33+
// 1) Find structureId(s) used by guide entries
34+
$structureIds = (new Query())
35+
->select(['structureId'])
36+
->from('{{%documentation_guide_entries}}')
37+
->where(['not', ['structureId' => null]])
38+
->distinct()
39+
->column();
40+
41+
if (empty($structureIds)) {
42+
throw new \RuntimeException('No structureId found in documentation_guide_entries.');
43+
}
44+
45+
// If there are multiple, choose deterministically.
46+
// Most plugins will only ever have one.
47+
sort($structureIds);
48+
$structureId = (int)$structureIds[0];
49+
50+
// 2) Convert ID -> UID
51+
$structure = Craft::$app->structures->getStructureById($structureId);
52+
if (!$structure?->uid) {
53+
throw new \RuntimeException("Structure $structureId not found.");
54+
}
55+
56+
// 3) Save UID to Project Config
57+
$settings['structureUid'] = $structure->uid;
58+
59+
$pc->set($settingsPath, $settings);
60+
61+
return true;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function safeDown(): bool
68+
{
69+
echo "m260202_132947_fix_structure cannot be reverted.\n";
70+
return false;
71+
}
72+
}

src/models/Settings.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class Settings extends Model
1616
*/
1717

1818
public $name = "Website Documentation";
19-
public $structure;
20-
public $structureExists;
19+
public ?string $structureUid = null;
2120
public $sites;
2221

2322
// Public Methods
@@ -29,8 +28,7 @@ class Settings extends Model
2928
public function rules(): array
3029
{
3130
return [
32-
["structure", "integer"],
33-
["structureExists", "boolean"],
31+
[['structureUid'], 'string'],
3432
];
3533
}
3634
}

src/resources/js/guide-index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,27 @@ if (typeof Craft.GuideEntries === typeof undefined) {
1111
Craft.GuideEntries.TemplateIndex = Craft.BaseElementIndex.extend({
1212
$newTemplateBtnGroup: null,
1313
$newTemplateBtn: null,
14+
1415
init(elements, main, controller) {
15-
this.on("selectSource", this.createButton.bind(this));
16-
this.on("selectSite", this.createButton.bind(this));
1716
this.base(elements, main, controller);
1817
},
18+
19+
updateElements: function () {
20+
this.base();
21+
22+
// After Craft updates the view (including structure view),
23+
// add/refresh our button.
24+
this.createButton();
25+
},
26+
1927
createButton: function () {
20-
if (null === this.$source) return;
28+
if (this.$source === null) return;
29+
30+
if (this.$newTemplateBtnGroup) {
31+
this.$newTemplateBtnGroup.remove();
32+
}
2133

22-
null !== this.$newTemplateBtnGroup && this.$newTemplateBtnGroup.remove(),
23-
(this.$newTemplateBtnGroup = $('<div class="btngroup submit" data-wrapper/>'));
34+
this.$newTemplateBtnGroup = $('<div class="btngroup submit" data-wrapper/>');
2435

2536
this.$newTemplateBtn = Craft.ui
2637
.createButton({
@@ -36,23 +47,20 @@ Craft.GuideEntries.TemplateIndex = Craft.BaseElementIndex.extend({
3647

3748
this.addButton(this.$newTemplateBtnGroup);
3849
},
39-
_createTemplate: async function () {
40-
const table = this.$source.data("handle");
4150

51+
_createTemplate: async function () {
4252
Craft.sendActionRequest("POST", "websitedocumentation/templates/create", {
4353
data: {
4454
siteId: this.siteId,
4555
entryType: "websiteDocumentationContent",
4656
},
4757
}).then(({ data: table }) => {
48-
document.location.href = Craft.getUrl(table.cpEditUrl, {
49-
fresh: 1,
50-
});
58+
document.location.href = Craft.getUrl(table.cpEditUrl, { fresh: 1 });
5159
});
5260
},
5361
});
5462

5563
Craft.registerElementIndexClass(
5664
"dowleydeveloped\\websitedocumentation\\elements\\GuideEntry",
57-
Craft.GuideEntries.TemplateIndex,
65+
Craft.GuideEntries.TemplateIndex
5866
);

0 commit comments

Comments
 (0)