From 615dc5356050d42d2cdbddd54f70de6a3e64dd98 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 30 Sep 2019 14:07:57 +0200 Subject: [PATCH] wait #3056 @6 --- .../Magic/Models/CubistMagicAbstractModel.php | 9 +++++--- .../Models/CubistMagicTranslatableModel.php | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 35931c9..fc5dc45 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -14,6 +14,7 @@ use Cubist\Util\Json; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\App; use Illuminate\Support\Str; use Spatie\MediaLibrary\HasMedia\HasMedia; use Spatie\MediaLibrary\HasMedia\HasMediaTrait; @@ -135,7 +136,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia * * @return array */ - public function sluggable():array + public function sluggable(): array { return [ 'slug' => [ @@ -152,8 +153,10 @@ class CubistMagicAbstractModel extends Model implements HasMedia $slug = []; foreach ($components as $component) { - if (isset($this->$component) && $this->$component != '') { - $slug[] = $this->$component; + $v = $this->getAttribute($component); + + if ($v) { + $slug[] = $v; } } if (count($slug) > 0) { diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index 0e0559d..db96702 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -46,6 +46,29 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel return $field; } + public function getTranslations(string $key = null): array + { + if ($key !== null) { + $this->guardAgainstNonTranslatableAttribute($key); + + if ($key === 'slug') { + return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { + return $value !== null; + }); + } else { + return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { + return $value !== null && $value !== ''; + }); + } + } + + return array_reduce($this->getTranslatableAttributes(), function ($result, $item) { + $result[$item] = $this->getTranslations($item); + + return $result; + }); + } + public function update(array $attributes = [], array $options = []) { return $this->updateTranslations($this->_prepareData($attributes), $options); -- 2.39.5