Editing image in custom page
Hi All,
I am using stancl tenancy with filament. I have this custom page to change the branding of the tenant. Code Below:
class Branding extends Page implements HasForms
{
use InteractsWithForms;
use WithFileUploads;
protected static ?string $navigationIcon = 'heroicon-o-paint-brush';
protected static ?string $navigationGroup = 'Settings';
protected static string $view = 'filament.tenant.pages.branding';
public ?array $data = [];
public function mount(): void
{
// TODO: fill existing images or allow users to clear
$this->form->fill([
'name' => tenant('name'),
'primary_color' => tenant('primary_color'),
'logo' => tenant_asset(tenant('logo')),
'favicon' => tenant_asset('favicon'),
]);
}
public function form(Form $form): Form
{
return $form->schema(BrandingForm::schema())->statePath('data');
}
public function submit()
{
$this->validate();
if (!empty($this->data['logo'])) {
reset($this->data['logo']);
$logo = current($this->data['logo']);
$logoUrl = $logo->store('logos', 'public');
} else {
$logoUrl = tenant('logo');
}
if (!empty($this->data['favicon'])) {
reset($this->data['favicon']);
$favicon = current($this->data['favicon']);
$faviconUrl = $favicon->store('favicons', 'public');
} else {
$faviconUrl = tenant('favicon');
}
tenant()->update([
'name' => $this->data['name'],
'primary_color' => $this->data['primary_color'],
'logo' => $logoUrl,
'favicon' => $faviconUrl,
]);
Notification::make()
->title('Branding updated')
->success()
->send();
$this->dispatch('reload');
}
}
class Branding extends Page implements HasForms
{
use InteractsWithForms;
use WithFileUploads;
protected static ?string $navigationIcon = 'heroicon-o-paint-brush';
protected static ?string $navigationGroup = 'Settings';
protected static string $view = 'filament.tenant.pages.branding';
public ?array $data = [];
public function mount(): void
{
// TODO: fill existing images or allow users to clear
$this->form->fill([
'name' => tenant('name'),
'primary_color' => tenant('primary_color'),
'logo' => tenant_asset(tenant('logo')),
'favicon' => tenant_asset('favicon'),
]);
}
public function form(Form $form): Form
{
return $form->schema(BrandingForm::schema())->statePath('data');
}
public function submit()
{
$this->validate();
if (!empty($this->data['logo'])) {
reset($this->data['logo']);
$logo = current($this->data['logo']);
$logoUrl = $logo->store('logos', 'public');
} else {
$logoUrl = tenant('logo');
}
if (!empty($this->data['favicon'])) {
reset($this->data['favicon']);
$favicon = current($this->data['favicon']);
$faviconUrl = $favicon->store('favicons', 'public');
} else {
$faviconUrl = tenant('favicon');
}
tenant()->update([
'name' => $this->data['name'],
'primary_color' => $this->data['primary_color'],
'logo' => $logoUrl,
'favicon' => $faviconUrl,
]);
Notification::make()
->title('Branding updated')
->success()
->send();
$this->dispatch('reload');
}
}
2 Replies
and BrandingForm
Now when I save the image, the url to retrive it is given by
public static function schema(): array
{
// TODO: allow to set same icon as favicon
// TODO: recommendation for banner or leave as text
// TODO: set the colour scheme
return [
TextInput::make('name')
->label('Name')
->required(),
ColorSwatchInput::make('primary_color')
->label('Primary Color')
->required(),
FileUpload::make('logo')
->label('Logo')
->image()
->maxSize(2048) // 2MB
->maxFiles(1),
FileUpload::make('favicon')
->label('Favicon')
->helperText('A small icon that appears in the browser tab')
->image()
->maxSize(2048) // 2MB
->maxFiles(1),
];
}
public static function schema(): array
{
// TODO: allow to set same icon as favicon
// TODO: recommendation for banner or leave as text
// TODO: set the colour scheme
return [
TextInput::make('name')
->label('Name')
->required(),
ColorSwatchInput::make('primary_color')
->label('Primary Color')
->required(),
FileUpload::make('logo')
->label('Logo')
->image()
->maxSize(2048) // 2MB
->maxFiles(1),
FileUpload::make('favicon')
->label('Favicon')
->helperText('A small icon that appears in the browser tab')
->image()
->maxSize(2048) // 2MB
->maxFiles(1),
];
}
tenant_asset(tenant('logo'))
this returns a url like http://mackillop.localhost:8000/tenancy/assets/logos/tg7der1J1nPkFmGvaPPkqxuts90Nx7DHrM4wlUMT.png
but when I fill this in, it fails to load the image.
if I try tenant('logo') it requests the wrong url going straight to localhost
Any ideas on how to fix this situationSolution
fixed 👍 use custom fileupload extend