callbackAfterDelete

callbackAfterDelete(callable $callback)

The callback that will be used right after the delete. The only parameter that the state will have is the primary key value.

Important: It is important to mention here that the callbackAfterDelete is called only for the Delete button of the row. The callback will not be called for the multiple delete functionality. If you need to use a callback for multiple delete rows, you should use callbackAfterDeleteMultiple instead and keep the callbackAfterDelete for the one-row delete button. Notice: Have in mind that if you haven't replaced the actual delete functionality (e.g. with a callback), it will be impossible to retrieve the data of the deleted row. If the last phrase doesn't make much sense to you we have an example below to understand exactly what we mean.

Example:

$crud->callbackAfterDelete(function ($stateParameters) {
    // Your code here    

    return $stateParameters;
});

The $stateParameters variable is at the below form:

$callbackAfterDelete = (object)[
    'primaryKeyValue' => '1234'
];

Example

Below there is a working example of the callbackAfterDelete method. As we also wanted to skip the delete so we can update the record, we are also using the callbackDelete at the example just for the demonstration to skip the delete.

$crud->setTable('employees');
$crud->setSubject('Employee', 'Employees');
$crud->setRelation('officeCode','offices','city');
$crud->displayAs('officeCode','City');
// Unsetting the multiple delete for this example. If we need it we also need to
// consider to add a $crud->callbackDeleteMultiple callback as well
$crud->unsetDeleteMultiple();

$crud->callbackDelete(function ($stateParameters) {
    // Making sure that we skip the delete functionality first!
    return $stateParameters;
});

$crud->callbackAfterDelete(function ($stateParameters) use ($callbackDeleteModel) {
    $callbackDeleteModel->deleteEmployee($stateParameters->primaryKeyValue);

    return $stateParameters;
});

$output = $crud->render();

$callbackDeleteModel is using Grocery CRUD Custom Model but you are not limited on that. On callbacks you can use any custom libraries.

For reference the code for CallbackDelete class can be found below:

<?php
namespace App\Models;
use GroceryCrud\Core\Exceptions\Exception;
use GroceryCrud\Core\Model;
use Laminas\Db\Sql\Sql;

class CallbackDelete extends Model {
    public function deleteEmployee($employeeNumber) {
        // Validating our data
        if (!is_numeric($employeeNumber)) {
            throw new Exception("Wrong input");
        }

        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from('employees');
        $select->where(['employeeNumber = ?' => $employeeNumber]);

        $employee = $this->getRowFromSelect($select, $sql);

        // More validations
        if (!empty($employee) && is_array($employee) && count($employee) === 1) {
            $employeeLastName = $employee[0]['lastName'];

            if (!strstr($employeeLastName, '[DELETED] ')) {
                $update = $sql->update('employees');
                $update->where(['employeeNumber = ?' => $employeeNumber]);

                $update->set(['lastName' => '[DELETED] ' . $employeeLastName]);

                $statement = $sql->prepareStatementForSqlObject($update);
                return $statement->execute();
            }
        }

        return false;
    }
}