private void PopulateTermEntries(List <TermMenuEntry> termEntries, IEnumerable <ContentItem> contentItems, int level, TermMenuEntry parent = null)
        {
            foreach (var contentItem in contentItems)
            {
                var children = Array.Empty <ContentItem>();

                if (contentItem.Content.Terms is JArray termsArray)
                {
                    children = termsArray.ToObject <ContentItem[]>();
                }

                var termEntry = new TermMenuEntry
                {
                    Term   = contentItem,
                    Parent = parent,
                    Level  = level,
                    IsLeaf = children.Length == 0
                };

                termEntries.Add(termEntry);

                if (children.Length > 0)
                {
                    PopulateTermEntries(termEntries, children, level + 1, termEntry);
                }
            }
        }
        private async Task BuildMenuLevels(NavigationItemBuilder urlTreeRoot, List <TermMenuEntry> termMenuEntries, int level, RouteValueDictionary taxonomyRouteValues, TermMenuEntry parent = null)
        {
            foreach (var termMenuEntry in termMenuEntries.Where(x => x.Level == level))
            {
                if (parent != null && termMenuEntry.Parent != parent)
                {
                    continue;
                }

                //ContentItemMetadata cim = null;
                // Not all segments will have a content item associated with them.
                //if (termMenuEntry.ContentItem != null)
                //{
                //    cim = await _contentManager.PopulateAspectAsync<ContentItemMetadata>(termMenuEntry.ContentItem);
                //}
                //// TODO fix for list, which by default uses display.
                //if (cim != null)
                //{
                //    cim.AdminRouteValues["Action"] = "Edit";
                //}

                var localizedDisplayText = new LocalizedString(termMenuEntry.Term.DisplayText, termMenuEntry.Term.DisplayText);

                await urlTreeRoot.AddAsync(localizedDisplayText, async menuLevel =>
                {
                    if (termMenuEntry.Term != null)
                    {
                        // TODO Clone taxonomyroutevalues and add this in.
                        var termRouteValues = new RouteValueDictionary
                        {
                            { "Area", "OrchardCore.Contents" },
                            { "Controller", "Admin" },
                            { "Action", "Display" },
                            { "ContentItemId", taxonomyRouteValues["ContentItemId"] },
                            { "TaxonomyPart.TermContentItemId", termMenuEntry.Term.ContentItemId }
                        };
                        menuLevel.Action(termRouteValues["Action"] as string, termRouteValues["Controller"] as string, termRouteValues);
                        //menuLevel.Action(cim.AdminRouteValues["Action"] as string, cim.AdminRouteValues["Controller"] as string, cim.AdminRouteValues);
                        //menuLevel.Resource(termMenuEntry.ContentItem);
                        //var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(termMenuEntry.ContentItem.ContentType);
                        //menuLevel.Permission(ContentTypePermissions.CreateDynamicPermission(
                        //    ContentTypePermissions.PermissionTemplates[global::OrchardCore.Contents.Permissions.EditContent.Name], contentTypeDefinition));
                    }

                    //menuLevel.Caption(T["test"]);
                    await BuildMenuLevels(menuLevel, termMenuEntries, level + 1, taxonomyRouteValues, termMenuEntry);
                });
            }
        }