Skip to content

Commit

Permalink
Autocompletely Stereotype OrchardCMS#16005
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzx86 committed May 10, 2024
1 parent 40455e1 commit 2e65031
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentTypes.ViewModels;
Expand All @@ -12,32 +13,34 @@ namespace OrchardCore.ContentTypes.Editors
public class ContentTypeSettingsDisplayDriver : ContentTypeDefinitionDisplayDriver
{
private static readonly ContentTypeDefinitionDriverOptions _defaultOptions = new();

private readonly IStereotypesProvider _stereotypesProvider;
private readonly ContentTypeDefinitionOptions _options;

protected readonly IStringLocalizer S;

public ContentTypeSettingsDisplayDriver(
IStringLocalizer<ContentTypeSettingsDisplayDriver> stringLocalizer,
IOptions<ContentTypeDefinitionOptions> options)
IOptions<ContentTypeDefinitionOptions> options,
IStereotypesProvider stereotypesProvider)
{
S = stringLocalizer;
_options = options.Value;
_stereotypesProvider = stereotypesProvider;
}

public override IDisplayResult Edit(ContentTypeDefinition contentTypeDefinition)
=> Initialize<ContentTypeSettingsViewModel>("ContentTypeSettings_Edit", model =>
=> Initialize<ContentTypeSettingsViewModel>("ContentTypeSettings_Edit", async model =>
{
var settings = contentTypeDefinition.GetSettings<ContentTypeSettings>();
var stereotypes = await _stereotypesProvider.GetStereotypesAsync();
model.Creatable = settings.Creatable;
model.Listable = settings.Listable;
model.Draftable = settings.Draftable;
model.Versionable = settings.Versionable;
model.Securable = settings.Securable;
model.Stereotype = settings.Stereotype;
model.Description = settings.Description;
model.Options = GetOptions(contentTypeDefinition, settings.Stereotype);
model.Options = await GetOptionsAsync(contentTypeDefinition, settings.Stereotype);
}).Location("Content:5");

public override async Task<IDisplayResult> UpdateAsync(ContentTypeDefinition contentTypeDefinition, UpdateTypeEditorContext context)
Expand All @@ -54,10 +57,11 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypeDefinition con
{
context.Updater.ModelState.AddModelError(nameof(ContentTypeSettingsViewModel.Stereotype), S["The stereotype should be alphanumeric."]);
}

var options = GetOptions(contentTypeDefinition, stereotype);

var options = await GetOptionsAsync(contentTypeDefinition, stereotype);

Apply(context, model, options);

return Edit(contentTypeDefinition);
}

Expand All @@ -84,21 +88,25 @@ private static void Apply(UpdateTypeEditorContext context, ContentTypeSettingsVi
}
}

private ContentTypeDefinitionDriverOptions GetOptions(ContentTypeDefinition contentTypeDefinition, string stereotype)
private async Task<ContentTypeDefinitionDriverOptions> GetOptionsAsync(ContentTypeDefinition contentTypeDefinition, string stereotype)
{
var options = _defaultOptions;

if (contentTypeDefinition.Name != null
&& _options.ContentTypes.TryGetValue(contentTypeDefinition.Name, out var typeOptions))
{
return typeOptions;
options = typeOptions;
}

if (stereotype != null
&& _options.Stereotypes.TryGetValue(stereotype, out var stereotypesOptions))
{
return stereotypesOptions;
options = stereotypesOptions;
}

return _defaultOptions;
options.Stereotypes = await _stereotypesProvider.GetStereotypesAsync();

return options;
}

private static bool IsAlphaNumericOrEmpty(string value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Provides custom content types management using admin section</Description>
<PackageTags>$(PackageTags) OrchardCoreCMS ContentManagement</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.ContentTypes.Services
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.ContentTypes.Services
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentTypes.Deployment;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.ContentTypes.RecipeSteps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@
<div class="mb-3">
<div class="w-md-75 w-xl-50">
<label asp-for="Stereotype">@T["Stereotype"]</label>
<input asp-for="Stereotype" type="text" class="form-control">
<input asp-for="Stereotype" list="stereotypeOptions" type="text" class="form-control">
<datalist id="stereotypeOptions">
@foreach (var item in Model.Options.Stereotypes)
{
<option value="@item.Stereotype">@item.DisplayName</option>
}
</datalist>
</div>
<span class="hint">@T["(Optional) The stereotype of the content type. e.g., Widget, MenuItem, ..."]</span>
</div>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.ContentTypes.Services;
namespace OrchardCore.ContentManagement;

public interface IStereotypeService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.ContentTypes.Services;
namespace OrchardCore.ContentManagement;

public interface IStereotypesProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace OrchardCore.ContentManagement.Metadata.Models;

public class ContentTypeDefinitionDriverOptions
Expand All @@ -11,4 +13,6 @@ public class ContentTypeDefinitionDriverOptions
public bool ShowVersionable { get; set; } = true;

public bool ShowSecurable { get; set; } = true;

public IEnumerable<StereotypeDescription> Stereotypes { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OrchardCore.ContentTypes.Services;
namespace OrchardCore.ContentManagement.Metadata.Models;

public class StereotypeDescription
{
Expand Down

0 comments on commit 2e65031

Please sign in to comment.