This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

tedbaker63209
tedbaker63209

I'd like to disable soft delete in the Martin Forms plugin, is it possible to somehow extend the model and disable this trait? Or disable this trait on the entire project?

Thanks in advance, any help really appreciated

Last updated

tedbaker63209
tedbaker63209

With the help of mjauvin on discord, I was able to forceDelete on afterDelete event was fired, from my own plugin, by doing so:

public $record_deleted = FALSE;
public function boot() {
    Martin\Forms\Models\Record::extend(function($model) {
        $model->bindEvent('model.afterDelete', function () use ($model) {
            if($this->record_deleted == FALSE) {
                $this->record_deleted = TRUE;
                $model->forceDelete();
            }
        });
    });
}

However, this only works inside the record detail, not on the record list delete button (after selecting one or multiple records). Is there a different event that is fired there?

Last updated

mjauvin
mjauvin

The better way is to auto-purge the records by hooking into the backend.list.extendQuery event:

\Event::listen('backend.list.extendQuery', function ($widget, $query) {
    if ($widget->getController() instanceof \Martin\Forms\Controllers\Records) {
        \Martin\Forms\Models\Record::whereNotNull('deleted_at')->forceDelete();
    }
});

Last updated

tedbaker63209
tedbaker63209

I ended up using backend.list.extendQuery as suggested above, and added a toggle in my plugin's settings to enable/disable this functionality.

This is my Plugin.php:

<?php namespace tedbaker\Magicformsextended;

use System\Classes\PluginBase;
use System\Classes\SettingsManager;
use Event;

use tedbaker\Magicformsextended\Models\Settings;

use Martin\Forms\Models\Record as RecordModel;
use Martin\Forms\Controllers\Records as RecordsController;

class Plugin extends PluginBase
{

    public function registerComponents()
    {
    }

    public function registerSettings()
    {
        return [
            'config' => [
                'label'       => 'tedbaker.magicformsextended::lang.menu.label',
                'description' => 'tedbaker.magicformsextended::lang.menu.settings',
                'category'    => SettingsManager::CATEGORY_CMS,
                'icon'        => 'icon-bolt',
                'class'       => 'tedbaker\Magicformsextended\Models\Settings',
                'permissions' => ['tedbaker.magicformsextended.access_settings'],
                'order'       => 500
            ]
        ];
    }

    public function registerPermissions() {
        return [
            'tedbaker.magicformsextended.access_settings' => [
                'tab' => 'tedbaker.magicformsextended::lang.permissions.tab', 
                'label' => 'tedbaker.magicformsextended::lang.permissions.access_settings'
            ]
        ];
    }

    public function boot() {

        // Purges soft deleted entries from martin_forms_records when the list is loaded/refreshed
        Event::listen('backend.list.extendQuery', function ($widget, $query) {
            if ($widget->getController() instanceof RecordsController) {
                if (Settings::get('softdelete_enabled', false)) { 
                    RecordModel::whereNotNull('deleted_at')->forceDelete();
                }
            }
        });

    }

}

created a models folder with just Settings.php:

<?php

    namespace tedbaker\Magicformsextended\Models;

    use Model;

    class Settings extends Model {

        public $implement      = ['System.Behaviors.SettingsModel'];
        public $settingsCode   = 'tedbaker_magicformsextended_settings';
        public $settingsFields = 'fields.yaml';

    }
?>

and models/settings/fields.yaml:

tabs:
    fields:
        softdelete_enabled:
            label  : tedbaker.magicformsextended::lang.settings.softdelete_enabled
            type   : switch
            default: false
            tab    : tedbaker.magicformsextended::lang.settings.tabs.general

and the necessary langs

Thanks again for the help mjauvin!

Last updated

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.