diff --git a/modules/site/Site.php b/modules/site/Site.php index 9573c01..ceed6f9 100644 --- a/modules/site/Site.php +++ b/modules/site/Site.php @@ -22,6 +22,7 @@ use craft\services\Dashboard; use craft\services\Gql; use craft\services\Plugins; +use craft\services\Sections; use craft\web\Application; use craft\web\twig\variables\CraftVariable; use craft\web\UrlManager; @@ -125,6 +126,9 @@ private function _registerElementBehaviors(): void */ private function _registerElementEvents(): void { + // Before saving a section + Event::on(Sections::class, Sections::EVENT_BEFORE_SAVE_SECTION, [$this->getSection(), 'beforeSaveSectionHandler']); + // Before saving a category Event::on(Category::class, Category::EVENT_BEFORE_SAVE, [$this->getCategory(), 'beforeSaveHandler']); diff --git a/modules/site/base/ModuleTrait.php b/modules/site/base/ModuleTrait.php index 4e13406..2f5a486 100644 --- a/modules/site/base/ModuleTrait.php +++ b/modules/site/base/ModuleTrait.php @@ -7,6 +7,7 @@ use modules\site\services\CategoryService; use modules\site\services\EntryService; use modules\site\services\GqlService; +use modules\site\services\SectionService; use modules\site\services\UserService; /** @@ -15,6 +16,7 @@ * @property-read CategoryService $category * @property-read EntryService $entry * @property-read GqlService $gql + * @property-read SectionService $section * @property-read UserService $user */ trait ModuleTrait @@ -72,6 +74,16 @@ public function getGql(): GqlService return $this->get('gql'); } + /** + * Returns the section service. + * + * @return SectionService + */ + public function getSection(): SectionService + { + return $this->get('section'); + } + /** * Returns the user service. * @@ -96,6 +108,7 @@ private function _setModuleComponents(): void 'category' => CategoryService::class, 'entry' => EntryService::class, 'gql' => GqlService::class, + 'section' => SectionService::class, 'user' => UserService::class, ]); } diff --git a/modules/site/services/SectionService.php b/modules/site/services/SectionService.php new file mode 100644 index 0000000..3220f92 --- /dev/null +++ b/modules/site/services/SectionService.php @@ -0,0 +1,30 @@ +isNew) { + return; + } + + // Set default preview target + $event->section->previewTargets = [[ + 'label' => 'Page', + 'urlFormat' => '{{ getPreviewUrl(object) }}', + 'refresh' => true, + ]]; + } +}