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

Add hidden content type settnigs to allow hidding a content type from GraphQL Schema #13048

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
@@ -0,0 +1,41 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement.GraphQL.Options;
using OrchardCore.ContentManagement.GraphQL.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.ContentTypes.GraphQL.ViewModels;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.ContentTypes.GraphQL.Drivers;

public class GraphQLContentTypeSettingsDisplayDriver : ContentTypeDefinitionDisplayDriver
{
private readonly GraphQLContentOptions _contentOptions;

public GraphQLContentTypeSettingsDisplayDriver(IOptions<GraphQLContentOptions> optionsAccessor)
{
_contentOptions = optionsAccessor.Value;
}

public override IDisplayResult Edit(ContentTypeDefinition contentTypeDefinition)
{
return Initialize<GraphQLContentTypeSettingsViewModel>("GraphQLContentTypeSettings_Edit", model =>
{
model.Definition = contentTypeDefinition;
model.Settings = contentTypeDefinition.GetSettings<GraphQLContentTypeSettings>();
model.Options = _contentOptions;
}).Location("Content:5");
}

public override async Task<IDisplayResult> UpdateAsync(ContentTypeDefinition contentTypeDefinition, UpdateTypeEditorContext context)
{
var model = new GraphQLContentTypeSettingsViewModel();

await context.Updater.TryUpdateModelAsync(model, Prefix);

context.Builder.WithSettings(model.Settings);

return Edit(contentTypeDefinition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IContentTypeDefinitionDisplayDriver, GraphQLContentTypeSettingsDisplayDriver>();
services.AddScoped<IContentTypePartDefinitionDisplayDriver, GraphQLContentTypePartSettingsDriver>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.ContentManagement.GraphQL.Options;
using OrchardCore.ContentManagement.GraphQL.Settings;
using OrchardCore.ContentManagement.Metadata.Models;

namespace OrchardCore.ContentTypes.GraphQL.ViewModels
{
public class GraphQLContentTypeSettingsViewModel
{
public GraphQLContentTypeSettings Settings { get; set; }

[BindNever]
public GraphQLContentOptions Options { get; set; }

[BindNever]
public ContentTypeDefinition Definition { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
<label class="form-check-label" asp-for="Settings.Hidden">@T["Hide"]</label>
@if (hiddenByDefault)
{
<span class="hint">@T["NB. Setting is collapsed by default, and cannot be overwritten."]</span>
<span class="hint">@T["Setting is hidden by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to hide type from the GraphQL schema."]</span>
<span class="hint">@T["Check to hide part from the GraphQL schema."]</span>
}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@using OrchardCore.ContentTypes.GraphQL.ViewModels
@model GraphQLContentTypeSettingsViewModel

@{
var hiddenByDefault = Model.Options.IsHiddenByDefault(Model.Definition.Name);
var hidden = hiddenByDefault ? true : Model.Settings.Hidden;
}

<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="Settings.Hidden" checked="@hidden" asp-is-disabled="@hiddenByDefault" />
<label class="form-check-label" asp-for="Settings.Hidden">@T["Hide"]</label>
@if (hiddenByDefault)
{
<span class="hint">@T["Setting is hidden by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to hide type from the GraphQL schema."]</span>
}
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SiteLayersQuery(

public Task BuildAsync(ISchema schema)
{
if (_graphQLContentOptions.ShouldSkipContentType("SiteLayers"))
if (_graphQLContentOptions.IsHiddenByDefault("SiteLayers"))
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SiteCulturesQuery(
/// <inheritdocs/>
public Task BuildAsync(ISchema schema)
{
if (_graphQLContentOptions.ShouldSkipContentType("SiteCultures"))
if (_graphQLContentOptions.IsHiddenByDefault("SiteCultures"))
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MediaAssetQuery(

public Task BuildAsync(ISchema schema)
{
if (_graphQLContentOptions.ShouldSkipContentType("MediaAssets"))
if (_graphQLContentOptions.IsHiddenByDefault("MediaAssets"))
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal bool ShouldSkip(ContentTypePartDefinition definition)
return false;
}

public bool ShouldSkipContentType(string contentType)
public bool IsHiddenByDefault(string contentType)
{
if (String.IsNullOrEmpty(contentType))
{
Expand All @@ -168,6 +168,23 @@ public bool ShouldSkipContentType(string contentType)
return contentTypeOption?.Hidden ?? false;
}

public bool ShouldHide(ContentTypeDefinition definition)
{
if (definition == null)
{
throw new ArgumentNullException(nameof(definition));
}

var settings = definition.GetSettings<GraphQLContentTypeSettings>();

if (settings.Hidden)
{
return true;
}

return IsHiddenByDefault(definition.Name);
}

internal bool ShouldSkip(Type fieldType, string fieldName)
{
return HiddenFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task BuildAsync(ISchema schema)

foreach (var typeDefinition in contentDefinitionManager.ListTypeDefinitions())
{
if (_contentOptionsAccessor.Value.ShouldSkipContentType(typeDefinition.Name))
if (_contentOptionsAccessor.Value.ShouldHide(typeDefinition))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
var serviceProvider = _httpContextAccessor.HttpContext.RequestServices;
var contentFieldProviders = serviceProvider.GetServices<IContentFieldProvider>().ToList();

if (_contentOptions.ShouldSkipContentType(contentTypeDefinition.Name))
if (_contentOptions.ShouldHide(contentTypeDefinition))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
var serviceProvider = _httpContextAccessor.HttpContext.RequestServices;
var typeActivator = serviceProvider.GetService<ITypeActivatorFactory<ContentPart>>();

if (_contentOptions.ShouldSkipContentType(contentTypeDefinition.Name))
if (_contentOptions.ShouldHide(contentTypeDefinition))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OrchardCore.ContentManagement.GraphQL.Settings;

public class GraphQLContentTypeSettings
{
public bool Hidden { get; set; }
}