Picture of Fabian Holtermann
Software Engineer, Sitecore Certified Trainer & Expert

Today I learned Part 5

Sitecore Logo

Today I learned... ... that Sitecore will automatically render the link field description in MVC even if you use it as an enclosing tag. The “BeginField” and “EndField” won’t work if you want to set up a link on an image. So I created my own “BeginField” helper.

My Sitecore "beginfield" helper

To keep the functionality of the “parameters”, all I did was to look into them and search for the parameter, in my example “DisableLinkDescription”.





public static HtmlString BeginCustomField(this Sitecore.Mvc.Helpers.SitecoreHelper sitecoreHelper, string fieldName, object parameters = null)
{
    Assert.ArgumentNotNull(fieldName, "fieldName");
    var output = sitecoreHelper.BeginField(fieldName, parameters);
    var prop = parameters.GetType().GetProperties().FirstOrDefault(x => x.Name == "DisableLinkDescription");

    if (prop != null)
    {
        var value = (Boolean)Convert.ChangeType(prop.GetValue(parameters), typeof(Boolean));
        if (value)
        {
            var strippedLink = output.ToString();
            strippedLink = strippedLink.Substring(0, strippedLink.IndexOf(">", StringComparison.Ordinal) + 1);
            return new HtmlString(strippedLink);
        }

    }
    return output;
}        

Now you are able to insert an additional parameter to prevent Sitecore from rendering the link description.




@Html.Sitecore().BeginCustomField("Link", new { DisableLinkDescription = true })
@Html.Sitecore().Field("Image")
@Html.Sitecore().EndField()
  

Kommentare
Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail
E-Mail bei weiteren Kommentaren
Mein Kommentar