F
Filament16mo ago
gon.exe

How to create a dynamically hidden header button?

Hello, I need to show a header button only when some criteria are met. If not, I don't want to show the button. Thanks in advance
<?php

namespace App\Filament\Resources\PurchaseRequisitionResource\Pages;

use App\Filament\Pages\CreatePurchaseOrderUsingPurchaseRequisition;
use App\Filament\Resources\PurchaseOrderResource;
use Filament\Pages\Actions;
use Filament\Pages\Actions\Action;
use Filament\Resources\Pages\ViewRecord;
use App\Filament\Resources\PurchaseRequisitionResource;

class ViewPurchaseRequisition extends ViewRecord
{
protected static string $resource = PurchaseRequisitionResource::class;

protected function getActions(): array
{
return [
Actions\EditAction::make(),

// Custom action to redirect to Create Purchase Order page
// TO DO: Only show button in X case
Action::make('Create Purchase Order')
->label(__('Create Purchase Order'))
->url(fn (): string => CreatePurchaseOrderUsingPurchaseRequisition::getUrl(['PRid' => $this->record->id]))
];
}
}
<?php

namespace App\Filament\Resources\PurchaseRequisitionResource\Pages;

use App\Filament\Pages\CreatePurchaseOrderUsingPurchaseRequisition;
use App\Filament\Resources\PurchaseOrderResource;
use Filament\Pages\Actions;
use Filament\Pages\Actions\Action;
use Filament\Resources\Pages\ViewRecord;
use App\Filament\Resources\PurchaseRequisitionResource;

class ViewPurchaseRequisition extends ViewRecord
{
protected static string $resource = PurchaseRequisitionResource::class;

protected function getActions(): array
{
return [
Actions\EditAction::make(),

// Custom action to redirect to Create Purchase Order page
// TO DO: Only show button in X case
Action::make('Create Purchase Order')
->label(__('Create Purchase Order'))
->url(fn (): string => CreatePurchaseOrderUsingPurchaseRequisition::getUrl(['PRid' => $this->record->id]))
];
}
}
No description
2 Replies
awcodes
awcodes16mo ago
Might be a cleaner way, but this should get you there.
protected function getActions(): array
{
$actions = collect();

if ($shouldShowPOAction) {
$actions->push(
Action::make('Create Purchase Order')
->label(__('Create Purchase Order'))
->url(fn (): string => POReq::getUrl([
'PRid' => $this->record->id
]))
);
}

return [
Actions\EditAction::make(),
...$actions->toArray(),
];
}
protected function getActions(): array
{
$actions = collect();

if ($shouldShowPOAction) {
$actions->push(
Action::make('Create Purchase Order')
->label(__('Create Purchase Order'))
->url(fn (): string => POReq::getUrl([
'PRid' => $this->record->id
]))
);
}

return [
Actions\EditAction::make(),
...$actions->toArray(),
];
}
gon.exe
gon.exeOP16mo ago
Excellent! That works for me! Thanks @awcodes

Did you find this page helpful?