Skip to content

Commit

Permalink
Added section service
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Leveille committed Jan 23, 2023
1 parent a73ddec commit 7d5073a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/site/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);

Expand Down
13 changes: 13 additions & 0 deletions modules/site/base/ModuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -96,6 +108,7 @@ private function _setModuleComponents(): void
'category' => CategoryService::class,
'entry' => EntryService::class,
'gql' => GqlService::class,
'section' => SectionService::class,
'user' => UserService::class,
]);
}
Expand Down
30 changes: 30 additions & 0 deletions modules/site/services/SectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace modules\site\services;

use craft\events\SectionEvent;

class SectionService extends \craft\base\Component
{
// Public Methods
// =========================================================================

/**
* Before saving a section.
*
* @param SectionEvent $event
*/
public function beforeSaveSectionHandler(SectionEvent $event): void
{
if (!$event->isNew) {
return;
}

// Set default preview target
$event->section->previewTargets = [[
'label' => 'Page',
'urlFormat' => '{{ getPreviewUrl(object) }}',
'refresh' => true,
]];
}
}

0 comments on commit 7d5073a

Please sign in to comment.