]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 25 Nov 2020 11:45:09 +0000 (12:45 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 25 Nov 2020 11:45:09 +0000 (12:45 +0100)
src/app/Magic/BunchOfFields.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/CubistMagicTranslatableModel.php

index 79f61d03878cd7c12653989f650fe37c3803a621..15b1166bb0f0061eb2a8a342cfa22046f93d329b 100644 (file)
@@ -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);
 
index 071543dbaf6a954c116d573cfa1318e7fb8c9cc1..047f8f76cecfaa9b687d07a8d15e7b4fc313ccb1 100644 (file)
@@ -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');
index e15a3612ba53cd99b03d1e8a6a56c11f91e97b6f..484985070ca7c1217567b04c8c1e5cc4b854823f 100644 (file)
@@ -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)) {