Skip to content

Commit

Permalink
Adding an event hook PostContentIsRendering
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Jul 29, 2017
1 parent 86e7320 commit db38c30
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Entities/PostTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
namespace Modules\Blog\Entities;

use Illuminate\Database\Eloquent\Model;
use Modules\Blog\Events\PostContentIsRendering;

class PostTranslation extends Model
{
public $timestamps = false;
protected $fillable = ['title', 'slug', 'content'];
protected $table = 'blog__post_translations';

public function getContentAttribute($content)
{
event($event = new PostContentIsRendering($content));

return $event->getContent();
}
}
47 changes: 47 additions & 0 deletions Events/PostContentIsRendering.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Modules\Blog\Events;

class PostContentIsRendering
{
/**
* @var string The body of the page to render
*/
private $content;
private $original;

public function __construct($content)
{
$this->content = $content;
$this->original = $content;
}

/**
* @return string
*/
public function getContent()
{
return $this->content;
}

/**
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}

/**
* @return mixed
*/
public function getOriginal()
{
return $this->original;
}

public function __toString()
{
return $this->getContent();
}
}
40 changes: 40 additions & 0 deletions Tests/PostContentIsRenderingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Modules\Blog\Tests;

use Illuminate\Support\Facades\Event;
use Modules\Blog\Entities\Status;
use Modules\Blog\Events\PostContentIsRendering;

class PostContentIsRenderingTest extends BaseBlogTestCase
{
/** @test */
public function it_can_change_final_content()
{
Event::listen(PostContentIsRendering::class, function (PostContentIsRendering $event) {
$event->setContent('<strong>' . $event->getOriginal() . '</strong>');
});

$post = $this->post->create([
'en' => ['title' => 'lorem en', 'slug' => 'something', 'content' => 'My Post Body'],
'fr' => ['title' => 'lorem fr', 'slug' => 'quelque-chose', 'content' => 'My Post Body'],
'category_id' => 1,
'status' => Status::PUBLISHED,
]);

$this->assertEquals('<strong>My Post Body</strong>', $post->content);
}

/** @test */
public function it_doesnt_alter_content_if_no_listeners()
{
$post = $this->post->create([
'en' => ['title' => 'lorem en', 'slug' => 'something', 'content' => 'My Post Body'],
'fr' => ['title' => 'lorem fr', 'slug' => 'quelque-chose', 'content' => 'My Post Body'],
'category_id' => 1,
'status' => Status::PUBLISHED,
]);

$this->assertEquals('My Post Body', $post->content);
}
}
1 change: 1 addition & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ versions:
added:
- Creating new hook when a post is creating
- Creating new hook when a post is updating
- Adding new <code>PostContentIsRendering</code> hook
changed:
- Using new <code>@editor</code> blade directive for wysiwyg
"2.0":
Expand Down

0 comments on commit db38c30

Please sign in to comment.