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
23c123f1
Unverified
Commit
23c123f1
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
c150e135
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
1 deletion
+94
-1
PageIsCreating.php
Modules/Page/Events/PageIsCreating.php
+49
-0
EloquentPageRepository.php
...les/Page/Repositories/Eloquent/EloquentPageRepository.php
+4
-1
PagesTest.php
Modules/Page/Tests/PagesTest.php
+38
-0
changelog.yml
Modules/Page/changelog.yml
+3
-0
No files found.
Modules/Page/Events/PageIsCreating.php
0 → 100644
View file @
23c123f1
<?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
;
}
}
Modules/Page/Repositories/Eloquent/EloquentPageRepository.php
View file @
23c123f1
...
...
@@ -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
));
...
...
Modules/Page/Tests/PagesTest.php
View file @
23c123f1
...
...
@@ -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'
,
],
]);
}
}
Modules/Page/changelog.yml
View file @
23c123f1
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
...
...
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