Skip to content

Commit

Permalink
[Gutenbergable] Use laravel attribute accessors instead of methods fo…
Browse files Browse the repository at this point in the history
…r getting and setting content
  • Loading branch information
mauricewijnia committed May 21, 2019
1 parent f06517a commit 396ac31
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions src/Models/Gutenbergable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,57 @@ public function content()
return $this->morphOne(Content::class, 'contentable');
}

/**
* Get the rendered content
*/
public function getLbContentAttribute() {
return $this->content->render();
}

/**
* Set the content
* @param String $content - Gutenberg output
*/
public function setLbContentAttribute($content)
{
if (!$this->content) { $this->createContent(); }

$this->content->setContent($content);
$this->content->save();
event(new ContentUpdated($this->content));
}

/**
* Get the raw gutenberg output
*/
public function getLbRawContentAttribute() {
return $this->content->raw_content;
}

/**
* Creates a content object and associates it with the parent object
*/
private function createContent()
{
$content = new Content;
$this->content()->save($content);
$this->content = $content;
event(new ContentCreated($content));
}

/**
* Delete content when model gets deleted
*/
protected static function bootGutenbergable()
{
self::deleting(function ($model) {
$model->content()->delete();
});
}

/**
* DEPRECATED
*/

/**
* Returns the rendered HTML from the Content object
Expand Down Expand Up @@ -55,25 +106,4 @@ public function setContent($content, $save = false)
if ($save) { $this->content->save(); }
event(new ContentUpdated($this->content));
}

/**
* Creates a content object and associates it with the parent object
*/
private function createContent()
{
$content = new Content;
$this->content()->save($content);
$this->content = $content;
event(new ContentCreated($content));
}

/**
* Delete content when model gets deleted
*/
protected static function bootGutenbergable()
{
self::deleting(function ($model) {
$model->content()->delete();
});
}
}

0 comments on commit 396ac31

Please sign in to comment.