When Editor cut and paste a page, then content will be simply moved from one place into another. It will get new URL, but nothing more will change. The copy and paste operation is more complicated. After Editor pasted a page, end users will be able to see a copy of the same content on the site. In some scenarios it would be good to have a better control on how exactly the content is copied. In this article I will describe my latest plugin that allows to use extended copy and paste functionality.
Site example
Let’s imagine we have a website with restaurants chain. Restaurant page type contains general information like name, description, location, type of cuisine and 50 other properties. But each restaurant page has additionally subpages:
- menu page – with menu offered by restaurant
- types of table pages – where user can check what kind of tables are available
- special offers – containing latest discounts and promotions
It’s a multilanguage site that supports 4 languages. The site is already available for end users.
Below is an example of site structure.
For now we have only two restaurants, in Stockholm and in Warsaw. But soon we would like to open another restaurant in Stockholm. Of course we will create a separate page for it. At the beginning we should have only English version with no details about the menu and type of tables.
We can create a new restaurant page from scratch. But it would be easier for Editor to copy „Alloy Stockholm” page and reuse most of the properties. When using default copy paste functionality, there can be some issues.
Published copy
The first problem is that the page will be automatically published. End users will be able to access the copied page immediately after it was copied. To avoid this we can create a project. All items pasted in project mode are drafts. But we would have to create a project to handle just one page. It would be better to simply copy the page as draft.
There is an answer in one of the forum threads that describes how to do this by intercepting “IContentCopyHandler” service.
Language branches
Another issue is related with the fact that our site is multilanguage. When copying a page we will copy English version, but also Swedish, Spanish and French branches. This means that we won’t be able to use language fallback behaviour. We would have to translate new page to all languages from the beginning. In this case, it would be preferable to copy only in the master language.
Page children
One last thing is that when copying restaurant page we will automatically copy all descendants which in this case is menu and list of tables. So next step would be to remove all subpages. It’s not very annoying because we have only two pages to remove, but it would be nice if we will be able to copy only restaurant page.
Extended content copy plugin
After a short introduction describing an example scenario, I can show a new addon. It solves the issues described above.
Plugin allows to:
- keep version as draft instead of copying published version
- copy only master language of the page
- do not copy descendants of the page
How to use an addon
The plugin can be used in two ways. We can either override the default behaviour of the paste command or we can show new “Extended paste” action in page tree context menu.
New action in context menu
In this mode, when Editor copies a page, the new command “Extended paste” will be available after clicking the context menu of the destination page:
After clicking the command, the dialog with available options will be displayed
In the restaurant example we can copy page with all options unselected. Copied page won’t be published nor translated, but also menu and tables pages won’t be copied.
Below you can see how it works when copying all descendants as drafts:
Options
Addon has few settings that are stored as Options. They define how extended paste behaves.
Option | Type | Description | Default value |
---|---|---|---|
Mode | Enum | Defines how plugin is working:
|
Command |
AllowedPasteActions | AllowedActions | Defines what actions are active: PublishOnDestination, CopyAllLanguageBranches and CopyDescendants | PublishOnDestination, CopyAllLanguageBranches, CopyDescendants are true |
PasteDefaults | Defaults | Defines default action values | PublishOnDestination is false while CopyAllLanguageBranches and CopyDescendants are true |
Below you can find an example for configuring options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[InitializableModule] public class ExtendedContentCopyInitialization : IConfigurableModule { public void ConfigureContainer(ServiceConfigurationContext context) { context.ConfigurationComplete += (o, e) => { // override default behaviour and allow to copy descendants context.Services.Configure<ExtendedContentCopyOptions>(options => { options.Mode = ExtendedContentCopyOptions.ExtendedContentCopyMode.Auto; options.AllowedPasteActions.CopyDescendants = true; }); }; } public void Initialize(InitializationEngine context) { } public void Uninitialize(InitializationEngine context) { } public void Preload(string[] parameters) { } } |
Overriding default copy behaviour
When Mode is set to auto, the edit mode command is not available. Editor won’t be able to decide how copy should work, but instead it will depend on configured Defaults values.
Below you can see how automatic mode is working for copying unpublished pages.
Source code is available on Github
There is also nuget package