Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Autocomplete to Stereotype #16025

Merged
merged 15 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ namespace OrchardCore.ContentTypes.Editors
public class ContentTypeSettingsDisplayDriver : ContentTypeDefinitionDisplayDriver
{
private static readonly ContentTypeDefinitionDriverOptions _defaultOptions = new();

private readonly IStereotypeService _stereotypeService;
private readonly ContentTypeDefinitionOptions _options;

protected readonly IStringLocalizer S;

public ContentTypeSettingsDisplayDriver(
IStringLocalizer<ContentTypeSettingsDisplayDriver> stringLocalizer,
IOptions<ContentTypeDefinitionOptions> options)
IOptions<ContentTypeDefinitionOptions> options,
IStereotypeService stereotypeService)
{
S = stringLocalizer;
_options = options.Value;
_stereotypeService = stereotypeService;
}

public override IDisplayResult Edit(ContentTypeDefinition contentTypeDefinition)
=> Initialize<ContentTypeSettingsViewModel>("ContentTypeSettings_Edit", model =>
=> Initialize<ContentTypeSettingsViewModel>("ContentTypeSettings_Edit", async model =>
{
var settings = contentTypeDefinition.GetSettings<ContentTypeSettings>();

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 +56,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 +87,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 _stereotypeService.GetStereotypesAsync();

return options;
}

private static bool IsAlphaNumericOrEmpty(string value)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Modules;

namespace OrchardCore.ContentTypes.Services
{
public class StereotypeService : IStereotypeService
{
private readonly IEnumerable<IStereotypesProvider> _providers;
private readonly IContentDefinitionService _contentDefinitionService;
private readonly ILogger<StereotypeService> _logger;

public StereotypeService(IEnumerable<IStereotypesProvider> providers)
public StereotypeService(
IEnumerable<IStereotypesProvider> providers,
IContentDefinitionService contentDefinitionService,
ILogger<StereotypeService> logger)
{
_providers = providers;
_contentDefinitionService = contentDefinitionService;
_logger = logger;
}

public async Task<IEnumerable<StereotypeDescription>> GetStereotypesAsync()
{
var descriptions = new List<StereotypeDescription>();
var providerStereotypes = (await _providers.InvokeAsync(provider => provider.GetStereotypesAsync(), _logger)).ToList();

foreach (var provider in _providers)
var stereotypes = providerStereotypes.Select(providerStereotype => providerStereotype.Stereotype)
.ToHashSet(StringComparer.OrdinalIgnoreCase);

foreach (var contentType in await _contentDefinitionService.GetTypesAsync())
{
descriptions.AddRange(await provider.GetStereotypesAsync());
if (!contentType.TypeDefinition.TryGetStereotype(out var stereotype) ||
stereotypes.Contains(stereotype))
{
continue;
}

providerStereotypes.Add(new StereotypeDescription
{
Stereotype = stereotype,
DisplayName = stereotype
});
}

return descriptions;
return providerStereotypes.OrderBy(x => x.DisplayName);
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 All @@ -19,7 +20,6 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddScoped<IContentDefinitionService, ContentDefinitionService>();
services.AddScoped<IStereotypesProvider, DefaultStereotypesProvider>();
services.AddScoped<IStereotypeService, StereotypeService>();
services.AddScoped<IContentDefinitionDisplayHandler, ContentDefinitionDisplayCoordinator>();
services.AddScoped<IContentDefinitionDisplayManager, DefaultContentDefinitionDisplayManager>();
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
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.CustomSettings.Services;

public class CustomSettingsStereotypesProvider : IStereotypesProvider
{
protected readonly IStringLocalizer S;

public CustomSettingsStereotypesProvider(IStringLocalizer<CustomSettingsStereotypesProvider> stringLocalizer)
{
S = stringLocalizer;
}

public Task<IEnumerable<StereotypeDescription>> GetStereotypesAsync()
=> Task.FromResult<IEnumerable<StereotypeDescription>>(
[new StereotypeDescription { Stereotype = "CustomSettings", DisplayName = S["Custom Settings"] }]);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.CustomSettings.Deployment;
using OrchardCore.CustomSettings.Drivers;
Expand All @@ -22,7 +23,7 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddScoped<IDisplayDriver<ISite>, CustomSettingsDisplayDriver>();
services.AddScoped<CustomSettingsService>();

services.AddScoped<IStereotypesProvider, CustomSettingsStereotypesProvider>();
// Permissions
services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<IAuthorizationHandler, CustomSettingsAuthorizationHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.Menu.Services;

public class MenuItemStereotypesProvider : IStereotypesProvider
{
protected readonly IStringLocalizer S;

public MenuItemStereotypesProvider(IStringLocalizer<MenuItemStereotypesProvider> stringLocalizer)
{
S = stringLocalizer;
}

public Task<IEnumerable<StereotypeDescription>> GetStereotypesAsync()
=> Task.FromResult<IEnumerable<StereotypeDescription>>(
[new StereotypeDescription { Stereotype = "MenuItem", DisplayName = S["Menu Item"] }]);
}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Menu/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using OrchardCore.Menu.Drivers;
using OrchardCore.Menu.Handlers;
using OrchardCore.Menu.Models;
using OrchardCore.Menu.Services;
using OrchardCore.Menu.Settings;
using OrchardCore.Menu.TagHelpers;
using OrchardCore.Modules;
Expand All @@ -25,6 +26,8 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<INavigationProvider, AdminMenu>();

services.AddScoped<IStereotypesProvider, MenuItemStereotypesProvider>();

// MenuPart
services.AddScoped<IContentHandler, MenuContentHandler>();
services.AddContentPart<MenuPart>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.Users.Services;

public class CustomUserSettingsStereotypesProvider : IStereotypesProvider
{
protected readonly IStringLocalizer S;

public CustomUserSettingsStereotypesProvider(IStringLocalizer<CustomUserSettingsStereotypesProvider> stringLocalizer)
{
S = stringLocalizer;
}

public Task<IEnumerable<StereotypeDescription>> GetStereotypesAsync()
=> Task.FromResult<IEnumerable<StereotypeDescription>>(
[new StereotypeDescription { Stereotype = "CustomUserSettings", DisplayName = S["Custom User Settings"] }]);

}
2 changes: 2 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Users/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OrchardCore.Admin.Models;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Data;
using OrchardCore.Data.Migration;
Expand Down Expand Up @@ -468,6 +469,7 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IDisplayDriver<User>, CustomUserSettingsDisplayDriver>();
services.AddScoped<IPermissionProvider, CustomUserSettingsPermissions>();
services.AddDeployment<CustomUserSettingsDeploymentSource, CustomUserSettingsDeploymentStep, CustomUserSettingsDeploymentStepDriver>();
services.AddScoped<IStereotypesProvider, CustomUserSettingsStereotypesProvider>();

services.Configure<ContentTypeDefinitionOptions>(options =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.Widgets.Services;

public class WidgetStereotypesProvider : IStereotypesProvider
{
protected readonly IStringLocalizer S;

public WidgetStereotypesProvider(IStringLocalizer<WidgetStereotypesProvider> stringLocalizer)
{
S = stringLocalizer;
}

public Task<IEnumerable<StereotypeDescription>> GetStereotypesAsync()
=> Task.FromResult<IEnumerable<StereotypeDescription>>(
[new StereotypeDescription { Stereotype = "Widget", DisplayName = S["Widget"] }]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishamco here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the original code.

<datalist id="stereotypeOptions">
                <option value="CustomSettings">Custom Settings</option>
                <option value="CustomUserSettings">Custom User Settings</option>
                <option value="MenuItem">Menu Item</option>
                <option value="Widget">Widget</option>
        </datalist>

}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Widgets/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OrchardCore.ResourceManagement;
using OrchardCore.Widgets.Drivers;
using OrchardCore.Widgets.Models;
using OrchardCore.Widgets.Services;
using OrchardCore.Widgets.Settings;

namespace OrchardCore.Widgets
Expand All @@ -23,6 +24,8 @@ public override void ConfigureServices(IServiceCollection services)
services.AddContentPart<WidgetsListPart>()
.UseDisplayDriver<WidgetsListPartDisplayDriver>();

services.AddScoped<IStereotypesProvider, WidgetStereotypesProvider>();

services.AddScoped<IContentTypePartDefinitionDisplayDriver, WidgetsListPartSettingsDisplayDriver>();
services.AddContentPart<WidgetMetadata>();
services.AddDataMigration<Migrations>();
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 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; }
}
Loading