Skip to content

Commit 9980a79

Browse files
authored
Merge pull request #5270 from Laravel-Backpack/crud-column-support-array
Add array support to CrudColumn object.
2 parents 0e8cda5 + df020ee commit 9980a79

File tree

6 files changed

+62
-20
lines changed

6 files changed

+62
-20
lines changed

src/app/Library/CrudPanel/CrudColumn.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class CrudColumn
3838

3939
protected $attributes;
4040

41-
public function upload($upload = true)
41+
public function __construct($nameOrDefinitionArray)
4242
{
43-
$this->attributes['upload'] = $upload;
44-
45-
return $this->save();
46-
}
43+
if (is_array($nameOrDefinitionArray)) {
44+
$column = $this->crud()->addAndReturnColumn($nameOrDefinitionArray);
45+
$name = $column->getAttributes()['name'];
46+
} else {
47+
$name = $nameOrDefinitionArray;
48+
}
4749

48-
public function __construct($name)
49-
{
5050
$column = $this->crud()->firstColumnWhere('name', $name);
5151

5252
// if column exists
@@ -159,6 +159,13 @@ public function before($destinationColumn)
159159
return $this;
160160
}
161161

162+
public function upload($upload = true)
163+
{
164+
$this->attributes['upload'] = $upload;
165+
166+
return $this->save();
167+
}
168+
162169
/**
163170
* Make the current column the first one in the columns list.
164171
*

src/app/Library/CrudPanel/Traits/Columns.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,21 @@ public function setColumns($columns)
6666
*/
6767
public function addColumn($column)
6868
{
69-
$column = $this->makeSureColumnHasNeededAttributes($column);
70-
$this->addColumnToOperationSettings($column);
71-
72-
(new CrudColumn($column['name']))->callRegisteredAttributeMacros();
69+
$this->prepareAttributesAndAddColumn($column);
7370

7471
return $this;
7572
}
7673

74+
/**
75+
* Add a column at the end of the CRUD object's "columns" array and return it.
76+
*/
77+
public function addAndReturnColumn(array|string $column): CrudColumn
78+
{
79+
$column = $this->prepareAttributesAndAddColumn($column);
80+
81+
return $column;
82+
}
83+
7784
/**
7885
* Add multiple columns at the end of the CRUD object's "columns" array.
7986
*
@@ -405,15 +412,16 @@ public function countColumnsWithoutActions()
405412
* in addition to the existing options:
406413
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
407414
* - CRUD::column('price')->type('number');
415+
* - CRUD::column(['name' => 'price', 'type' => 'number']);
408416
*
409417
* And if the developer uses the CrudColumn object as Column in their CrudController:
410418
* - Column::name('price')->type('number');
411419
*
412-
* @param string $name The name of the column in the db, or model attribute.
420+
* @param string|array $name The name of the column in the db, or model attribute.
413421
* @return CrudColumn
414422
*/
415-
public function column($name)
423+
public function column($nameOrDefinition)
416424
{
417-
return new CrudColumn($name);
425+
return new CrudColumn($nameOrDefinition);
418426
}
419427
}

src/app/Library/CrudPanel/Traits/ColumnsProtectedMethods.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Str;
78

@@ -325,4 +326,17 @@ protected function hasDatabaseColumn($table, $name)
325326

326327
return in_array($name, $columns);
327328
}
329+
330+
/**
331+
* Prepare the column attributes and add it to operation settings.
332+
*/
333+
private function prepareAttributesAndAddColumn(array|string $column): CrudColumn
334+
{
335+
$column = $this->makeSureColumnHasNeededAttributes($column);
336+
$this->addColumnToOperationSettings($column);
337+
338+
$column = (new CrudColumn($column['name']))->callRegisteredAttributeMacros();
339+
340+
return $column;
341+
}
328342
}

src/app/Library/CrudPanel/Traits/Fields.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,16 @@ public function holdsMultipleInputs(string $fieldName): bool
562562
* in addition to the existing options:
563563
* - CRUD::addField(['name' => 'price', 'type' => 'number']);
564564
* - CRUD::field('price')->type('number');
565+
* - CRUD::field(['name' => 'price', 'type' => 'number']);
565566
*
566567
* And if the developer uses the CrudField object as Field in their CrudController:
567568
* - Field::name('price')->type('number');
568569
*
569-
* @param string $name The name of the column in the db, or model attribute.
570+
* @param string|array $nameOrDefinition The name of the column in the db, or model attribute.
570571
* @return CrudField
571572
*/
572-
public function field($name)
573+
public function field($nameOrDefinition)
573574
{
574-
return new CrudField($name);
575+
return new CrudField($nameOrDefinition);
575576
}
576577
}

src/app/Library/CrudPanel/Traits/Support/MacroableWithAttributes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ public function getMacros()
2929

3030
/**
3131
* Call the macros registered for the given macroable attributes.
32-
*
33-
* @return void
3432
*/
35-
public function callRegisteredAttributeMacros()
33+
public function callRegisteredAttributeMacros(): self
3634
{
3735
$macros = $this->getMacros();
3836
$attributes = $this->getAttributes();
@@ -58,5 +56,7 @@ function ($item) use ($subfieldsWithMacros, $macro) {
5856
);
5957
}
6058
}
59+
60+
return $this;
6161
}
6262
}

tests/Unit/CrudPanel/CrudPanelColumnsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,16 @@ public function testItCanAddAColumnToCrudFromClass()
621621
CrudColumn::name('test');
622622
$this->assertCount(1, $this->crudPanel->columns());
623623
}
624+
625+
public function testItCanAddAFluentColumnUsingArray()
626+
{
627+
$this->crudPanel->column($this->oneColumnArray);
628+
$this->assertCount(1, $this->crudPanel->columns());
629+
}
630+
631+
public function testItCanAddAFluentColumnUsingArrayWithoutName()
632+
{
633+
$this->crudPanel->column(['type' => 'text']);
634+
$this->assertCount(1, $this->crudPanel->columns());
635+
}
624636
}

0 commit comments

Comments
 (0)