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

parent 23c123f1
<?php
namespace Modules\Page\Events;
use Modules\Core\Contracts\EntityIsChanging;
use Modules\Page\Entities\Page;
class PageIsUpdating 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;
/**
* @var Page
*/
private $page;
public function __construct(Page $page, array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
$this->page = $page;
}
/**
* @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;
}
/**
* @return Page
*/
public function getPage()
{
return $this->page;
}
}
......@@ -5,6 +5,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\PageIsUpdating;
use Modules\Page\Events\PageWasCreated;
use Modules\Page\Events\PageWasDeleted;
use Modules\Page\Events\PageWasUpdated;
......@@ -60,7 +61,9 @@ class EloquentPageRepository extends EloquentBaseRepository implements PageRepos
if (array_get($data, 'is_home') === '1') {
$this->removeOtherHomepage($model->id);
}
$model->update($data);
event($event = new PageIsUpdating($model, $data));
$model->update($event->getAttributes());
event(new PageWasUpdated($model->id, $data));
......
......@@ -4,6 +4,7 @@ namespace Modules\Page\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Page\Events\PageIsCreating;
use Modules\Page\Events\PageIsUpdating;
use Modules\Page\Events\PageWasCreated;
use Modules\Page\Events\PageWasDeleted;
use Modules\Page\Events\PageWasUpdated;
......@@ -118,6 +119,33 @@ class PagesTest extends BasePageTest
$this->assertEquals('better-tpl', $page->template);
}
/** @test */
public function it_triggers_an_event_when_page_is_updating()
{
Event::fake();
$page = $this->createPage();
$this->page->update($page, ['en' => ['title' => 'Better!']]);
Event::assertDispatched(PageIsUpdating::class, function ($e) use ($page) {
return $e->getPage()->id === $page->id;
});
}
/** @test */
public function it_can_change_page_data_before_updating_page()
{
Event::listen(PageIsUpdating::class, function (PageIsUpdating $event) {
$event->setAttributes(['template' => 'better-tpl']);
});
$page = $this->createPage();
$this->page->update($page, ['template' => 'my-template', 'en' => ['title' => 'Better!']]);
$this->assertEquals('better-tpl', $page->template);
}
/** @test */
public function it_triggers_event_when_page_was_updated()
{
......
......@@ -3,6 +3,7 @@ versions:
"2.5.0@unreleased":
added:
- Trigger event before a page is created (<code>PageIsCreating</code>) allow data to be changed
- Trigger event before a page is updated (<code>PageIsUpdating</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