Skip to content

Commit a066876

Browse files
author
Warren Buckley
authored
Merge pull request #60 from Matthew-Wise/feature/optional-link
adds a fallback element feature to our-link
2 parents 7b2a60a + f136cd4 commit a066876

File tree

2 files changed

+133
-39
lines changed

2 files changed

+133
-39
lines changed

Our.Umbraco.TagHelpers/LinkTagHelper.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,42 @@ public class LinkTagHelper : TagHelper
1010
[HtmlAttributeName("Link")]
1111
public Link? Link { get; set; }
1212

13+
/// <summary>
14+
/// If the link should render child content even if there is no link
15+
/// </summary>
16+
[HtmlAttributeName("Fallback")]
17+
public bool Fallback { get; set; }
18+
19+
/// <summary>
20+
/// element to replace the anchor with when there is no link i.e div
21+
/// </summary>
22+
[HtmlAttributeName("FallbackElement")]
23+
public string? FallbackElement { get; set; }
24+
1325
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
1426
{
15-
// Ensure we have a Url set on the LinkPicker property editor in Umbraco
16-
if (string.IsNullOrWhiteSpace(Link?.Url))
17-
{
18-
output.SuppressOutput();
19-
return;
20-
}
21-
2227
// If the <our-link /> is self closing
2328
// Ensure that our <a></a> always has a matching end tag
2429
output.TagMode = TagMode.StartTagAndEndTag;
25-
2630
output.TagName = "a";
31+
2732
var childContent = await output.GetChildContentAsync();
2833

34+
// Ensure we have a Url set on the LinkPicker property editor in Umbraco
35+
if (string.IsNullOrWhiteSpace(Link?.Url))
36+
{
37+
if (Fallback && !childContent.IsEmptyOrWhiteSpace)
38+
{
39+
output.TagName = FallbackElement;
40+
}
41+
else
42+
{
43+
output.SuppressOutput();
44+
}
45+
46+
return;
47+
}
48+
2949
// If we use the TagHelper <umb-link></umb-link>
3050
// Without child DOM elements then it will use the Link Name property inside the <a> it generates
3151
if (childContent.IsEmptyOrWhiteSpace)

0 commit comments

Comments
 (0)