Stubbing out initial ideas on metafields.

This commit is contained in:
Ryan Scherler
2020-05-01 14:38:47 -07:00
parent ccc55a4e45
commit 0cba76b774
3 changed files with 64 additions and 11 deletions
+2 -2
View File
@@ -7,8 +7,8 @@ return [
| Permissions
|--------------------------------------------------------------------------
|
| The initial system permissions needed by Filament. These are create using
| the `php artisan filament:install` command.
| The initial system permissions needed by Filament. These are added when
| running the initial seed `php artisan db:seed --class=FilamentSeeder`
|
*/
+23
View File
@@ -20,6 +20,10 @@ class DummyFieldset
/**
* Return the fields for the given form.
*
* Note: these are core database columns defined by the model schema.
* If you add a field, first create a migration and then add the referring field here.
* If you want to attach arbitrary data then add the field to the `metaFields()` method defined below.
*
* @var object $model
* @return array
*/
@@ -32,6 +36,25 @@ class DummyFieldset
];
}
/**
* Return the meta data fields for the given form.
*
* This will allow for easily attaching arbitrary custom fields
* without needing to adjust the database schema.
*
* @var object $model
* @return array
*/
public static function metaFields($model)
{
return [
Field::make('meta-field')
->input()
->label('A Custom Meta Field')
->rules(['string', 'max:255']),
];
}
/**
* Return the realtime rules to ignore for the given form.
*
+39 -9
View File
@@ -152,7 +152,24 @@ trait HasForm
}
/**
* Return fields as a collection from a given fieldset.
* Return realtime rules to ignore from a given fieldset.
*
* @return array
*/
public function rulesIgnoreRealtime()
{
$rules = [];
if ($fieldset = $this->fieldset()) {
if (method_exists($fieldset, 'rulesIgnoreRealtime')) {
$rules = call_user_func("{$fieldset}::rulesIgnoreRealtime");
}
}
return $rules;
}
/**
* Return the fields as a collection from a given fieldset.
*
* @return Illuminate\Support\Collection
*/
@@ -169,7 +186,7 @@ trait HasForm
}
/**
* Get a field from a fields collection.
* Get a field from the fields collection.
*
* @var string $field_name
* @return null|object
@@ -182,19 +199,32 @@ trait HasForm
}
/**
* Return realtime rules to ignore from a given fieldset.
* Return the meta fields as a collection from a given fieldset.
*
* @return array
* @return Illuminate\Support\Collection
*/
public function rulesIgnoreRealtime()
public function metaFields()
{
$rules = [];
$metaFields = [];
if ($fieldset = $this->fieldset()) {
if (method_exists($fieldset, 'rulesIgnoreRealtime')) {
$rules = call_user_func("{$fieldset}::rulesIgnoreRealtime");
if (method_exists($fieldset, 'metaFields')) {
$metaFields = call_user_func("{$fieldset}::metaFields", $this->model);
}
}
return $rules;
return collect($metaFields);
}
/**
* Get a meta field from the metaFields collection.
*
* @var string $field_name
* @return null|object
*/
public function getMetaField(string $field_name)
{
return $this->metaFields()->filter(function ($metaField, $key) use ($field_name) {
return $metaField->name === $field_name;
})->first();
}
}