October CMS resources and help articles

Simple and to the point. Optimized by the community.

Refresh the backend form of an extended plugin after saving

0
by chocolata, last modified on November 30th, 2020

Refreshing a backend form on save can be done if you modify the _post_toolbar.htm partial to include refresh:1 as mentioned below. Create a new file called _post_toolbar.htm with the contents below and place it in the partials folder of your own plugin.

<?php 
        $isCreate = $this->formGetContext() == 'create'; 
        $pageUrl = isset($pageUrl) ? $pageUrl : null; 
?> 
<div class="form-buttons loading-indicator-container"> 

        <!-- Save --> 
        <a 
                href="nojavascript...;" 
                class="btn btn-primary oc-icon-check save" 
                data-request="onSave" 
                data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>" 
                data-request-before-update="$el.trigger('unchange.oc.changeMonitor')" 
                <?php if (!$isCreate): ?>data-request-data="refresh:1"<?php endif ?> 
                data-hotkey="ctrl+s, cmd+s"> 
                        <?= e(trans('backend::lang.form.save')) ?> 
        </a> 

        <?php if (!$isCreate): ?> 
                <!-- Save and Close --> 
                <a 
                        href="nojavascript...;" 
                        class="btn btn-primary oc-icon-check save" 
                        data-request-before-update="$el.trigger('unchange.oc.changeMonitor')" 
                        data-request="onSave" 
                        data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"> 
                                <?= e(trans('backend::lang.form.save_and_close')) ?> 
                </a> 
        <?php endif ?> 

        <!-- Preview --> 
        <a 
                href="<?= URL::to($pageUrl) ?>" 
                target="_blank" 
                class="btn btn-primary oc-icon-crosshairs <?php if (!false): ?>hide<?php endif ?>" 
                data-control="preview-button"> 
                        <?= e(trans('backend::lang.form.preview_title')) ?> 
        </a> 

        <?php if (!$isCreate): ?> 
                <!-- Delete --> 
                <button 
                        type="button" 
                        class="btn btn-default empty oc-icon-trash-o" 
                        data-request="onDelete" 
                        data-request-confirm="<?= e(trans('rainlab.blog::lang.post.delete_confirm')) ?>" 
                        data-control="delete-button"></button> 
        <?php endif ?> 
</div> 

Then, in your plugins own Plugin.php file, add the following code in the boot() method:

\Event::listen('backend.form.extendFieldsBefore', function ($widget) {
                if (!($widget->getController() instanceof \RainLab\Blog\Controllers\Posts && $widget->model instanceof \RainLab\Blog\Models\Post)) {
                        return;
                }
                $widget->fields['toolbar']['path'] = '$/author/plugin/partials/_post_toolbar.htm';
});

After saving your form, the page should refresh and display the right values if you modified something with a beforeSave event.

Credits to @mjauvin and @lucas.sanner54070 for suggesting this in the October CMS forums, with the above example of the Rainlab Blog plugin:

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?