kirby-modules icon indicating copy to clipboard operation
kirby-modules copied to clipboard

Duplicate button

Open H-i-red opened this issue 8 years ago • 8 comments

Hi trying to implement a duplicate button in the nav bar, but with no bigger luck, any help is highly appreciated ! The idea was a simple shortcut (the plus icon) for adding fields without having to open up the modal and select the desired field from a select. Thanks..

skarmavbild 2016-06-27 kl 21 36 07

H-i-red avatar Jun 27 '16 19:06 H-i-red

Added a new public function to the controller.php and a new route in modules.php

controller.php

public function dope($path) {

        $path = explode('/', $path);
        $self = $this;
        $model = $this->model();
        $cache = $this->cache($model);
        $entry = $cache->get($path);

        $types = array(
            'type' => 'gallery'
        );

        $cache->add($path, $types);
        $self->redirect($model);
    }

But can´t figure out how to pass the type to the url('dope') function !? Now I just default it to "gallery"..

H-i-red avatar Jun 27 '16 19:06 H-i-red

hmm ok got the right type working now by appending the entry->type() to the url and then "pop" it, but would like to push the new entry right after the one I would like to duplicate, now it get last..

public function adder($path) {

        $path = explode('/', $path);
        $self = $this;
        $model = $this->model();
        $cache = $this->cache($model);
        $entry = $cache->get($path);
        $type = array_pop($path);

        $type = array(
            'type' => $type
        );

        $cache->add($path, $type);
        $self->redirect($model);
    }

H-i-red avatar Jun 27 '16 20:06 H-i-red

uhm ok I think I might over complicated things here.. oh dear..

H-i-red avatar Jun 27 '16 20:06 H-i-red

It could probably just do exactly what the Add modules button does;

https://github.com/frekw/kirby-modules/blob/master/fields/modules/template.php#L24

frekw avatar Jun 28 '16 07:06 frekw

No.. I was thinking of creating a button that adds a "entry" with out having to open a modal and select it from there, see image. So if I push the plus sign it creates just another grid entry beneath the one I clicked. Hope I make sense.. If I have a gallery "entry" and hit the plus sign it creates a gallery entry and so on..

H-i-red avatar Jun 28 '16 09:06 H-i-red

I am trying to send the entry type with href path on the button so I can duplicate the entry:

<a class="modules-entry-add" href="<?php __($field->url('adder') . '/' . $entry->type()) ?>"><?php i('plus') ?></a></li>

H-i-red avatar Jun 28 '16 09:06 H-i-red

That's unfortunately really hard to do due to the way Kirby renders fields. Which is the reason why it's done via modal in the current implementation.

In order to do it you would pretty much need to export the module hierarchy to Javascript (e.g what module can be under another module), pick the "right" module, insert a hidden field into the entire form, submit it and reload the page.

It would be nice for the UX, though!

frekw avatar Jun 28 '16 09:06 frekw

Well this works actually!

button :

<a class="modules-entry-add" href="<?php __($field->url('adder') . '/' . $entry->type()) ?>"><?php i('plus') ?></a></li>

function :

public function adder($path) {

        $path = explode('/', $path);
        $self = $this;
        $model = $this->model();
        $cache = $this->cache($model);
        $entry = $cache->get($path);
        $type = array_pop($path);

        $type = array(
            'type' => $type
        );

        $cache->add($path, $type);
        $self->redirect($model);
    }

routes :

array(
        'pattern' => 'adder/(:all)',
        'method'  => 'get|post',
        'action'  => 'adder'
      )

The only thing I am struggling with is the position of the newly created entry, it gets last, would like it to appear directly after the one I clicked..

H-i-red avatar Jun 28 '16 09:06 H-i-red