Trigger an event before a page is updated allowing data to be changed

parent c150e135
<?php
namespace Modules\Page\Events;
use Modules\Core\Contracts\EntityIsChanging;
class PageIsCreating implements EntityIsChanging
{
/**
* Contains the attributes which can be changed by other listeners
* @var array
*/
private $attributes;
/**
* Contains the original attributes which cannot be changed
* @var array
*/
private $original;
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_merge($this->attributes, $attributes);
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
}
......@@ -4,6 +4,7 @@ namespace Modules\Page\Repositories\Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Page\Events\PageIsCreating;
use Modules\Page\Events\PageWasCreated;
use Modules\Page\Events\PageWasDeleted;
use Modules\Page\Events\PageWasUpdated;
......@@ -38,7 +39,9 @@ class EloquentPageRepository extends EloquentBaseRepository implements PageRepos
if (array_get($data, 'is_home') === '1') {
$this->removeOtherHomepage();
}
$page = $this->model->create($data);
event($event = new PageIsCreating($data));
$page = $this->model->create($event->getAttributes());
event(new PageWasCreated($page->id, $data));
......
......@@ -3,6 +3,7 @@
namespace Modules\Page\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Page\Events\PageIsCreating;
use Modules\Page\Events\PageWasCreated;
use Modules\Page\Events\PageWasDeleted;
use Modules\Page\Events\PageWasUpdated;
......@@ -93,6 +94,30 @@ class PagesTest extends BasePageTest
});
}
/** @test */
public function it_triggers_an_event_when_page_is_creating()
{
Event::fake();
$page = $this->createPage();
Event::assertDispatched(PageIsCreating::class, function ($e) use ($page) {
return $e->getAttributes()['template'] === $page->template;
});
}
/** @test */
public function it_can_change_page_data_before_creating_page()
{
Event::listen(PageIsCreating::class, function (PageIsCreating $event) {
$event->setAttributes(['template' => 'better-tpl']);
});
$page = $this->createPage();
$this->assertEquals('better-tpl', $page->template);
}
/** @test */
public function it_triggers_event_when_page_was_updated()
{
......@@ -135,4 +160,17 @@ class PagesTest extends BasePageTest
return $e->page->id === $page->id;
});
}
private function createPage()
{
return $this->page->create([
'is_home' => '1',
'template' => 'default',
'en' => [
'title' => 'My Other Page',
'slug' => 'my-other-page',
'body' => 'My Page Body',
],
]);
}
}
url: https://github.com/AsgardCms/Platform
versions:
"2.5.0@unreleased":
added:
- Trigger event before a page is created (<code>PageIsCreating</code>) allow data to be changed
"2.2.0":
added:
- Testing event trigger on page deletion
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment