Page Editor: Clear Image Field from Sitecore Page Editor

Update October 3, 2012

Apparently, there was already a solution available. Apparently, they teach you this in Sitecore education these days. Apparently, my Google skills sucks. Here’s a better solution: http://mdpcode.be/blog/2012/05/page-editor-clear-image-button/ (Kudos @kayeeNL)

– – –

We had a need to enable web editors to clear image fields from the Page Editor (same as ‘clear’ button in Content Editor) but I was unable to find a way to do this using out of the box commands from Sitecore so I decided to roll my own command, here’s what I did;

 

Tested on Sitecore 6.5 but should apply to 6.4 (and probably any release with Page Editor).

1. Create the command class

This was a bit tricky. I hadn’t had the time to dive deep into Page Editor commands before so I ended up opening the Sitecore.Kernel.dll in ILSpy and located a similar command (the Sitecore.Shell.Applications.WebEdit.Commands.EditImage in this case). From there I grabbed the parts of code that seemed resonable; the Execute method and parts of the Run method. I kept the Execute method as it was and changed the Run method to the following (the key here is string value = String.Empty, that’s what is clearing the field):

protected static void Run(ClientPipelineArgs args)
{
    Assert.IsNotNull(args, "args");
    var item = Context.ContentDatabase.GetItem(args.Parameters["itemid"], ContentLanguage);
    Assert.IsNotNull(item, typeof(Item));
    var field = item.Fields[args.Parameters["fieldid"]];

    Assert.IsNotNull(field, typeof(Field));
    string str = args.Parameters["controlid"];

    string value = String.Empty;

    SheerResponse.SetAttribute("scHtmlValue", "value", value);
    SheerResponse.SetAttribute("scPlainValue", "value", string.Empty);
    SheerResponse.Eval("scSetHtmlValue('" + str + "')");
}

2. Command definition

I added the following line to the end of the App_Config/commands.config file:

<command name="webedit:removeimage" type="Website.WebEdit.Commands.RemoveImage, Website"/>

3. Add a button

To get a clickable button in Sitecore Page Editor, switch to the core database and fire up your Content Editor. Navigate to /sitecore/system/Field types/Simple Types/Image/WebEdit Buttons. Grab the Image properties item under WebEdit Buttons and duplicate it.

Change details to the following:

Name: Clear
Header: Clear Image
Icon: core3/32×32/undo.png
Click: chrome:field:editcontrol({command:”webedit:removeimage”})
Tooltip: Clear The Image

Feel free to change the fields as you see fit, just make sure that the Click field is mapped to your removeimage command defined in commands.config.

4. Remove images at will!

That’s it! If everything worked out, all image fields will have a Remove Image button in the Page Editor allowing the user to clear the field contents.

 

This should be applicable to other field types as well, since all we’re doing is clearing the field from its contents, but I haven’t tried any other fields.

 

Enjoy, and let me know how it works out for you!

Posted in Sitecore Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*