statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
32 stars 1 forks source link

Duplicate entry #118

Closed ossedk closed 1 year ago

ossedk commented 4 years ago

A small but useful function could be the ability to duplicate an entry:)

jackmcdade commented 4 years ago

Moved to the ideas repo.

sauerbraten commented 4 years ago

We have this implemented as a custom action, but we also have a custom Entry class so our implementation is probably only of limited use:

app/Actions/DuplicateEntry.php:

<?php

namespace App\Actions;

use Statamic\Contracts\Entries\Entry;
use Statamic\Facades;
use Statamic\Facades\User;
use Statamic\Actions\Action;

class DuplicateEntry extends Action
{
    protected static $title = 'Duplicate';

    public function filter($item)
    {
        return $item instanceof Entry;
    }

    public function authorize($user, $item)
    {
        return $user->isSuper() || $user->hasPermission('duplicate '.$item->collection()->handle().' entries'); // this checks for a custom permission we introduced for this purpose, see below
    }

    public function run($items, $values)
    {
        foreach ($items as $item) {
            $item->id(null); // the entry repository will generate a new one automatically
            $item->set('title', $item->get('title').' (copy)');
            $item->slug($item->slug().'-copy');
            $item->published(false);

            // here we would null a few custom fields we never want duplicated

            Facades\Entry::save($item);
        }
    }
}

from app/Providers/AppServiceProvider.php, boot():

        $this->app->booted(function () {
            Facades\Permission::get('edit {collection} entries')->addChild(
                Facades\Permission::make('duplicate {collection} entries')
                    ->label(__('polygon.permission_duplicate_entries'))
            );
        });
ReneWeCode commented 4 years ago

Indeed. We really need this feature. đŸ‘€

jasonvarga commented 4 years ago

https://statamic.com/addons/double-three-digital/duplicator

dannyuk1982 commented 4 years ago

This would be great if this was part of core, especially as V2 had it.

It definitely feels like core functionality and not an add-on - and I say this as the guy that wrote the V2 addon before it became core back then!

duncanmcclean commented 4 years ago

Is this something the gents would accept being PR'd into core? I'd be happy to merge my Duplicator addon into core.