A RemoveLink command for PageEditor
Something that has bugged me for quite some time in the Sitecore Page Editor, is the inability to remove a Link field once one has been set (at least you cannot at the time of writing in Sitecore 7.5, perhaps 8 will alter this).
As of 7.5 and before, you can only edit the link via the Page Editor, and in that dialog there is no way to remove the existing link:
The only real way to remove the link is to shift over to the Content Editor and do it there. Whilst this works, if your editors are used to using the Page Editor it’s better if they can accomplish the task without navigating away.
Fortunately, it’s quite straight-forward to add a new command - one that clears any existing link in the field. Here’s the code for the command itself:
|
|
This code has been adapted from the existing EditLink
and ClearImage
commands, to mimic their behaviour.
The execution is very simple - first of all it checks to see if the field currently has a value in it, preventing it from clearing an already empty field. This prevents the page from being marked as ‘dirty’ and the user being warned that changes have been made if they try to navigate away without saving. If you feel the alert message here is overkill for the user, it could be removed so that in that branch of the code, it simply does nothing.
If there is a value, this just returns a javascript function call to clear the value from the field.
With this code in our solution, we need to add the command to config so that it can be referenced. This can done with a simple config file in the Include folder, with the appropriate assembly + class reference:
|
|
The last step is to create an Item in the Core database so that this command appears in the Page Editor for all Link Fields. In the folder at /sitecore/system/Field types/Link Types/General Link/WebEdit Buttons
, add a new WebEdit Button
item called Remove Link
.
Within this item, set the appropriate fields to reference the command and choose a nice icon:
With that in place, you will immediately see a new icon in the Page Editor:
And that’s it!
I hope that proves useful, it’s a pretty straight-forward solution. I do suspect though that this could be entirely accomplished through javascript, as there is little need for any server-side processing. Please leave a comment if you found this useful, or have any suggestions.