Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
5474ea54
Unverified
Commit
5474ea54
authored
Jul 11, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trigger an event before a page is updated allowing data to be changed
parent
23c123f1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
1 deletion
+96
-1
PageIsUpdating.php
Modules/Page/Events/PageIsUpdating.php
+63
-0
EloquentPageRepository.php
...les/Page/Repositories/Eloquent/EloquentPageRepository.php
+4
-1
PagesTest.php
Modules/Page/Tests/PagesTest.php
+28
-0
changelog.yml
Modules/Page/changelog.yml
+1
-0
No files found.
Modules/Page/Events/PageIsUpdating.php
0 → 100644
View file @
5474ea54
<?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
;
}
}
Modules/Page/Repositories/Eloquent/EloquentPageRepository.php
View file @
5474ea54
...
...
@@ -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
));
...
...
Modules/Page/Tests/PagesTest.php
View file @
5474ea54
...
...
@@ -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
()
{
...
...
Modules/Page/changelog.yml
View file @
5474ea54
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment