use Cubist\Backpack\app\Magic\Fields\Field;
+use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
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);
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');
$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)) {