2
2
using Our . Umbraco . TagHelpers . Extensions ;
3
3
using Our . Umbraco . TagHelpers . Services ;
4
4
using System . Text ;
5
+ using Microsoft . AspNetCore . Mvc . Rendering ;
6
+ using System . Threading . Tasks ;
7
+ using Umbraco . Cms . Core . Web ;
5
8
6
9
namespace Our . Umbraco . TagHelpers
7
10
{
@@ -14,113 +17,75 @@ namespace Our.Umbraco.TagHelpers
14
17
public class EditLinkTagHelper : TagHelper
15
18
{
16
19
private readonly IBackofficeUserAccessor _backofficeUserAccessor ;
20
+ private IUmbracoContextAccessor _umbracoContextAccessor ;
17
21
18
- public EditLinkTagHelper ( IBackofficeUserAccessor backofficeUserAccessor )
22
+ public EditLinkTagHelper ( IBackofficeUserAccessor backofficeUserAccessor , IUmbracoContextAccessor umbracoContextAccessor )
19
23
{
20
24
_backofficeUserAccessor = backofficeUserAccessor ;
25
+ _umbracoContextAccessor = umbracoContextAccessor ;
21
26
}
22
27
23
28
/// <summary>
24
- /// The id of the current content item. Defaults to 0
29
+ /// The id of the current content item
25
30
/// </summary>
26
31
[ HtmlAttributeName ( "content-id" ) ]
27
- public int ContentId { get ; set ; } = 0 ;
32
+ public int ContentId { get ; set ; } = int . MinValue ;
28
33
29
34
/// <summary>
30
35
/// Override the umbraco edit content url if yours is different
31
36
/// </summary>
32
37
[ HtmlAttributeName ( "edit-url" ) ]
33
38
public string EditUrl { get ; set ; } = "/umbraco#/content/content/edit/" ;
34
39
35
- /// <summary>
36
- /// Override the text of the link
37
- /// </summary>
38
- [ HtmlAttributeName ( "text" ) ]
39
- public string Text { get ; set ; } = "Edit" ;
40
-
41
40
/// <summary>
42
41
/// A boolean to say whether or not you would like to use the default styling.
43
42
/// </summary>
44
43
[ HtmlAttributeName ( "use-default-styles" ) ]
45
44
public bool UseDefaultStyles { get ; set ; } = false ;
46
45
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 )
78
47
{
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" ) ;
81
53
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 ( ) )
85
57
{
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
86
65
var editLinkUrl = $ "{ EditUrl } { ContentId } ";
87
66
88
- StringBuilder editLinkCode = new StringBuilder ( ) ;
89
- SetAttributeValue ( "style" , Style , editLinkCode ) ;
90
-
91
67
if ( UseDefaultStyles )
92
68
{
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 ( ) ) ;
97
73
}
98
74
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 ) ;
107
77
108
78
if ( UseDefaultStyles )
109
79
{
110
- SetAttributeValue ( "style" , GetLinkStyles ( ) , editLinkCode ) ;
80
+ output . Attributes . SetAttribute ( "style" , GetLinkStyles ( ) ) ;
111
81
}
112
82
113
- //Add the link text and close the link tag
114
- editLinkCode . Append ( $ ">{ Text } </a>") ;
115
-
116
83
if ( UseDefaultStyles )
117
84
{
118
85
//Add the closing outer div
119
- editLinkCode . Append ( $ "</div>" ) ;
86
+ output . PostElement . AppendHtml ( outerDiv . RenderEndTag ( ) ) ;
120
87
}
121
88
122
- //Set the content of the tag helper
123
- output . Content . SetHtmlContent ( editLinkCode . ToString ( ) ) ;
124
89
return ;
125
90
}
126
91
else
@@ -130,14 +95,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
130
95
}
131
96
}
132
97
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
-
141
98
/// <summary>
142
99
/// Helper method to get the link styles
143
100
/// </summary>
0 commit comments