Revo Docs
v16.0.1

Actions

Actions in Thrust allow you to perform tasks on your resources. They can be defined for single or multiple resources, and their visibility can be toggled as buttons or dropdown options. Thrust also supports defining main actions , which are typically displayed as prominent buttons.

Adding Actions to a Resource

To use actions, they must be registered in your resource by overriding the actions() method. This method returns an array of actions that should be available for the resource.

namespace App\Thrust;

use App\Thrust\Actions\ActivateVeriFactu;
use BadChoice\Thrust\Resource;

class SomeClass extends Resource
{
    public function actions(): array
    {
        return [
            new ActivateVeriFactu(),
        ];
    }
}
Copied!

Adding Main Actions to a Resource

Main actions should be registered in the mainActions() method of the resource. These actions are displayed as prominent buttons, typically for global tasks.

namespace App\Thrust;

use App\Thrust\Actions\Delete;
use BadChoice\Thrust\Resource;

class SomeClass extends Resource
{
    public function mainActions(): array
    {
        return [
            new Delete(),
        ];
    }
}
Copied!

The mainActions() method is specifically for main actions, ensuring that they are displayed prominently in the UI.

Parent Definitions for Actions and Main Actions

The default behavior of actions() and mainActions() can be customized further by extending their parent implementations. For example:

namespace App\Thrust;

use BadChoice\Thrust\Actions\MainAction;
use BadChoice\Thrust\Resource;

class SomeClass extends Resource
{
    public function mainActions()
    {
        return [
            ...static::$importable ? [new Import()] : [],
            ...static::$singleResource ? [] : [MainAction::make('new')],
        ];
    }

    public function actions()
    {
        return !static::$singleResource && $this->canDelete(static::$model)
            ? [new Delete()]
            : [];
    }
}
Copied!
  • mainActions() : Checks if the resource is importable or whether it's a single resource to dynamically add actions.
  • actions() : Conditionally adds actions based on the model's state, such as whether deletion is permitted.

Defining an Action

To define an action, create a class that extends the BadChoice\Thrust\Actions\Action class. The handle method should contain the logic for executing the action. Use the $main property to toggle its display as a button (true ) or a dropdown option (false ).

use BadChoice\Thrust\Actions\Action;
use Illuminate\Support\Collection;

class ActivateVeriFactu extends Action
{
    public $icon = 'gavel';
    public $main = true;
    public $responseAsPopup = true;
    public $needsSelection = false;
    public $needsConfirmation = false;

    public function handle(Collection $objects)
    {
        return view('admin.config.fiscalSettings.veriFactu.activate')->render();
    }
}
Copied!

Defining a Main Action

To define a main action, create a class that extends the BadChoice\Thrust\Actions\MainAction class. These actions are often global and displayed as buttons. Implement the handle or display method to define its behavior.

use BadChoice\Thrust\Actions\MainAction;

class Export extends MainAction
{
    public $title = 'Export';
    public $icon = 'fa fa-download';

    public function display($resourceName, $parent_id = null)
    {
        $title = $this->getTitle();
        $link  = route('thrust.export', $resourceName);
        return " {$title}";
    }

    protected function getTitle()
    {
        return __('thrust::messages.' . $this->title);
    }
}
Copied!