Closed ossedk closed 1 year ago
Moved to the ideas repo.
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'))
);
});
Indeed. We really need this feature. đŸ‘€
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!
Is this something the gents would accept being PR'd into core? I'd be happy to merge my Duplicator addon into core.
A small but useful function could be the ability to duplicate an entry:)