Link List Field Type: Provision to Edit Anchor Links

Well, first, I am thankful to Mr. Bergstrom for contributing Link List field type to Marketplace and thus making it quite easy for us when it comes to do anything with a collection of links. I have used it in multiple projects and multiple times.

I won’t say it’s a bug reporting, rather it’s a small enhancement that could be done in case you have a requirement for the Content Editor “to be able to EDIT the anchor links added to your link list field”.

Issue Description: Cannot Edit the Anchor links in Link List Field Type

By default, you can add different type of links to it e.g. Internal, external, media link and the anchor link. But recently our client had reported that, although they could add an anchor link but can’t edit it once added. It will simply open up a blank dialogue box as shown below:

LinkList1

 

LinkList2

 

And as a work around, they were simply removing the existing link and adding a new one, which didn’t sound great.

Then I started looking for the solution to it and the solution is as below:

The Solution:

In the LinkListField.cs file, go to the “EditNode” function which is responsible to open the correct user control for the selected link type. And add a new case for “anchor” links as highlighted below:

private void EditNode(int index, NameValueCollection arguments)
{
var argument = string.Empty;
var nodeByIndex = this.GetNodeByIndex(this.GetDocument(), index);
if (nodeByIndex != null)
{
if (nodeByIndex.Attributes != null)
{
var linkType = nodeByIndex.Attributes[“linktype”].Value;
var url = String.Empty;
switch (linkType)
{
case “external”:
{
url = “/sitecore/shell/Applications/Dialogs/External Link.aspx”;
break;
}
case “internal”:
{
url = “/sitecore/shell/Applications/Dialogs/Internal Link.aspx”;
break;
}
case “media”:
{
url = “/sitecore/shell/Applications/Dialogs/Media Link.aspx”;
break;
}
case”anchor”:
{
url = “/sitecore/shell/Applications/Dialogs/Anchor Link.aspx”;
break;
}
}
var parameters = new NameValueCollection();
parameters.Add(“url”, url);
parameters.Add(“link”, nodeByIndex.OuterXml);
parameters.Add(“index”, index.ToString());
parameters.Add(arguments);
Sitecore.Context.ClientPage.Start(this, “InsertLink”, parameters);
}
}
}

And then in the same code file, go to the Overridden function “HandleMessage” and add the highlighted code for Anchor Links, as shown below:

/// <summary>
/// Handle user manipulation of the field
/// </summary>
/// <param name=”message”></param>
public override void HandleMessage(Message message)
{
Assert.ArgumentNotNull(message, “message”);
base.HandleMessage(message);
if (message[“id”] == ID)
{
NameValueCollection additionalParameters = new NameValueCollection();
if (message[“link”] != null && !string.IsNullOrEmpty(message[“link”]))
{
additionalParameters.Add(“link”, message[“link”]);
}
switch (message.Name)
{
case “contentlink:internallink”:
Insert(“/sitecore/shell/Applications/Dialogs/Internal link.aspx”, additionalParameters);
return;
case “contentlink:media”:
{
additionalParameters.Add(“umwn”, “1”);
Insert(“/sitecore/shell/Applications/Dialogs/Media link.aspx”, additionalParameters);
return;
}
case “contentlink:externallink”:
{
Insert(“/sitecore/shell/Applications/Dialogs/External link.aspx”, additionalParameters);
return;
}
case “contentlink:anchorlink”:
{
Insert(“/sitecore/shell/Applications/Dialogs/Anchor link.aspx”, aditionalParameters);
return;
}
case “action:move”:
{
var direction = message.Arguments[“direction”];
var nodeIndex = int.Parse(message.Arguments[“index”]);
this.MoveNodeByIndex(direction, nodeIndex);
break;
}
case “action:delete”:
{
var s = message.Arguments[“remove”];
if (!string.IsNullOrEmpty(s))
{
foreach (var str4 in s.Split(‘,’))
{
var index = int.Parse(str4);
this.RemoveEntryByIndex(index);
Sitecore.Context.ClientPage.ClientResponse.Eval(“scContent.linklistRemoveLink(‘” + this.ID + (“‘, {index: ” + index + “})”));
}
}
break;
}
case “action:edit”:
{
this.EditNode(int.Parse(message.Arguments[“index”]), additionalParameters);
break;
}
}
}
}

Do a rebuild and publish. Now when we go to any of the added anchor links and try editing it, it will show the below screen, wherein we can make the changes and save, like we do for other type of links:

LinkList3

 

Please feel free to add your valuable comments/feedback. Thanks.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>