Custom Action
An action helps to perform a request on a CRUD entry. By default, NexoPOS comes with "edit" and "delete" actions that operate directly on the selected row. This will show you how to create a custom action on a CRUD entry.
Register Custom Filter
<?php
namespace Modules\YourModule\Providers;
use App\Classes\Hook;
use App\Services\CrudEntry;
use App\Crud\OrderCrud;
class ServiceProvider
{
public function register()
{
Hook::addFilter( OrderCrud::method( 'setActions' ), [ $this, 'customAction' ]);
}
}
Let's explain what is happening above. First of all, we'll register our filter with the service provider of our module. We've imported 3 classes (Hook, CrudEntry, and OrderCrud).
In this example, we're trying to add a new action to a CRUD entry on the order table. We then add a filter using the method "addFilter" that will put the callback to the method "setActions" of the OrderCrud class.
So every time the method "setActions" is called, the callback "customAction" is fired with as a unique parameter a "CrudEntry" instance.
Add Action To The CrudEntry Instance
Now, we'll focus on the callback method as that's where we'll inject our action. Note that we can inject more than one action at a time. Here is our to defining an action:
<?php
namespace Modules\YourModule\Providers;
use App\Classes\Hook;
use App\Services\CrudEntry;
use App\Crud\OrderCrud;
class ServiceProvider
{
public function regiser()
{
Hook::addFilter( OrderCrud::method( 'setActions' ), [ $this, 'customAction' ]);
}
public function customAction( CrudEntry $crudEntry )
{
$crudEntry->action(
label: 'Email to Customer',
identifier: 'email',
type: 'GET',
url: url( '/api/your/slug/' . $crudEntry->id )
);
return $entry;
}
}
Note that here, our action will have as an identifier "email" and as a label "Email to Customer" (visible by the user). When it's clicked, a GET request (asynchronous) will be performed to the URL provided on the "url" parameter.
In case you want the user to confirm their action, you can add a new entry to that array named "confirm" like so:
<?php
namespace Modules\YourModule\Providers;
use App\Classes\Hook;
use App\Services\CrudEntry;
use App\Crud\OrderCrud;
class ServiceProvider
{
public function regiser()
{
Hook::addFilter( OrderCrud::method( 'setActions' ), [ $this, 'customAction' ]);
}
public function customAction( CrudEntry $crudEntry )
{
$crudEntry->action(
label: 'Email to Customer',
type: 'GET',
url: url( '/api/your/slug/' . $crudEntry->id ),
confirm: [
'message' => 'Would you like to send this as an email?'
]
);
return $entry;
}
}
For asynchronous requests, only "GET" and "DELETE" are supported. If you would like the user to land on a different page after clicking on the action, you'll use the type "GOTO" instead of "GET" like this:
<?php
namespace Modules\YourModule\Providers;
use App\Classes\Hook;
use App\Services\CrudEntry;
use App\Crud\OrderCrud;
class ServiceProvider
{
public function regiser()
{
Hook::addFilter( OrderCrud::method( 'setActions' ), [ $this, 'customAction' ]);
}
public function customAction( CrudEntry $crudEntry )
{
$crudEntry->action(
lable: 'Email to customer',
type: 'GOTO',
url: url( '/api/your/slug/' . $crudEntry->id ),
confirm: [
'message' => 'Would you like to send this as an email?'
]
);
return $entry;
}
}
By clicking on an action, the user will be redirected to the page on the "url" key.
Performing Async Requests
By default, GET and DELETE requests will perform an asynchronous request. However, one of the requirements is to add a confirmation message. This should prevent the end-user from triggering an action mistakenly.
<?php
namespace Modules\YourModule\Providers;
use App\Classes\Hook;
use App\Services\CrudEntry;
use App\Crud\OrderCrud;
class ServiceProvider
{
public function regiser()
{
Hook::addFilter( OrderCrud::method( 'setActions' ), [ $this, 'customAction' ]);
}
public function customAction( CrudEntry $crudEntry )
{
$crudEntry->action(
label: 'Refresh Product',
type: 'GET',
url: url( '/api/your/slug/' . $crudEntry->id ),
confirm: [
'message' => 'Would you like to refresh the products ?'
]
);
return $entry;
}
}