One of “must have” tasks in Sitecore implementation is editing meta data related with pages and components. Ideally content authors should have the ability to edit it directly from experience editor. I noticed in many implementation that “Edit Frames” are used to achieve this. Edit Frames are easy to implement from developers perspective (especially with Glass Mapper), but they may not be as convenient for content authors for meta data editing scenario, cause “Edit Frame” creates additional layer over components, which may be confusing for authors if editing something related with page context (like page’s meta title, or meta description). Actually there is an easy way to add editing capabilities to Sitecore, using additional ribbons and context menu buttons.
Page Metadata
Page metadata may be for example SEO related meta tags. The goal is to add new main menu item in the page context, which will trigger pop up containing fields from current page’s item.
Let’s start with adding new menu item in core database under “/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Page Editor/Edit” using one of the buttons template (e.g. “Small Button”). We need to fill the “Header”, “Icon” and “Tooltip” fields, but also enter command name “trigger:button:click” in “Click” field and a custom “ID”.
Cause we are modifying SPEAK component in the Experience Editor, now we have to switch to Sitecore Rocks to set up layout for our button item, created in previous step. We use “SmallButton” rendering and fill following fields:
- “AccessKey” – we enter here the names of fields, which we want to edit for our page items
- “Click” – should be same as “Click” property on the item
- “Command” – this is the name of javascript function which will be executed after clicking the button
- “PageCodeScriptFileName” – the path to .js file containing the function
Now we need to define js function for SPEAK command. The code will pass field names to the pipeline, which returns URL for editing area for given fields. Finally it opens the area in new dialog window. Mind that this code works on Sitecore 8.1 and above (for earlier versions check source article from references).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
define(["sitecore", "/-/speak/v1/ExperienceEditor/ExperienceEditor.js"], function (Sitecore, ExperienceEditor) { Sitecore.Commands.LaunchFieldEditor = { canExecute: function (context) { return true; }, execute: function (context) { context.currentContext.argument = context.button.viewModel.$el[0].accessKey; ExperienceEditor.PipelinesUtil.generateRequestProcessor("ExperienceEditor.GenerateFieldEditorUrl", function (response) { var dialogUrl = response.responseValue.value; var dialogFeatures = "dialogHeight: 380px;dialogWidth: 640px; edge:raised; center:yes; help:no; resizable:yes; status:no; scroll:no"; ExperienceEditor.Dialogs.showModalDialog(dialogUrl, '', dialogFeatures, null); }).execute(context); } }; }); |
In pipeline code we create field descriptors for every field passed as argument and return it as URL for the editing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System.Collections.Generic; using Sitecore.Data; using Sitecore.Text; using Sitecore.Shell.Applications.ContentEditor; using Sitecore.ExperienceEditor.Speak.Server.Requests; using Sitecore.ExperienceEditor.Speak.Server.Responses; using Sitecore.ExperienceEditor.Speak.Server.Contexts; namespace Sitecore.Foundation.FieldEditor.Pipelines.SpeakRequest.Processors { public class GenerateFieldEditorUrl : PipelineProcessorRequest<ItemContext> { public override PipelineProcessorResponseValue ProcessRequest() { return new PipelineProcessorResponseValue { Value = GenerateUrl() }; } private string GenerateUrl() { var fieldList = CreateFieldDescriptors(RequestContext.Argument); var fieldeditorOption = new FieldEditorOptions(fieldList); //Save item when ok button is pressed fieldeditorOption.SaveItem = true; return fieldeditorOption.ToUrlString().ToString(); } private List<FieldDescriptor> CreateFieldDescriptors(string fields) { var fieldList = new List<FieldDescriptor>(); var fieldString = new ListString(fields); foreach (string field in new ListString(fieldString)) fieldList.Add(new FieldDescriptor(RequestContext.Item, field)); return fieldList; } } } |
In last step we need to register the pipeline with config patch:
1 2 3 4 5 6 7 |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <sitecore.experienceeditor.speak.requests> <request name="ExperienceEditor.GenerateFieldEditorUrl" type="Sitecore.Foundation.FieldEditor.Pipelines.SpeakRequest.Processors.GenerateFieldEditorUrl, Sitecore.Foundation.FieldEditor" /> </sitecore.experienceeditor.speak.requests> </sitecore> </configuration> |
After deploying the code, new button should appear in the “Edit” section in the Experience Editor’s menu.
Clicking the button will trigger editorial area for page context metadata:
Component Metadata
Component related metadata are usually some properties which are not render directly in the view, but affect somehow the component, for example it may be style selector, or order of displayed elements.
Adding custom button to component’s context in Experience Editor is actually very easy and doesn’t require writing additional code. What we need is a custom experience editor button item in core database under “/sitecore/content/Applications/WebEdit/Custom Experience Buttons/”. We should create it using “Field Editor Button” template and fill “Header”, “Icon”, “Tooltip” and “Fields” fields. In the last one we enter names of the field we want to edit for selected component:
Now let’s switch back to master database and select the rendering of the component, where we want to edit meta data. In the “Experience Editor Buttons” field we select the item created in previous step:
After opening Experience Editor and selecting the component, we should have new button:
Clicking the button will open editorial area for fields, selected in first step: