Created
April 15, 2017 03:21
-
-
Save robearlam/d5645454e208664f442394739d9247c6 to your computer and use it in GitHub Desktop.
Extending Sitecore Revolver to bulk update placeholder names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private const string RecursiveSwitch = "-r"; | |
private const string RenderingElementName = "r"; | |
private const string RenderingsFieldName = "__renderings"; | |
private const string DeviceElementName = "d"; | |
private const string PlaceholderAttributeName = "ph"; | |
private Context _context; | |
private bool _recursive; | |
private string _oldPlaceHolderName; | |
private string _newPlaceHolderName; | |
public void Initialise(Context context, IFormatContext formatContext) | |
{ | |
_context = context; | |
} | |
public CommandResult Run(string[] args) | |
{ | |
ParseSwitchArgs(args); | |
if (string.IsNullOrEmpty(_oldPlaceHolderName) || string.IsNullOrEmpty(_newPlaceHolderName)) | |
{ | |
return new CommandResult(CommandStatus.Failure, "Required arguements are missing, please use 'help rp' to view documentation"); | |
} | |
if (_context == null) | |
{ | |
return new CommandResult(CommandStatus.Failure, "No Context"); | |
} | |
if (_context.CurrentItem == null) | |
{ | |
return new CommandResult(CommandStatus.Failure, "No Context Item"); | |
} | |
return BuildResponse(ProcessPlaceHolderRename(_context.CurrentItem)); | |
} | |
private string ProcessPlaceHolderRename(Item currentItem) | |
{ | |
var outputText = ReplacePlaceholderNameForItem(currentItem); | |
if (_recursive) | |
{ | |
foreach (Item childItem in currentItem.Children) | |
{ | |
outputText += ProcessPlaceHolderRename(childItem); | |
} | |
} | |
return outputText; | |
} | |
private string ReplacePlaceholderNameForItem(Item item) | |
{ | |
if (item != null && !string.IsNullOrEmpty(item[RenderingsFieldName])) | |
{ | |
var renderingsElement = XElement.Parse(item[RenderingsFieldName]); | |
var devices = renderingsElement.Elements(DeviceElementName); | |
foreach (var device in devices) | |
{ | |
var renderings = device.Elements(RenderingElementName); | |
foreach (var rendering in renderings) | |
{ | |
TrySetAttributeName(rendering); | |
TrySetAttributeNameWithNamespace(rendering); | |
} | |
} | |
using (new global::Sitecore.Data.Items.EditContext(item)) | |
{ | |
item[RenderingsFieldName] = renderingsElement.ToString(); | |
} | |
return item.Paths.Path + "\r\n"; | |
} | |
return string.Empty; | |
} | |
private void TrySetAttributeNameWithNamespace(XElement rendering) | |
{ | |
XNamespace ns = "s"; | |
var itemPlaceHolderAttribute = rendering.Attributes().FirstOrDefault(x => x.Name == ns + PlaceholderAttributeName); | |
if (itemPlaceHolderAttribute != null && itemPlaceHolderAttribute.Value.Contains(_oldPlaceHolderName)) | |
{ | |
var newValue = itemPlaceHolderAttribute.Value.Replace(_oldPlaceHolderName, _newPlaceHolderName); | |
itemPlaceHolderAttribute.SetValue(newValue); | |
} | |
} | |
private void TrySetAttributeName(XElement rendering) | |
{ | |
var placeHolderAttribute = rendering.Attributes().FirstOrDefault(x => x.Name == PlaceholderAttributeName); | |
if (placeHolderAttribute != null && placeHolderAttribute.Value.Contains(_oldPlaceHolderName)) | |
{ | |
var newValue = placeHolderAttribute.Value.Replace(_oldPlaceHolderName, _newPlaceHolderName); | |
placeHolderAttribute.SetValue(newValue); | |
} | |
} | |
private CommandResult BuildResponse(string outputText) | |
{ | |
return new CommandResult(CommandStatus.Success, outputText); | |
} | |
private void ParseSwitchArgs(string[] args) | |
{ | |
_recursive = args.Any(x => x == RecursiveSwitch); | |
if (args.Length >= 2) | |
{ | |
_oldPlaceHolderName = args[0]; | |
_newPlaceHolderName = args[1]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bind RevolverExtensions.RenamePlaceholders,RevolverExtensions rp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rp old-name new-name -r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment