Skip to content

Commit 988232d

Browse files
committed
addon’s booting callback
1 parent d79821e commit 988232d

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

src/ServiceProvider.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace TransformStudios\Events;
44

5-
use Statamic\Entries\Entry;
65
use Statamic\Facades\Collection;
76
use Statamic\Facades\Field as FieldFacade;
8-
use Statamic\Fields\Field;
9-
use Statamic\Fields\Value;
10-
use Statamic\Fieldtypes\Dictionary;
117
use Statamic\Providers\AddonServiceProvider;
128
use Statamic\Statamic;
139

@@ -18,26 +14,19 @@ public function bootAddon()
1814
FieldFacade::computedDefault('default-events-timezone', fn () => Statamic::displayTimezone());
1915
FieldFacade::computedDefault('default-event-timezone', fn () => Events::defaultTimezone());
2016

21-
collect(Events::setting('collections', ['events']))
22-
->each(fn (string $collection) => Collection::computed(
23-
$collection,
24-
'timezone',
25-
$this->timezone(...)
26-
));
27-
}
28-
29-
private function timezone(Entry $entry, $value): string|Value
30-
{
31-
$value ??= Events::defaultTimezone();
32-
33-
if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Dictionary) {
34-
return $value;
35-
}
36-
37-
return (new Field('timezone', ['dictionary' => 'timezones', 'max_items' => '1', 'type' => 'dictionary']))
38-
->setValue($value)
39-
->setParent($entry)
40-
->augment()
41-
->value();
17+
// has to be in booted so that the `events::event` fieldset is properly registered and loaded
18+
$this->booted(function () {
19+
collect(Events::setting('collections', ['events']))
20+
->each(function (string $collection) {
21+
Collection::findByHandle($collection)?->entryBlueprint()->ensureField(
22+
'timezone',
23+
[
24+
'dictionary' => 'timezones',
25+
'max_items' => '1',
26+
'type' => 'dictionary',
27+
'default' => 'computed:default-event-timezone',
28+
]);
29+
});
30+
});
4231
}
4332
}

0 commit comments

Comments
 (0)