XF 2.2 Add built-in handler for deletion

This is already built-in:

PHP:
public function actionDelete(ParameterBag $params)
{
    $thing = $this->assertThingExists($params->thing_id); // or whatever to grab the pertinent entity

    $plugin = $this->plugin('XF:Delete');
    return $plugin->actionDelete(
        $thing, // entity
        $this->buildLink('things/delete', $thing), // confirm URL (ie. route to this action)
        $this->buildLink('things/view', $thing), // entity URL
        $this->buildLink('things'), // return URL
        $thing->title // entity title
    );
}

Most things in XF (especially the front-end) are designed around progressive enhancement/graceful degradation, so it's handled in PHP rather than JS. You can add data-xf-click="overlay" to open the confirmation form (or any link in general) in an overlay.
 
Last edited:
Appreciate the example, you can mark the feedback as implemented / close.

I do have a few questions on the implementation though (intellisense doesn't seem to have any suggestions :():
1) Do I have to have to be using entity framework to support this or is there a way to skip?
2) With buildLink is it possible to set the value via post? I can refactor to support GET, but I have been using post for deletion where I had planned on using this.
 
1) Do I have to have to be using entity framework to support this or is there a way to skip?
Yes, it only supports deleting entities. The plugin method is only 20 lines of code, so it should be pretty easy to implement the same pattern for something else:

PHP:
public function actionDelete(): \XF\Mvc\Reply\AbstractReply
{
    // fetch the content and do any applicable sanity checks

    if ($this->isPost())
    {
        // perform the deletion
        return $this->redirect($this->buildLink(...)); // the route to redirect to after deletion
    }

    $confirmUrl = $this->buildLink(...); // the route to this action
    $contentUrl = $this->buildLink(...); // the route to the content
    $contentTitle = 'Some title'; // the title of the content

    $viewParams = [
        'confirmUrl' => $confirmUrl,
        'contentUrl' => $contentUrl,
        'contentTitle' => $contentTitle,
    ];
    return $this->view(
        'XF:Delete\Delete',
        'public:delete_confirm',
        $viewParams
    );
}

2) With buildLink is it possible to set the value via post? I can refactor to support GET, but I have been using post for deletion where I had planned on using this.
The pattern implemented by the plugin already does this. A GET request displays the confirmation form and a POST request is treated as confirmation. The given URL is merely the route the form is submitted to (its action attribute), which in the case of the plugin is the same route that is displaying the form.
 
Last edited:
This worked great! The only thing I noticed is when specifying data-force-flash-message="on" on the button, a redirect with a message doesn't seem to be displayed.
 
It would need to be set on the form instead. You would have to create your own delete_confirm template if you wanted to set that.
 
Top Bottom