Skip to content

Instantly share code, notes, and snippets.

@RobJohnston
Last active August 16, 2020 01:49
Show Gist options
  • Save RobJohnston/afa1c6dfc7ab2c7f0c0fcef664621bfb to your computer and use it in GitHub Desktop.
Save RobJohnston/afa1c6dfc7ab2c7f0c0fcef664621bfb to your computer and use it in GitHub Desktop.
Extend the razor page's label tag helper to write labels the WET-BOEW way. Be sure to add `@addTagHelper *, WebApplication1` to the Pages/_ViewImports.cshtml file.
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Text.Encodings.Web;
using System.Threading;
using System.Threading.Tasks;
namespace WebApplication1.Extensions
{
/// <summary>
/// Extend the label tag helper to write labels the WET-BOEW way.
/// </summary>
/// <example>
/// https://wet-boew.github.io/wet-boew/demos/formvalid/formvalid-en.html.
/// </example>
[HtmlTargetElement("label", Attributes = "asp-for")]
public class LabelRequiredTagHelper : LabelTagHelper
{
private readonly string languageCode;
public LabelRequiredTagHelper(IHtmlGenerator generator)
: base(generator)
{
languageCode = Thread.CurrentThread.CurrentUICulture.ThreeLetterISOLanguageName;
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
await base.ProcessAsync(context, output);
if (For.Metadata.IsRequired)
{
// Add the "required" class to the label.
output.AddClass("required", HtmlEncoder.Default);
// Set the span tag around the original content of the label.
var content = output.Content.GetContent(NullHtmlEncoder.Default);
var spanTag = new TagBuilder("span");
spanTag.AddCssClass("field-name");
spanTag.InnerHtml.SetContent(content);
output.Content.SetHtmlContent(spanTag);
// Translate the "required" text, if necessary.
var requiredText = languageCode == "fra" ? "(obligatoire)" : "(required)";
// Append the strong after the span.
output.Content.Append(" ");
var strongTag = new TagBuilder("strong");
strongTag.AddCssClass("required");
strongTag.InnerHtml.Append(requiredText);
output.Content.AppendHtml(strongTag);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment