Skip to content

Commit d3b588a

Browse files
author
Warren Buckley
committed
Refactor to make a little simpler - great work Paul :)
1 parent cc44030 commit d3b588a

File tree

1 file changed

+32
-75
lines changed

1 file changed

+32
-75
lines changed

Our.Umbraco.TagHelpers/EditLinkTagHelper.cs

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using Our.Umbraco.TagHelpers.Extensions;
33
using Our.Umbraco.TagHelpers.Services;
44
using System.Text;
5+
using Microsoft.AspNetCore.Mvc.Rendering;
6+
using System.Threading.Tasks;
7+
using Umbraco.Cms.Core.Web;
58

69
namespace Our.Umbraco.TagHelpers
710
{
@@ -14,113 +17,75 @@ namespace Our.Umbraco.TagHelpers
1417
public class EditLinkTagHelper : TagHelper
1518
{
1619
private readonly IBackofficeUserAccessor _backofficeUserAccessor;
20+
private IUmbracoContextAccessor _umbracoContextAccessor;
1721

18-
public EditLinkTagHelper(IBackofficeUserAccessor backofficeUserAccessor)
22+
public EditLinkTagHelper(IBackofficeUserAccessor backofficeUserAccessor, IUmbracoContextAccessor umbracoContextAccessor)
1923
{
2024
_backofficeUserAccessor = backofficeUserAccessor;
25+
_umbracoContextAccessor = umbracoContextAccessor;
2126
}
2227

2328
/// <summary>
24-
/// The id of the current content item. Defaults to 0
29+
/// The id of the current content item
2530
/// </summary>
2631
[HtmlAttributeName("content-id")]
27-
public int ContentId { get; set; } = 0;
32+
public int ContentId { get; set; } = int.MinValue;
2833

2934
/// <summary>
3035
/// Override the umbraco edit content url if yours is different
3136
/// </summary>
3237
[HtmlAttributeName("edit-url")]
3338
public string EditUrl { get; set; } = "/umbraco#/content/content/edit/";
3439

35-
/// <summary>
36-
/// Override the text of the link
37-
/// </summary>
38-
[HtmlAttributeName("text")]
39-
public string Text { get; set; } = "Edit";
40-
4140
/// <summary>
4241
/// A boolean to say whether or not you would like to use the default styling.
4342
/// </summary>
4443
[HtmlAttributeName("use-default-styles")]
4544
public bool UseDefaultStyles { get; set; } = false;
4645

47-
/// <summary>
48-
/// Set the id attribute if you want
49-
/// </summary>
50-
[HtmlAttributeName("id")]
51-
public string Id { get; set; } = "";
52-
53-
/// <summary>
54-
/// The class attribute for the link
55-
/// </summary>
56-
[HtmlAttributeName("class")]
57-
public string Class { get; set; } = "";
58-
59-
/// <summary>
60-
/// Set the target attribute if you want. Defaults to _blank
61-
/// </summary>
62-
[HtmlAttributeName("target")]
63-
public string Target { get; set; } = "_blank";
64-
65-
/// <summary>
66-
/// Add some inline styles to the link if you want
67-
/// </summary>
68-
[HtmlAttributeName("style")]
69-
public string Style { get; set; } = "";
70-
71-
/// <summary>
72-
/// Set the title attribute if you want
73-
/// </summary>
74-
[HtmlAttributeName("title")]
75-
public string Title { get; set; } = "";
76-
77-
public override void Process(TagHelperContext context, TagHelperOutput output)
46+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
7847
{
79-
//don't output the tag name as it's not valid HTML
80-
output.TagName = "";
48+
// Turn <our-edit-link> into an <a> tag
49+
output.TagName = "a";
50+
51+
// An outter wrapper div if we use inbuilt styling
52+
var outerDiv = new TagBuilder("div");
8153

82-
//check if the user is logged in to the backoffice
83-
//and they have access to the content section
84-
if(_backofficeUserAccessor.BackofficeUser.IsAllowedToSeeEditLink())
54+
// Check if the user is logged in to the backoffice
55+
// and they have access to the content section
56+
if (_backofficeUserAccessor.BackofficeUser.IsAllowedToSeeEditLink())
8557
{
58+
// Try & get Umbraco Current Node int ID (Only do this if ContentId has NOT been set)
59+
if (_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext) && ContentId == int.MinValue)
60+
{
61+
ContentId = umbracoContext.PublishedRequest.PublishedContent.Id;
62+
}
63+
64+
// Backoffice URL to content item
8665
var editLinkUrl = $"{EditUrl}{ContentId}";
8766

88-
StringBuilder editLinkCode = new StringBuilder();
89-
SetAttributeValue("style", Style, editLinkCode);
90-
9167
if (UseDefaultStyles)
9268
{
93-
//Render the outer div with some inline styles
94-
editLinkCode.Append($"<div");
95-
SetAttributeValue("style", GetOuterElementStyles(), editLinkCode);
96-
editLinkCode.Append($">");
69+
// Wrap the <a> in a <div>
70+
// Render the outer div with some inline styles
71+
outerDiv.Attributes.Add("style", GetOuterElementStyles());
72+
output.PreElement.AppendHtml(outerDiv.RenderStartTag());
9773
}
9874

99-
//Add the opening tag of the link
100-
editLinkCode.Append($"<a");
101-
102-
SetAttributeValue("href", editLinkUrl, editLinkCode);
103-
SetAttributeValue("id", Id, editLinkCode);
104-
SetAttributeValue("class", Class, editLinkCode);
105-
SetAttributeValue("target", Target, editLinkCode);
106-
SetAttributeValue("title", Title, editLinkCode);
75+
// Set the link on the <a> tag
76+
output.Attributes.SetAttribute("href", editLinkUrl);
10777

10878
if (UseDefaultStyles)
10979
{
110-
SetAttributeValue("style", GetLinkStyles(), editLinkCode);
80+
output.Attributes.SetAttribute("style", GetLinkStyles());
11181
}
11282

113-
//Add the link text and close the link tag
114-
editLinkCode.Append($">{Text}</a>");
115-
11683
if (UseDefaultStyles)
11784
{
11885
//Add the closing outer div
119-
editLinkCode.Append($"</div>");
86+
output.PostElement.AppendHtml(outerDiv.RenderEndTag());
12087
}
12188

122-
//Set the content of the tag helper
123-
output.Content.SetHtmlContent(editLinkCode.ToString());
12489
return;
12590
}
12691
else
@@ -130,14 +95,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
13095
}
13196
}
13297

133-
private static void SetAttributeValue(string attributeName, string attributeValue, StringBuilder editLinkCode)
134-
{
135-
if (!string.IsNullOrWhiteSpace(attributeValue))
136-
{
137-
editLinkCode.Append($" {attributeName}=\"{attributeValue}\"");
138-
}
139-
}
140-
14198
/// <summary>
14299
/// Helper method to get the link styles
143100
/// </summary>

0 commit comments

Comments
 (0)