Index Actions

Index actions are the buttons that appear next to each item in a CRUD index.

Default IndexActions

By default Ensō comes with three basic index actions, added during a Crud Config's default ->configure() phase.

  1. Clone
  2. Edit
  3. Delete

Edit and Delete are applied to all Crud Configs by default, and the Clone options can be added by adding/setting the enable_cloning property of your Crud's configuration file config/enso/crud/crudname.php to true.

You can add, remove etc. these programatically by calling:

$config->addIndexAction('action_name', IndexAction::make());

$config->addIndexActions(['action_name' => IndexAction::make()]);

$config->getIndexAction('action_name');

// return array of all actions
$config->getIndexActions();

// return action with the given name
$config->getIndexActions('action_name');

// returns array of only named actions
$config->getIndexActions(['action_name']);

// Boolean whether any actions exist
$config->hasIndexActions();

$config->hasIndexAction('action_name');

$config->removeIndexAction('action_name');

$config->removeIndexActions(['action_name']);

// overwrite all existing actions
$config->setIndexActions(['action_name' => IndexAction::make()]);

Be aware that if you have upgraded from an older version of Ensō where you made your own index actions to replace these, they may still be using the old data array format. Otherwise, these should now accept and return implementations of IsIndexAction.

New Style (Actions as IsIndexAction instances)

IndexActions must be implementations of the Yadda\Enso\Crud\Contracts\IsIndexAction interface. Base classes have been provided as:

  1. Yadda\Enso\Crud\IndexActions\BaseActions\BaseLinkButton which implements buttons that should redirect the user when clicked.
  2. Yadda\Enso\Crud\IndexActions\BaseActions\BaseActionButton which should perform an XHR request when clicked, and then refresh the data upon receipt of a successful response.

By extending these base classes and pre-populating any properties that you need, you can create reusable actions that can be added to any Crud Config.

These implementations provide chainable methods to programatially update your actions. A common use-case woud be to add a condition to your action, so that the button only shows for some records in the index table and not others.

$this->addIndexAction(
    'action_name',
    IndexAction::make()
        ->condition([$this, 'conditionCalculation']),
);

They have getters, setters and ease-of-use helper calls. For example, the ->title() call interacts with the title property and defers to getTitle() and setTitle($value), depending on whether an argument was passed in.

BaseLinkButton implements the following helpers (and associated getters and setters);

$action->buttonContent('fa fa-file-o');

$action->buttonType('fa');

$action->component('link-button');

$action->condition([$item, 'callableFunction']);

$action->order(23);

$action->route('/%ID%/url-segment');

$action->title('Edit');

$action->wrapperClass('button is-primary');

BaseActionButton implements all of those, as well as:

// Data for adding a confirmation modal
$action->confirm([
    'title' => 'Sure?',
    'text' => 'Are you sure?',
    'type' => 'warning',
]);

$action->method('POST');

Old Style (Actions as data arrays)

Old style actions as data arrays will continue to function in the same way that they previously had. The new style is just an easy to use wrapper around this.

In addition to the other index action altering calls on the Crud Config, old style actions also support direct updating of specific array elements of the action.

$config->updateIndexAction('edit', [
    'title' => 'Change',
]);

You can pass a boolean as a third parameter to updateIndexAction which, if true, will use array_replace_recursive instead of array_replace.

$config->updateIndexAction('edit',
    [
        'button' => [
            'content' => 'fa fa-bicycle',
        ],
    ],
    true
);

Finally, you may also update multiple actions at once:

$config->updateIndexActions(
    [
        'edit' => [
            'button' => [
                'content' => 'fa fa-bicycle',
            ],
        ]
    ],
    true // This will be passed as the third parameter to each index action update
);

Example adding an index action with the data array format

$config->addIndexAction('edit', [
    'component' => 'link-button',
    'route' => '/%ID%/edit',
    'title' => 'Edit',
    'wrapperClass' => 'button',
    'button' => [
        'type' => 'fa',
        'content' => 'fa fa-pencil-square-o',
    ],
    'order' => 10,
];

Customisation

The default Index actions may also be updated most simply by overriding the getCloneIndexAction, getDeleteIndexAction and getEditIndexAction function on the Crud Config. Otherwise, you should extend your Crud Confid's ->configure() call;

When preparing buttons to show in the table, the available buttons are ordered in ascending order based on their order property.

Aditional configuration for the Clone route

Once enabled, the clone action redirects to a clone function on the appropriate controller. This action then defers to two other functions to determine how to clone the given item.

You should Override cloneItem to alter how the actual Model is cloned. The default action is to call ->replicate() on the model, and if it implements the IsPublishable interface, ensure that it is not published. You will likely need to add to this on a case-by-case basis, such as emptying or generating new slugs to maintain the unique requirement in a database column while duplicating items.

You should Override replicateRelations to alter how relations of the model that has been duplicated are copied. This is called after the clone is saved, so there will be a model key set base relationship on. By default, no action is taken here.