611

Product support

Get help in the plugin support forum.

  • Added on Jan 24, 2019
  • Current version: 1.0.1
  • Platform Compatibility
    v3.x not tested
    v2.x not tested
    v1.x use latest
  • Created by

Categories

Adds a Controller Behavior like Backend\Behaviors\FormController for duplicating Models. Provides an easy way of duplicating Entities.

You only have to add a Form behavior, a Link to the Duplicate action and a view.

Usage

Duplicate View

In your Controller class add PcWeb\DuplicateController\Behaviors\DuplicateController to the list of implemented behaviors.

public $implement = [
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',
        'Backend\Behaviors\RelationController',
        'PcWeb\DuplicateController\Behaviors\DuplicateController'
    ];

in your update view or list view add an link to the duplicate/[model-id] action.

UpdateView:

<a href="<?= Backend::url('pcweb/myplugin/mycontroller/duplicate/', $formModel->id) ?>">Duplicate</a>

ListView:

<a href="<?= Backend::url('pcweb/myplugin/mycontroller/duplicate/', $recordId) ?>">Duplicate</a>

Create a file similar to the create.htm inside the controller views folder.

The file could look like follows:

<?php Block::put('breadcrumb') ?>
    <ul>
        <li>
            <a href="<?= Backend::url('pcweb/myplugin/mycontroller') ?>">MyController</a>
        </li>
        <li><?= e($this->pageTitle) ?></li>
    </ul>
<?php Block::endPut() ?>

<?php if (!$this->fatalError): ?>

    <?php Block::put('form-contents') ?>

    <div class="layout-row min-size">
        <?= $this->formRenderOutsideFields() ?>
    </div>

    <div class="layout-row">
        <?= $this->formRenderPrimaryTabs() ?>
    </div>

    <div class="form-buttons">
        <div class="loading-indicator-container">
            <button
                    type="submit"
                    data-request="onSave"
                    data-request-data="redirect:1"
                    data-hotkey="ctrl+s, cmd+s"
                    data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
                    class="btn btn-primary">
                <?= e(trans('backend::lang.form.save')) ?>
            </button>
            <button
                    type="button"
                    data-request="onSave"
                    data-request-data="close:1"
                    data-hotkey="ctrl+enter, cmd+enter"
                    data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
                    class="btn btn-default">
                <?= e(trans('backend::lang.form.save_and_close')) ?>
            </button>

            <button
                    type="button"
                    class="oc-icon-trash-o btn-icon danger pull-right"
                    data-request="onDelete"
                    data-load-indicator="<?= e(trans('backend::lang.form.deleting')) ?>"
                    data-request-confirm="<?= e(trans('backend::lang.form.confirm_delete')) ?>">
            </button>

            <span class="btn-text">
                    <?= e(trans('backend::lang.form.or')) ?> <a
                        href="<?= Backend::url('pcweb/myplugin/mycontroller') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>
                </span>
        </div>
    </div>

    <?php Block::endPut() ?>

    <?php Block::put('form-sidebar') ?>
        <div class="hide-tabs"><?= $this->formRenderSecondaryTabs() ?></div>
    <?php Block::endPut() ?>

    <?php Block::put('body') ?>
        <?= Form::open(['class' => 'layout stretch']) ?>
            <?= $this->makeLayout('form-with-sidebar') ?>
        <?= Form::close() ?>
    <?php Block::endPut() ?>

<?php else: ?>
    <p class="flash-message static error"><?= e(trans($this->fatalError)) ?></p>
    <p><a href="<?= Backend::url('pcweb/myplugin/mycontroller') ?>"
          class="btn btn-default"><?= e(trans('backend::lang.form.return_to_list')) ?></a></p>
<?php endif ?>

You have to update all lines containing Backend::url(...

A new Model opens with all attributes duplicated.

You can customize attributes to duplicate with the $duplicates property in the model. You can also include relations in this array. Relation duplicates are handled with Defered Bindings

For example: public $duplicates = ['relBelongsToMany', 'relBelongsTo', 'relHasOne', 'relHasMany', 'name'];

Extend Duplicate Logic

you can define the method formExtendDuplicate in your controller to handle additional actions after duplication.

public function formExtendDuplicate($model, $existing)
{
// ...
}

Known issues: Many2Many relations don't get duplicated, they are linked instead. If you would like to clone the rows inside the relation table, you have to use the formExtendDuplicate in the controller.

  • Found the plugin useful on 20 Nov, 2020

    I often use it with ease since it does the expected job !

  • Found the plugin not useful on 15 Apr, 2019

    Not working, missing documentation. I finally made it work but relations are not correctly populated.

  • author

    Replied on 24 Apr, 2019

    Can you please provide further information on what relations you want to duplicate. The best place to discuss is on the Product support Page.

  • Found the plugin not useful on 13 Apr, 2019

    Doesn't work actually (see issue : https://octobercms.com/plugin/support/pcweb-duplicatecontroller/duplicate-form-is-empty)

    Marked as not useful cause problem reported 3 weeks ago without any kind of support. Will update my review if the maintainer bring a solution

1.0.1

Initialize plugin.

Jan 24, 2019