Skip to content

Commit 32d6490

Browse files
committed
Media field input
1 parent 0551028 commit 32d6490

File tree

3 files changed

+122
-30
lines changed

3 files changed

+122
-30
lines changed

README.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ Hi! We are a web development agency from Nijmegen in the Netherlands and we use
1111

1212
## About this package
1313

14-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
14+
This package adds a powerful Media field component to the Backstage CMS. It allows you to:
15+
16+
- Upload and manage media files (images, videos, audio, PDFs) directly in your forms
17+
- Configure accepted file types per field
18+
- Enable single or multiple file uploads
19+
- Easily integrate with Laravel's file storage system
20+
- Preview uploaded media files
21+
- Organize media assets with a built-in media library
22+
- Handle file validation and processing automatically
23+
24+
The Media field is built on top of Filament's form components and integrates seamlessly with the Backstage CMS interface.
1525

1626
## Installation
1727

@@ -21,38 +31,19 @@ You can install the package via composer:
2131
composer require vormkracht10/backstage-media-field
2232
```
2333

24-
You can publish and run the migrations with:
25-
26-
```bash
27-
php artisan vendor:publish --tag="backstage-media-field-migrations"
28-
php artisan migrate
29-
```
30-
31-
You can publish the config file with:
32-
33-
```bash
34-
php artisan vendor:publish --tag="backstage-media-field-config"
35-
```
36-
37-
Optionally, you can publish the views using
38-
39-
```bash
40-
php artisan vendor:publish --tag="backstage-media-field-views"
41-
```
42-
43-
This is the contents of the published config file:
34+
Then you need to add the Uploadcare field to your `backstage.php` config file:
4435

4536
```php
4637
return [
38+
'fields' => [
39+
Vormkracht10\MediaField\Media::class,
40+
],
4741
];
4842
```
4943

5044
## Usage
5145

52-
```php
53-
$media = new Vormkracht10\Media();
54-
echo $media->echoPhrase('Hello, Vormkracht10!');
55-
```
46+
After adding the Media field to your `backstage.php` config file, the field will automatically be available in the Backstage CMS.
5647

5748
## Testing
5849

src/Facades/MediaField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @see \Vormkracht10\MediaField\MediaField
8+
* @see \Vormkracht10\MediaField\Media
99
*/
10-
class MediaField extends Facade
10+
class Media extends Facade
1111
{
1212
protected static function getFacadeAccessor(): string
1313
{
14-
return \Vormkracht10\MediaField\MediaField::class;
14+
return \Vormkracht10\MediaField\Media::class;
1515
}
16-
}
16+
}

src/MediaField.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,105 @@
22

33
namespace Vormkracht10\MediaField;
44

5-
class MediaField {}
5+
use Filament\Forms;
6+
use Vormkracht10\Fields\Fields\Base;
7+
use Vormkracht10\Fields\Models\Field;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Vormkracht10\Backstage\Models\Media as MediaModel;
10+
use Vormkracht10\MediaPicker\MediaPicker;
11+
use Vormkracht10\Fields\Contracts\FieldContract;
12+
use Vormkracht10\MediaPicker\Components\MediaPicker as Input;
13+
14+
class Media extends Base implements FieldContract
15+
{
16+
public static function getDefaultConfig(): array
17+
{
18+
return [
19+
...parent::getDefaultConfig(),
20+
'acceptedFileTypes' => ['image/*', 'video/*', 'audio/*', 'application/pdf'],
21+
'multiple' => false,
22+
];
23+
}
24+
25+
public static function make(string $name, Field $field): Input
26+
{
27+
$input = self::applyDefaultSettings(Input::make($name), $field);
28+
29+
if (! empty($field->config['acceptedFileTypes']) && ! is_array($field->config['acceptedFileTypes'])) {
30+
$field->config['acceptedFileTypes'] = [$field->config['acceptedFileTypes']];
31+
}
32+
33+
$input = $input->label($field->name ?? self::getDefaultConfig()['label'] ?? null)
34+
->acceptedFileTypes($field->config['acceptedFileTypes'] ?? self::getDefaultConfig()['acceptedFileTypes'])
35+
->multiple($field->config['multiple'] ?? self::getDefaultConfig()['multiple']);
36+
37+
return $input;
38+
}
39+
40+
public function getForm(): array
41+
{
42+
return [
43+
Forms\Components\Tabs::make()
44+
->schema([
45+
Forms\Components\Tabs\Tab::make('General')
46+
->label(__('General'))
47+
->schema([
48+
...parent::getForm(),
49+
]),
50+
Forms\Components\Tabs\Tab::make('Field specific')
51+
->label(__('Field specific'))
52+
->schema([
53+
Forms\Components\Grid::make(2)->schema([
54+
Forms\Components\Select::make('config.acceptedFileTypes')
55+
->label(__('Accepted file types'))
56+
->options([
57+
'image/*' => 'Images',
58+
'video/*' => 'Videos',
59+
'audio/*' => 'Audio',
60+
'application/pdf' => 'PDF',
61+
])
62+
->multiple(),
63+
Forms\Components\Toggle::make('config.multiple')
64+
->label(__('Multiple')),
65+
]),
66+
]),
67+
])->columnSpanFull(),
68+
];
69+
}
70+
71+
public static function mutateFormDataCallback(Model $record, Field $field, array $data): array
72+
{
73+
if (! isset($record->values[$field->slug])) {
74+
return $data;
75+
}
76+
77+
$media = MediaModel::whereIn('ulid', $record->values[$field->slug])
78+
->get()
79+
->map(function ($media) {
80+
return 'media/' . $media->filename;
81+
})->toArray();
82+
83+
$data['value'][$field->slug] = $media;
84+
85+
return $data;
86+
}
87+
88+
public static function mutateBeforeSaveCallback(Model $record, Field $field, array $data): array
89+
{
90+
if ($field->field_type !== 'media') {
91+
return $data;
92+
}
93+
94+
if (! isset($data['value'][$field->slug])) {
95+
return $data;
96+
}
97+
98+
$media = MediaPicker::create($data['value'][$field->slug]);
99+
100+
$data['value'][$field->slug] = collect($media)->map(function ($media) {
101+
return $media->ulid;
102+
})->toArray();
103+
104+
return $data;
105+
}
106+
}

0 commit comments

Comments
 (0)