From 396ac31ca8d0102dc3b2c3982f2a00dba261302d Mon Sep 17 00:00:00 2001 From: Maurice Wijnia Date: Tue, 21 May 2019 10:24:19 +0200 Subject: [PATCH] [Gutenbergable] Use laravel attribute accessors instead of methods for getting and setting content --- src/Models/Gutenbergable.php | 72 +++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Models/Gutenbergable.php b/src/Models/Gutenbergable.php index cb95e92e..a10ab963 100644 --- a/src/Models/Gutenbergable.php +++ b/src/Models/Gutenbergable.php @@ -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 @@ -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(); - }); - } }