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

Utilize ContentTypeExtensions #12231

Merged
merged 11 commits into from
Sep 16, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Contents;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.ModelBinding;
Expand Down Expand Up @@ -109,9 +109,7 @@ public async Task<IActionResult> Manage()
var dashboardCreatable = new List<SelectListItem>();

var widgetContentTypes = _contentDefinitionManager.ListTypeDefinitions()
.Where(t =>
!string.IsNullOrEmpty(t.GetSettings<ContentTypeSettings>().Stereotype) &&
t.GetSettings<ContentTypeSettings>().Stereotype.Contains("DashboardWidget"))
.Where(t => t.TryGetStereotype(out var stereotype) && stereotype.Contains("DashboardWidget"))
.OrderBy(x => x.DisplayName);
foreach (var ctd in widgetContentTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;

namespace OrchardCore.ContentFields.Controllers
{
Expand Down Expand Up @@ -61,9 +60,9 @@ public async Task<IActionResult> SearchContentItems(string part, string field, s
contentTypes = _contentDefinitionManager.ListTypeDefinitions()
.Where(contentType =>
{
var stereotype = contentType.GetSettings<ContentTypeSettings>().Stereotype;
var hasSteretype = contentType.TryGetStereotype(out var stereotype);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

return !String.IsNullOrEmpty(stereotype) && fieldSettings.DisplayedStereotypes.Contains(stereotype);
return hasSteretype && fieldSettings.DisplayedStereotypes.Contains(stereotype);
}).Select(contentType => contentType.Name)
.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Records;
using YesSql;
using YesSql.Services;
Expand Down Expand Up @@ -32,7 +32,7 @@ public async Task<IEnumerable<ContentPickerResult>> Search(ContentPickerSearchCo
{
contentTypes = _contentDefinitionManager
.ListTypeDefinitions()
.Where(x => string.IsNullOrEmpty(x.GetSettings<ContentTypeSettings>().Stereotype))
.Where(x => !x.HasStereotype())
.Select(x => x.Name)
.AsEnumerable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<span class="badge rounded-pill ta-badge font-weight-normal" data-bs-toggle="tooltip" title="@T["Technical Name"]"><i class="fas fa-file-alt text-danger" aria-hidden="true"></i> @Model.Name</span>

@if (!string.IsNullOrWhiteSpace(settings.Stereotype))
@if (!String.IsNullOrWhiteSpace(settings.Stereotype))
{
<span class="badge rounded-pill ta-badge font-weight-normal" data-bs-toggle="tooltip" title="Stereotype"><i class="fa fa-file text-info" aria-hidden="true"></i> @settings.Stereotype</span>
}
Expand All @@ -24,7 +24,7 @@
</div>
</div>

@if (!string.IsNullOrWhiteSpace(settings.Description))
@if (!String.IsNullOrWhiteSpace(settings.Description))
{
<div class="row">
<div class="col">
Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Contents/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using OrchardCore.Admin.Models;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Contents.Controllers;
using OrchardCore.Contents.Security;
using OrchardCore.Entities;
Expand Down Expand Up @@ -56,7 +56,7 @@ public async Task BuildNavigationAsync(string name, NavigationBuilder builder)
}

var contentTypeDefinitions = _contentDefinitionManager.ListTypeDefinitions().OrderBy(d => d.Name);
var contentTypes = contentTypeDefinitions.Where(ctd => ctd.GetSettings<ContentTypeSettings>().Creatable).OrderBy(ctd => ctd.DisplayName);
var contentTypes = contentTypeDefinitions.Where(ctd => ctd.IsCreatable()).OrderBy(ctd => ctd.DisplayName);
await builder.AddAsync(S["Content"], NavigationConstants.AdminMenuContentPosition, async content =>
{
content.AddClass("content").Id("content");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
Expand All @@ -29,7 +29,7 @@ public override IDisplayResult Display(ContentTypesAdminNode treeNode)
public override IDisplayResult Edit(ContentTypesAdminNode treeNode)
{
var listable = _contentDefinitionManager.ListTypeDefinitions()
.Where(ctd => ctd.GetSettings<ContentTypeSettings>().Listable)
.Where(ctd => ctd.IsListable())
.OrderBy(ctd => ctd.DisplayName).ToList();

var entries = listable.Select(x => new ContentTypeEntryViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using OrchardCore.AdminMenu.Services;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Contents.Security;
using OrchardCore.Navigation;

Expand Down Expand Up @@ -86,7 +85,7 @@ public async Task BuildNavigationAsync(MenuItem menuItem, NavigationBuilder buil
private IEnumerable<ContentTypeDefinition> GetContentTypesToShow(ContentTypesAdminNode node)
{
var typesToShow = _contentDefinitionManager.ListTypeDefinitions()
.Where(ctd => ctd.GetSettings<ContentTypeSettings>().Listable);
.Where(ctd => ctd.IsListable());

if (!node.ShowAll)
{
Expand All @@ -98,7 +97,7 @@ private IEnumerable<ContentTypeDefinition> GetContentTypesToShow(ContentTypesAdm
return typesToShow.OrderBy(t => t.DisplayName);
}

private List<string> GetIconClasses(ContentTypeDefinition contentType, ContentTypesAdminNode node)
private static List<string> GetIconClasses(ContentTypeDefinition contentType, ContentTypesAdminNode node)
{
if (node.ShowAll)
{
Expand All @@ -114,12 +113,12 @@ private List<string> GetIconClasses(ContentTypeDefinition contentType, ContentTy
}
}

private List<string> AddPrefixToClasses(string unprefixed)
private static List<string> AddPrefixToClasses(string unprefixed)
{
return unprefixed?.Split(' ')
.ToList()
.Select(c => "icon-class-" + c)
.ToList<string>()
.ToList()
?? new List<string>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using OrchardCore.ContentManagement.Display;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Contents.Services;
using OrchardCore.Contents.ViewModels;
Expand Down Expand Up @@ -91,7 +90,7 @@ public async Task<IActionResult> List(
string contentTypeId = "")
{
var contentTypeDefinitions = _contentDefinitionManager.ListTypeDefinitions()
.Where(ctd => IsCreatable(ctd))
.Where(ctd => ctd.IsCreatable())
.OrderBy(ctd => ctd.DisplayName);

if (!await _authorizationService.AuthorizeContentTypeDefinitionsAsync(User, CommonPermissions.ViewContent, contentTypeDefinitions, _contentManager))
Expand Down Expand Up @@ -126,7 +125,7 @@ public async Task<IActionResult> List(
var creatableList = new List<SelectListItem>();

// Allows non creatable types to be created by another admin page.
if (IsCreatable(contentTypeDefinition) || options.CanCreateSelectedContentType)
if (contentTypeDefinition.IsCreatable() || options.CanCreateSelectedContentType)
{
var contentItem = await CreateContentItemForOwnedByCurrentAsync(contentTypeDefinition.Name);

Expand All @@ -148,7 +147,7 @@ public async Task<IActionResult> List(
{
var contentItem = await CreateContentItemForOwnedByCurrentAsync(contentTypeDefinition.Name);

if (IsCreatable(contentTypeDefinition) && await IsAuthorizedAsync(CommonPermissions.EditContent, contentItem))
if (contentTypeDefinition.IsCreatable() && await IsAuthorizedAsync(CommonPermissions.EditContent, contentItem))
{
creatableList.Add(new SelectListItem(contentTypeDefinition.DisplayName, contentTypeDefinition.Name));
}
Expand Down Expand Up @@ -194,7 +193,7 @@ public async Task<IActionResult> List(

foreach (var ctd in _contentDefinitionManager.ListTypeDefinitions())
{
if (!ctd.GetSettings<ContentTypeSettings>().Listable)
if (!ctd.IsListable())
{
continue;
}
Expand Down Expand Up @@ -722,11 +721,6 @@ public async Task<IActionResult> Unpublish(string contentItemId, string returnUr
return Url.IsLocalUrl(returnUrl) ? (IActionResult)this.LocalRedirect(returnUrl, true) : RedirectToAction(nameof(List));
}

private static bool IsCreatable(ContentTypeDefinition contentTypeDefinition)
{
return contentTypeDefinition.GetSettings<ContentTypeSettings>().Creatable;
}

private async Task<ContentItem> CreateContentItemForOwnedByCurrentAsync(string contentType)
{
var contentItem = await _contentManager.NewAsync(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.ContentManagement.Display.ViewModels;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;

Expand Down Expand Up @@ -44,14 +44,9 @@ public override IDisplayResult Display(ContentItem contentItem, IUpdateModel upd
{
contentsMetadataShape.Displaying(ctx =>
{
var stereotype = "";
var settings = contentTypeDefinition?.GetSettings<ContentTypeSettings>();
if (settings != null)
{
stereotype = settings.Stereotype;
}
var hasStereotype = contentTypeDefinition.TryGetStereotype(out var stereotype);
if (!String.IsNullOrEmpty(stereotype) && !String.Equals("Content", stereotype, StringComparison.OrdinalIgnoreCase))
if (hasStereotype && !String.Equals("Content", stereotype, StringComparison.OrdinalIgnoreCase))
{
ctx.Shape.Metadata.Alternates.Add($"{stereotype}__ContentsMetadata");
}
Expand All @@ -62,12 +57,11 @@ public override IDisplayResult Display(ContentItem contentItem, IUpdateModel upd
{
ctx.Shape.Metadata.Alternates.Add($"ContentsMetadata_{ctx.Shape.Metadata.DisplayType}");
if (!String.IsNullOrEmpty(stereotype) && !String.Equals("Content", stereotype, StringComparison.OrdinalIgnoreCase))
if (hasStereotype && !String.Equals("Content", stereotype, StringComparison.OrdinalIgnoreCase))
{
ctx.Shape.Metadata.Alternates.Add($"{stereotype}_{displayType}__ContentsMetadata");
}
}
});

results.Add(contentsMetadataShape);
Expand Down Expand Up @@ -113,7 +107,7 @@ public override IDisplayResult Edit(ContentItem contentItem, IUpdateModel update
results.Add(Dynamic("Content_SaveDraftButton").Location("Actions:20")
.RenderWhen(async () =>
{
if (contentTypeDefinition.GetSettings<ContentTypeSettings>().Draftable &&
if (contentTypeDefinition.IsDraftable() &&
await _authorizationService.AuthorizeAsync(context.User, CommonPermissions.EditContent, contentItem))
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Security.Permissions;

namespace OrchardCore.Contents.Security
Expand All @@ -20,7 +20,7 @@ public Task<IEnumerable<Permission>> GetPermissionsAsync()
{
// manage rights only for Securable types
var securableTypes = _contentDefinitionManager.ListTypeDefinitions()
.Where(ctd => ctd.GetSettings<ContentTypeSettings>().Securable);
.Where(ctd => ctd.IsSecurable());

var result = new List<Permission>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Contents.Security;
using OrchardCore.Contents.ViewModels;
Expand Down Expand Up @@ -168,7 +167,7 @@ public void Build(QueryEngineBuilder<ContentItem> builder)
foreach (var ctd in contentDefinitionManager.ListTypeDefinitions())
{
if (!ctd.GetSettings<ContentTypeSettings>().Listable)
if (!ctd.IsListable())
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Contents.ViewModels;

namespace OrchardCore.Contents.ViewComponents
Expand All @@ -21,15 +21,15 @@ public IViewComponentResult Invoke(IEnumerable<string> selectedContentTypes, str
{
if (selectedContentTypes == null)
{
selectedContentTypes = new string[0];
selectedContentTypes = Array.Empty<string>();
}

var contentTypes = ContentTypeSelection.Build(_contentDefinitionManager, selectedContentTypes);

if (!String.IsNullOrEmpty(stereotype))
{
contentTypes = contentTypes
.Where(x => x.ContentTypeDefinition.GetSettings<ContentTypeSettings>().Stereotype == stereotype)
.Where(x => x.ContentTypeDefinition.GetStereotypeOrDefault() == stereotype)
.ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Settings;

namespace OrchardCore.CustomSettings.Services
Expand Down Expand Up @@ -37,7 +36,7 @@ public CustomSettingsService(
_settingsTypes = new Lazy<IDictionary<string, ContentTypeDefinition>>(
() => _contentDefinitionManager
.ListTypeDefinitions()
.Where(x => x.GetSettings<ContentTypeSettings>().Stereotype == "CustomSettings")
.Where(x => x.GetStereotypeOrDefault() == "CustomSettings")
.ToDictionary(x => x.Name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using OrchardCore.ContentManagement.Display;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.Flows.Models;
Expand Down Expand Up @@ -112,12 +111,12 @@ private IEnumerable<ContentTypeDefinition> GetContainedContentTypes(string conte

if (settings == null || settings.ContainedContentTypes == null || !settings.ContainedContentTypes.Any())
{
return _contentDefinitionManager.ListTypeDefinitions().Where(t => t.GetSettings<ContentTypeSettings>().Stereotype == "Widget");
return _contentDefinitionManager.ListTypeDefinitions().Where(t => t.GetStereotypeOrDefault() == "Widget");
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}

return settings.ContainedContentTypes
.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType))
.Where(t => t != null && t.GetSettings<ContentTypeSettings>().Stereotype == "Widget");
.Where(t => t != null && t.GetStereotypeOrDefault() == "Widget");
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using OrchardCore.ContentManagement.Display.Models;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Contents;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Flows.Models;
Expand Down Expand Up @@ -205,9 +204,7 @@ private static bool IsSecurable(IContentDefinitionManager contentDefinitionManag
{
contentTypeDefinition = contentDefinitionManager.GetTypeDefinition(contentType);

var settings = contentTypeDefinition.GetSettings<ContentTypeSettings>();

return settings.Securable;
return contentTypeDefinition.IsSecurable();
}

private async Task<IEnumerable<ContentTypeDefinition>> GetContainedContentTypesAsync(IContentDefinitionManager contentDefinitionManager, ContentTypePartDefinition typePartDefinition)
Expand Down
Loading