Skip to content

Commit

Permalink
Merge pull request #2951 from Laravel-Backpack/add-fluent-field-group
Browse files Browse the repository at this point in the history
Add fluent field group
  • Loading branch information
pxpm authored Jul 22, 2024
2 parents 73c265e + 267e88e commit cb95ac0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/Library/CrudPanel/CrudField.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public function getAttributes()
{
return $this->attributes;
}

// ---------------
// PRIVATE METHODS
// ---------------
Expand Down
36 changes: 36 additions & 0 deletions src/app/Library/CrudPanel/CrudObjectGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel;

class CrudObjectGroup
{
protected $objects;

/**
* Add CrudObjects (fields, columns etc) to the group.
*/
public function __construct(...$objects)
{
if (is_array($objects[0])) {
$objects = $objects[0];
}

$this->objects = $objects;
}

// -------------
// MAGIC METHODS
// -------------

/**
* We forward any call to the corresponding class passed by developer (Field, Columns, Filters etc ..).
*/
public function __call(string $method, array $parameter)
{
foreach ($this->objects as $object) {
$object->{$method}($parameter[0]);
}

return $this;
}
}
15 changes: 15 additions & 0 deletions src/app/Library/CrudPanel/CrudPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,19 @@ private function getRelatedEntries($model, $relationString)

return $results;
}

/**
* Allow to add an attribute to multiple fields/columns/filters/buttons at same time.
*
* Using the fluent syntax allow the developer to add attributes to multiple fields at the same time. Eg:
*
* - CRUD::group(CRUD::field('price')->type('number'), CRUD::field('title')->type('text'))->tab('both_on_same_tab');
*
* @param mixed fluent syntax objects.
* @return CrudObjectGroup
*/
public function group(...$objects)
{
return new CrudObjectGroup(...$objects);
}
}
31 changes: 31 additions & 0 deletions tests/Unit/CrudPanel/CrudPanelObjectGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Backpack\CRUD\Tests\Unit\CrudPanel;

/**
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudObjectGroup
*/
class CrudPanelObjectGroupTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel
{
public function testItCanCreateAGroupOfCrudObjects()
{
$this->crudPanel->group(
$this->crudPanel->field('test'),
$this->crudPanel->field('test2')
)->label('testing');

$this->assertEquals('testing', $this->crudPanel->fields()['test']['label']);
$this->assertEquals('testing', $this->crudPanel->fields()['test2']['label']);
}

public function testItCanCreateAGroupOfCrudObjectsFromArrayInput()
{
$this->crudPanel->group([
$this->crudPanel->field('test'),
$this->crudPanel->field('test2'),
])->label('testing');

$this->assertEquals('testing', $this->crudPanel->fields()['test']['label']);
$this->assertEquals('testing', $this->crudPanel->fields()['test2']['label']);
}
}

0 comments on commit cb95ac0

Please sign in to comment.