Support localization for content search (backoffice) (#12618)
* Support localization for content search * Formatting Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> * Review changes * Review changes Co-authored-by: kjac <kjn@impact.dk> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
This commit is contained in:
21
src/Umbraco.Core/Trees/ISearchableTreeWithCulture.cs
Normal file
21
src/Umbraco.Core/Trees/ISearchableTreeWithCulture.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
|
||||
namespace Umbraco.Cms.Core.Trees
|
||||
{
|
||||
[Obsolete("This interface will be merged into ISearchableTree in Umbraco 12")]
|
||||
public interface ISearchableTreeWithCulture : ISearchableTree
|
||||
{
|
||||
/// <summary>
|
||||
/// Searches for results based on the entity type
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="searchFrom">
|
||||
/// A starting point for the search, generally a node id, but for members this is a member type alias
|
||||
/// </param>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
Task<EntitySearchResults> SearchAsync(string query, int pageSize, long pageIndex, string? searchFrom = null, string? culture = null);
|
||||
}
|
||||
}
|
||||
@@ -204,6 +204,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
return result;
|
||||
}
|
||||
|
||||
var culture = ClientCulture();
|
||||
var allowedSections = _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.AllowedSections.ToArray();
|
||||
|
||||
var searchTasks = new List<Task>();
|
||||
@@ -221,7 +222,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
var rootNodeDisplayName = Tree.GetRootNodeDisplayName(tree, _localizedTextService);
|
||||
if (rootNodeDisplayName is not null)
|
||||
{
|
||||
searchTasks.Add(ExecuteSearchAsync(query, searchableTree, rootNodeDisplayName,result));
|
||||
searchTasks.Add(ExecuteSearchAsync(query, culture, searchableTree, rootNodeDisplayName,result));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -232,13 +233,22 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
|
||||
private static async Task ExecuteSearchAsync(
|
||||
string query,
|
||||
string? culture,
|
||||
KeyValuePair<string, SearchableApplicationTree> searchableTree,
|
||||
string rootNodeDisplayName,
|
||||
ConcurrentDictionary<string, TreeSearchResult> result)
|
||||
{
|
||||
ISearchableTree searcher = searchableTree.Value.SearchableTree;
|
||||
const int pageSize = 200;
|
||||
IEnumerable<SearchResultEntity> results = (
|
||||
searcher is ISearchableTreeWithCulture searcherWithCulture
|
||||
? await searcherWithCulture.SearchAsync(query, pageSize, 0, culture: culture)
|
||||
: await searcher.SearchAsync(query, pageSize, 0))
|
||||
.WhereNotNull();
|
||||
|
||||
var searchResult = new TreeSearchResult
|
||||
{
|
||||
Results = (await searchableTree.Value.SearchableTree.SearchAsync(query, 200, 0)).WhereNotNull(),
|
||||
Results = results,
|
||||
TreeAlias = searchableTree.Key,
|
||||
AppAlias = searchableTree.Value.AppAlias,
|
||||
JsFormatterService = searchableTree.Value.FormatterService,
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Umbraco.Cms.Web.BackOffice.Trees
|
||||
[PluginController(Constants.Web.Mvc.BackOfficeTreeArea)]
|
||||
[CoreTree]
|
||||
[SearchableTree("searchResultFormatter", "configureContentResult", 10)]
|
||||
public class ContentTreeController : ContentTreeControllerBase, ISearchableTree
|
||||
public class ContentTreeController : ContentTreeControllerBase, ISearchableTreeWithCulture
|
||||
{
|
||||
private readonly UmbracoTreeSearcher _treeSearcher;
|
||||
private readonly ActionCollection _actions;
|
||||
@@ -369,8 +369,11 @@ namespace Umbraco.Cms.Web.BackOffice.Trees
|
||||
}
|
||||
|
||||
public async Task<EntitySearchResults> SearchAsync(string query, int pageSize, long pageIndex, string? searchFrom = null)
|
||||
=> await SearchAsync(query, pageSize, pageIndex, searchFrom, null);
|
||||
|
||||
public async Task<EntitySearchResults> SearchAsync(string query, int pageSize, long pageIndex, string? searchFrom = null, string? culture = null)
|
||||
{
|
||||
var results = _treeSearcher.ExamineSearch(query, UmbracoEntityTypes.Document, pageSize, pageIndex, out long totalFound, searchFrom);
|
||||
var results = _treeSearcher.ExamineSearch(query, UmbracoEntityTypes.Document, pageSize, pageIndex, out long totalFound, culture: culture, searchFrom: searchFrom);
|
||||
return new EntitySearchResults(results, totalFound);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user