The `IUmbNavMenuBuilderService` processes raw menu data from the property editor, resolving content, filtering items, and applying build options. Understanding this service is key to advanced UmbNav u
Overview
The Menu Builder Service is responsible for:
Content Resolution - Converting content keys to IPublishedContent
Image Resolution - Loading media items for menu images
Visibility Filtering - Hiding items based on authentication status
Active State Detection - Marking the current page and ancestors
Child Processing - Auto-expanding children from content nodes
Depth Limiting - Enforcing maximum nesting depth
Automatic Processing
When you call Model.Value<IEnumerable<UmbNavItem>>(), the built-in value converter automatically calls the Menu Builder Service. You typically don't need to call it manually.
However, for advanced scenarios, you can inject and use the service directly.
Manual Usage
Injecting the Service
@injectIUmbNavMenuBuilderServiceMenuBuilder
Or in a controller/service:
Basic Usage
With Build Options
Build Options Reference
The UmbNavBuildOptions class controls how the service processes items:
Option
Effect
RemoveNoopener
Sets Noopener to null on all items
RemoveNoreferrer
Sets Noreferrer to null on all items
HideIncludeChildren
Ignores the "Include Child Nodes" setting
RemoveDescription
Sets Description to null on all items
RemoveCustomClasses
Sets CustomClasses to null on all items
RemoveImages
Sets Image and ImageArray to null
MaxDepth
Limits nesting depth (0 = unlimited)
Processing Pipeline
The service processes items through these stages:
Practical Examples
Filtered Navigation
Create a navigation that only shows certain item types:
Different Depths for Different Views
Stripping Data for JSON API
Caching Processed Menus
Service Interface
Extending the Service
You can replace the service with a custom implementation. See Extending the Menu Builder Service for detailed guidance.
Quick example:
Register with:
Delivery API Support
The service automatically supports Umbraco's Content Delivery API. When items are requested through the Delivery API, the UmbNavValueConverter uses the same Menu Builder Service with options derived from the Data Type configuration.
Performance Considerations
Content Resolution - The service looks up content by GUID, which is fast with Umbraco's caching
Recursive Processing - Deep nesting increases processing time; use MaxDepth to limit
Authentication Check - Uses HttpContext.User, which is lightweight
Caching - Consider caching processed results for high-traffic sites
Error Handling
The service handles common error cases:
Missing Content - Items with unresolvable content keys are excluded
Unpublished Content - Unpublished content items are excluded
Null Items - Empty or null collections return empty results
Invalid Options - Null options default to UmbNavBuildOptions.Default
Errors are logged but don't throw exceptions, ensuring navigation degrades gracefully.
public class NavigationService
{
private readonly IUmbNavMenuBuilderService _menuBuilder;
public NavigationService(IUmbNavMenuBuilderService menuBuilder)
{
_menuBuilder = menuBuilder;
}
}
@inject IUmbNavMenuBuilderService MenuBuilder
@{
// Get raw items (bypassing the value converter)
var rawJson = Model.Value<string>("navigation");
var rawItems = JsonSerializer.Deserialize<IEnumerable<UmbNavItem>>(rawJson);
// Process with the service
var menuItems = MenuBuilder.BuildMenu(rawItems);
}
var options = new UmbNavBuildOptions
{
MaxDepth = 2,
RemoveDescription = true,
RemoveImages = true
};
var menuItems = MenuBuilder.BuildMenu(rawItems, options);
public class UmbNavBuildOptions
{
public bool RemoveNoopener { get; set; }
public bool RemoveNoreferrer { get; set; }
public bool HideIncludeChildren { get; set; }
public bool RemoveDescription { get; set; }
public bool RemoveCustomClasses { get; set; }
public bool RemoveImages { get; set; }
public int MaxDepth { get; set; }
public static UmbNavBuildOptions Default => new();
}
// In the value converter
public object? ConvertIntermediateToDeliveryApiObject(...)
{
var items = JsonSerializer.Deserialize<IEnumerable<UmbNavItem>>(inter.ToString()!);
var options = GetBuildOptions(propertyType);
return _umbNavMenuBuilderService.BuildMenu(items, options);
}