From 2f3cb158c63903fdb9c473ef1d488ca89c671f3f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 25 Nov 2020 12:45:09 +0100 Subject: [PATCH] wip #3753 @1 --- src/app/Magic/BunchOfFields.php | 4 +++- .../Magic/Models/CubistMagicAbstractModel.php | 20 +++++++++++++++++-- .../Models/CubistMagicTranslatableModel.php | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/app/Magic/BunchOfFields.php b/src/app/Magic/BunchOfFields.php index 79f61d0..15b1166 100644 --- a/src/app/Magic/BunchOfFields.php +++ b/src/app/Magic/BunchOfFields.php @@ -5,6 +5,7 @@ namespace Cubist\Backpack\app\Magic; use Cubist\Backpack\app\Magic\Fields\Field; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; trait BunchOfFields { @@ -24,8 +25,9 @@ trait BunchOfFields * @return Field * @throws \Exception */ - public function addField(array $attributes) + public function addField($name, $type = 'Text', $label = '', $attributes = []) { + $attributes = CubistMagicAbstractModel::normalizeAttributes($name, $type, $label, $attributes); $attributes = array_merge($this->defaultFieldAttributes, $attributes); $field = Field::getInstance($attributes); diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 071543d..047f8f7 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -223,13 +223,29 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $this->_fieldsToAppend[] = $attributes; } + public static function normalizeAttributes($name, $type = 'Text', $label = '', $attributes = []) + { + if (is_array($name)) { + $attributes = $name; + } else { + $at = ['name' => $name, 'type' => $type, 'label' => $label]; + $attributes = array_merge($at, $attributes); + } + return $attributes; + } + /** - * @param $attributes array + * @param string|array $name + * @param string $type + * @param string $label + * @param array $attributes * @return Field * @throws \Exception */ - public function addField($attributes) + public function addField($name, $type = 'Text', $label = '', $attributes = []) { + $attributes = self::normalizeAttributes($name, $type, $label, $attributes); + $field = $this->bunchAddField($attributes); $field->setModelInstance($this); $name = $field->getAttribute('name'); diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index e15a361..4849850 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -29,9 +29,9 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel $this->translatable = array_unique($this->translatable); } - public function addField($attributes) + public function addField($name, $type = 'Text', $label = '', $attributes = []) { - $field = parent::addField($attributes); + $field = parent::addField($name, $type, $label, $attributes); if ($field->getAttribute('fake', false) === true) { if ($field->getAttribute('translatable')) { if (!in_array($field->getAttribute('store_in'), $this->translatable)) { -- 2.39.5