Actions

Ron FullerNovember 14, 2022About 3 min

Default Actions

The default actions are:

  • view
  • edit
  • create
  • delete

To change the default actions, create a protected variable on the resource

file: PropertyResource.php


    protected array $defaultActions = ['view'];

This will set the default actions for the Property Resource to only the "view" action.

Note

You will need a web route for each default action you enable. The route mapping is the same as resource route mapping.

VerbURIActionRoute Name
GET/properitesindexproperties.index
GET/properties/createcreateproperties.create
POST/propertiesstoreproperties.store
GET/properties/showproperties.show
GET/properties/{property}/editeditproperties.edit
PUT/PATCH/properties/updateproperties.update
DELETE/properties/destoryproperties.destroy

Make Action

You can use the static make method to make an action creating an action with the slug attribute useful for handling the action.

    use Psi\FlexAdmin\Actions\Action;

    Action::make(slug: 'view-website');

Output to Array

You can use the toArray function to output the Action Data. The toArray function is called when the action is built from the resource.

    use Psi\FlexAdmin\Actions\Action;
    use Psi\FlexAdmin\Fields\Field;

    Action::make(slug:'view-website')->toArray(
        context: Field::CONTEXT_INDEX,
        resource:  null
    );

Note

The resource attribute is required if setting canAct on the resource model.

This function call output will result in the following:

    [
        "enabled" => true
        "type" => "inline"
        "slug" => "view-website"
        "withDisabled" => false
        "canAct" => false
        "attributes" => array:6 [â–¼
            "disabled" => false
            "asEvent" => true
            "confirm" => false
            "confirmText" => ""
            "divider" => false
            "title" => "View Website"
        ]
    ]

Attributes

The default attributes are:

    [
        'disabled' => false,    // set to true to show action disabled
        'asEvent' => true,      // action is an event handled in js callback
        'confirm' => false,     // we don't need to confirm the action
        'confirmText' => '',    // if confirm is set, use text to prompt
        'divider' => false,     // this action is not a separation divider
    ]

You can append additional attributes for the action using the attributes function and passing an array of attributes.

Note

The additional attributes must be supported by the Action Menu component in the front-end component library

file: PropertyResource.php

use Psi\FlexAdmin\Actions\Action;

public function actions(): array
{
    return [
        Action::make('view-website')
            ->attributes([
                'iconColor' => 'warning'
            ])
    ]
}

Without Permissions

You can disable permissions for the resource action. Default is to enable permissions via the model policy.

    Action::make('action-slug')->withoutPermissions();

With Disabled

You can allow the action to have a disabled display when the user is not able to execute the action. By default, actions don't appear when disabled.

    Action::make('action-slug')->withDisabled();

Icon

Show an icon for the action label

    Action::make('action-slug')->icon('mdi-account');

Title

The default title will be created from the slug. A slug of view-website would result in a title of "View Website"

You can override the default title.

    use Psi\FlexAdmin\Actions\Action;

    Action::make('view-website')->title('View Pacific Screening');

Display

Hide From Index

Don't show the action when listing the resource.

    Action::make('action-slug')->hideFromIndex()

You can also add a condition that will conditionally hide the action. In this example, we have a flag attribute on the resource which is a boolean.

    Action::make('action-slug')->hideFromIndex(condition: $this->resource->flag)

Routing

Actions with a url or route attribute will automatically be handled as an Inertia link or external link. Without route or url attributes, the action should be handled in the front-end JS library - usually an Ajax call to the laravel backend API.

Route

You can specify a route name and parameters. The following example shows a route defined for the route named "users.show".

The params attribute specifies an array of parameters. Each parameter has a name to specify the parameter name and a field from the resource to supply the parameter value.

params is an array

You must pass an array for the params variable even if there is only one parameter for the route

    Action::make('action-slug')->route(
        name: "users.show",
        method: "get",
        target: "_self",
        params: [
            [
            'name' => 'user',
            'field' => 'id'
            ]
        ]
    );

URL

Setting the url attribute will result in an external link.

    use Psi\FlexAdmin\Actions\Action;

    Action::make('view-website')->url('https://pacificscreening.com')

Resulting toArray would render the attributes as following

    [
        'url' => 'https://pacificscreening.com',
        'target' => '_blank',
        'external' => true,
        'asEvent' => false,
    ]

Enable via Model

You can dynamically enable the action by adding a canAct function to your model. The canAct will pass the action slug and you can dynamically check other model attributes such as status to determine if the action is valid for the current model state.

Example Model File: Property.php


function canAct(string $slug):bool
{
    return match($slug) {
        'view-website' => PropertyStatus::viewable($this->status),
        default: true
    }
}

This example checks to see if the status is one that enables the view-website action. In this case, the action is only enabled for Pending and Incomplete statuses.

file: PropertyStatus.php


enum PropertyStatus: string
{
    case PENDING = 'Pending';
    case INCOMPLETE = 'Incomplete';
    case COMPLETE = 'Complete';


    public static function viewable(PropertyStatus $status): bool
    {
        return in_array($status, [self::PENDING, self::INCOMPLETE]);
    }
}
Last update:
Contributors: Ron Fuller