CRUDlex\ControllerProvider::delete PHP Method

delete() public method

The controller for the "delete" action.
public delete ( Silex\Application $app, string $entity, string $id ) : Response
$app Silex\Application the Silex application
$entity string the current entity
$id string the instance id to delete
return Symfony\Component\HttpFoundation\Response redirects to the entity list page or 404 on invalid input
    public function delete(Application $app, $entity, $id)
    {
        $crudData = $app['crud']->getData($entity);
        $instance = $crudData->get($id);
        if (!$instance) {
            return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound'));
        }
        $filesDeleted = $crudData->deleteFiles($instance, $entity);
        $deleted = $filesDeleted ? $crudData->delete($instance) : AbstractData::DELETION_FAILED_EVENT;
        if ($deleted === AbstractData::DELETION_FAILED_EVENT) {
            $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.delete.failed'));
            return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id]));
        } elseif ($deleted === AbstractData::DELETION_FAILED_STILL_REFERENCED) {
            $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.delete.error', ['%label%' => $crudData->getDefinition()->getLabel()]));
            return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id]));
        }
        $redirectPage = 'crudList';
        $redirectParameters = $this->getAfterDeleteRedirectParameters($app['request_stack']->getCurrentRequest(), $entity, $redirectPage);
        $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.delete.success', ['%label%' => $crudData->getDefinition()->getLabel()]));
        return $app->redirect($app['url_generator']->generate($redirectPage, $redirectParameters));
    }