From 4ce6f5c3b0e18299ecf9ab931d2c74e1be0c2392 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 10 May 2018 18:20:33 +1000 Subject: [PATCH] Fixes how the name is ensured on the content tree so that the media tree doesn't break. --- .../Trees/ContentTreeController.cs | 49 +++++++++++++++++++ .../Trees/ContentTreeControllerBase.cs | 41 ++-------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Web/Trees/ContentTreeController.cs b/src/Umbraco.Web/Trees/ContentTreeController.cs index a68cd720fd..1054e4a7fa 100644 --- a/src/Umbraco.Web/Trees/ContentTreeController.cs +++ b/src/Umbraco.Web/Trees/ContentTreeController.cs @@ -191,6 +191,21 @@ namespace Umbraco.Web.Trees { var entity = GetEntityFromId(id); return HasPathAccess(entity, queryStrings); + } + + protected override IEnumerable GetChildEntities(string id, FormDataCollection queryStrings) + { + var result = base.GetChildEntities(id, queryStrings); + var culture = queryStrings["culture"].TryConvertTo(); + + //if this is null we'll set it to the default. + var cultureVal = (culture.Success ? culture.Result : null) ?? Services.LocalizationService.GetDefaultLanguageIsoCode(); + + // set names according to variations + foreach (var entity in result) + EnsureName(entity, cultureVal); + + return result; } /// @@ -227,6 +242,40 @@ namespace Umbraco.Web.Trees return menu; } + + /// + /// set name according to variations + /// + /// + /// + private void EnsureName(IEntitySlim entity, string culture) + { + if (culture == null) + { + if (string.IsNullOrWhiteSpace(entity.Name)) + entity.Name = "[[" + entity.Id + "]]"; + return; + } + + if (entity is IDocumentEntitySlim docEntity) + { + // we are getting the tree for a given culture, + // for those items that DO support cultures, we need to get the proper name, IF it exists + // otherwise, invariant is fine + + if (docEntity.Variations.Has(Core.Models.ContentVariation.CultureNeutral) && + docEntity.CultureNames.TryGetValue(culture, out var name) && + !string.IsNullOrWhiteSpace(name)) + { + entity.Name = name; + } + + if (string.IsNullOrWhiteSpace(entity.Name)) + entity.Name = "[[" + entity.Id + "]]"; + } + + throw new InvalidOperationException($"Cannot render a tree node for a culture when the entity isn't {typeof(IDocumentEntitySlim)}, instead it is {entity.GetType()}"); + } private void AddActionNode(IUmbracoEntity item, MenuItemCollection menu, bool hasSeparator = false, bool convert = false) where TAction : IAction diff --git a/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs b/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs index 53d9df5505..6c9ab0e76c 100644 --- a/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs +++ b/src/Umbraco.Web/Trees/ContentTreeControllerBase.cs @@ -149,8 +149,7 @@ namespace Umbraco.Web.Trees // get child entities - if id is root, but user's start nodes do not contain the // root node, this returns the start nodes instead of root's children - var culture = queryStrings["culture"].TryConvertTo(); - var entities = GetChildEntities(id, culture.Success ? culture.Result : null).ToList(); + var entities = GetChildEntities(id, queryStrings).ToList(); nodes.AddRange(entities.Select(x => GetSingleTreeNodeWithAccessCheck(x, id, queryStrings)).Where(x => x != null)); // if the user does not have access to the root node, what we have is the start nodes, @@ -182,7 +181,7 @@ namespace Umbraco.Web.Trees protected abstract UmbracoObjectTypes UmbracoObjectType { get; } - protected IEnumerable GetChildEntities(string id, string culture) + protected virtual IEnumerable GetChildEntities(string id, FormDataCollection queryStrings) { // try to parse id as an integer else use GetEntityFromId // which will grok Guids, Udis, etc and let use obtain the id @@ -208,45 +207,11 @@ namespace Umbraco.Web.Trees else { result = Services.EntityService.GetChildren(entityId, UmbracoObjectType).ToArray(); - } - - // should really never be null, but we'll error check anyways - culture = culture ?? Services.LocalizationService.GetDefaultLanguageIsoCode(); - - // set names according to variations - if (!culture.IsNullOrWhiteSpace()) - foreach (var entity in result) - EnsureName((IDocumentEntitySlim)entity, culture); + } return result; } - // set name according to variations - // - private void EnsureName(IDocumentEntitySlim entity, string culture) - { - if (culture == null) - { - if (string.IsNullOrWhiteSpace(entity.Name)) - entity.Name = "[[" + entity.Id + "]]"; - return; - } - - // we are getting the tree for a given culture, - // for those items that DO support cultures, we need to get the proper name, IF it exists - // otherwise, invariant is fine - - if (entity.Variations.Has(ContentVariation.CultureNeutral) && - entity.CultureNames.TryGetValue(culture, out var name) && - !string.IsNullOrWhiteSpace(name)) - { - entity.Name = name; - } - - if (string.IsNullOrWhiteSpace(entity.Name)) - entity.Name = "[[" + entity.Id + "]]"; - } - /// /// Returns true or false if the current user has access to the node based on the user's allowed start node (path) access ///