Added checks to properly display CustomForm content items at front end (#8751)

* Display CustomForm_Wrapper shape only if shape type is Detail.
Publish button has to be visible only if content is draftable AND is set to show the publish button

# Conflicts:
#	src/Orchard.Web/Modules/Orchard.CustomForms/Drivers/CustomFormPartDriver.cs
#	src/Orchard.Web/Modules/Orchard.CustomForms/Views/Item/Create.cshtml
#	src/Orchard.Web/Modules/Orchard.CustomForms/Views/Parts.CustomForm.Wrapper.cshtml

* Added EditorBuilderWrapper.
This commit is contained in:
Andrea Piovanelli 2024-01-26 09:12:09 +01:00 committed by GitHub
parent 4043df7a0d
commit 417af34b4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 136 additions and 40 deletions

View File

@ -11,20 +11,26 @@ using System;
using Orchard.Core.Contents.Settings;
using Orchard.Security;
using Orchard.Localization;
using Orchard.CustomForms.Services;
namespace Orchard.CustomForms.Drivers {
public class CustomFormPartDriver : ContentPartDriver<CustomFormPart> {
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IOrchardServices _orchardServices;
private readonly IAuthorizationService _authService;
private readonly IEditorBuilderWrapper _editorBuilderWrapper;
public CustomFormPartDriver(
IContentDefinitionManager contentDefinitionManager,
IOrchardServices orchardServices,
IAuthorizationService authService) {
IAuthorizationService authService,
IEditorBuilderWrapper editorBuilderWrapper) {
_contentDefinitionManager = contentDefinitionManager;
_orchardServices = orchardServices;
_authService = authService;
_editorBuilderWrapper = editorBuilderWrapper;
T = NullLocalizer.Instance;
}
@ -32,39 +38,43 @@ namespace Orchard.CustomForms.Drivers {
protected override DriverResult Display(CustomFormPart part, string displayType, dynamic shapeHelper) {
// this method is used by the widget to render the form when it is displayed
// Display CustomForm_Wrapper shape only if shape type is Detail.
if (displayType.Equals("Detail")) {
int contentId = 0;
var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
int contentId = 0;
var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
if (queryString.AllKeys.Contains("contentId")) {
int.TryParse(queryString["contentId"], out contentId);
}
if (queryString.AllKeys.Contains("contentId")) {
int.TryParse(queryString["contentId"], out contentId);
}
ContentItem contentItem;
if (contentId > 0) {
contentItem = _orchardServices.ContentManager.Get(contentId);
ContentItem contentItem;
if (contentId > 0) {
contentItem = _orchardServices.ContentManager.Get(contentId);
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.EditContent, contentItem))
return null;
} else {
contentItem = _orchardServices.ContentManager.New(part.ContentType);
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.EditContent, contentItem))
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
return null;
}
if (contentItem == null || contentItem.ContentType != part.ContentType)
return null;
} else {
contentItem = _orchardServices.ContentManager.New(part.ContentType);
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
if (!contentItem.Has<ICommonPart>()) {
return null;
}
return ContentShape("Parts_CustomForm_Wrapper", () => {
return shapeHelper.Parts_CustomForm_Wrapper()
.Editor(_editorBuilderWrapper.BuildEditor(contentItem))
.ContentPart(part);
});
}
if (contentItem == null || contentItem.ContentType != part.ContentType)
return null;
if (!contentItem.Has<ICommonPart>()) {
return null;
}
return ContentShape("Parts_CustomForm_Wrapper", () => {
return shapeHelper.Parts_CustomForm_Wrapper()
.Editor(_orchardServices.ContentManager.BuildEditor(contentItem))
.ContentPart(part);
});
// Returning null avoids rendering the edit shape for current custom form.
return null;
}
protected override DriverResult Editor(CustomFormPart part, dynamic shapeHelper) {

View File

@ -146,6 +146,8 @@
<Compile Include="Rules\CustomFormEvents.cs" />
<Compile Include="Rules\IRulesManager.cs" />
<Compile Include="Security\AuthorizationEventHandler.cs" />
<Compile Include="Services\EditorBuilderWrapper.cs" />
<Compile Include="Services\IEditorBuilderWrapper.cs" />
<Compile Include="ViewModels\CustomFormIndexViewModel.cs" />
<Compile Include="ViewModels\CustomFormPartEditViewModel.cs" />
</ItemGroup>
@ -231,4 +233,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
</Project>
</Project>

View File

@ -0,0 +1,22 @@
using Orchard.ContentManagement;
namespace Orchard.CustomForms.Services {
public class EditorBuilderWrapper : IEditorBuilderWrapper {
private readonly IContentManager _contentManager;
public EditorBuilderWrapper(
IContentManager contentManager) {
_contentManager = contentManager;
}
public dynamic BuildEditor(IContent content) {
return _contentManager.BuildEditor(content);
}
public dynamic UpdateEditor(IContent content, IUpdateModel updateModel) {
return _contentManager.UpdateEditor(content, updateModel);
}
}
}

View File

@ -0,0 +1,8 @@
using Orchard.ContentManagement;
namespace Orchard.CustomForms.Services {
public interface IEditorBuilderWrapper : IDependency {
dynamic BuildEditor(IContent content);
dynamic UpdateEditor(IContent content, IUpdateModel updateModel);
}
}

View File

@ -1,7 +1,13 @@
@using Orchard.ContentManagement
@using Orchard.Utility.Extensions
@using Orchard.ContentManagement.Aspects;
@using Orchard.ContentManagement.MetaData;
@using Orchard.ContentManagement.MetaData.Models;
@using Orchard.Core.Contents.Settings;
@{
IContentDefinitionManager _contentDefinitionManager = WorkContext.Resolve<IContentDefinitionManager>();
ContentItem customForm = Model.ContentItem;
string returnUrl = Model.ReturnUrl;
var metadata = customForm.ContentManager.GetItemMetadata(customForm);
@ -14,6 +20,18 @@
var submitButtonText = String.IsNullOrEmpty(Model.ContentItem.CustomFormPart.SubmitButtonText) ? T("Submit").Text : Model.ContentItem.CustomFormPart.SubmitButtonText;
var publishButtonText = String.IsNullOrEmpty(Model.ContentItem.CustomFormPart.PublishButtonText) ? T("Publish").Text : Model.ContentItem.CustomFormPart.PublishButtonText;
var showPublishButton = Model.ContentItem.CustomFormPart.SavePublishContentItem;
// Read type definition to check if content is draftable
var typeDefinition = _contentDefinitionManager
.ListTypeDefinitions()
.Where(x => String.Equals(x.Name, Model.ContentItem.CustomFormPart.ContentType, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (typeDefinition != null) {
// Publish button has to be visible only if content is draftable AND is set to show the publish button
showPublishButton = showPublishButton &&
typeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable;
}
}
@Display(New.Parts_Title().Title(displayText))
@ -29,8 +47,22 @@
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == false || Model.ContentItem.CustomFormPart.SaveContentItem == true) {
<button type="submit" name="submit.Save" value="submit.Save">@submitButtonText</button>
}
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == true) {
@if (showPublishButton) {
<button type="submit" name="submit.Publish" value="submit.Publish">@publishButtonText</button>
}
</fieldset>
<div class="edit-item-secondary group">
@if (Model.Actions != null) {
<div class="edit-item-actions">
@Display(Model.Actions)
</div>
}
@if (Model.Sidebar != null) {
<div class="edit-item-sidebar group">
@Display(Model.Sidebar)
</div>
}
</div>
}

View File

@ -1,4 +1,10 @@
@{
@using Orchard.ContentManagement.MetaData;
@using Orchard.ContentManagement.MetaData.Models;
@using Orchard.Core.Contents.Settings;
@{
IContentDefinitionManager _contentDefinitionManager = WorkContext.Resolve<IContentDefinitionManager>();
dynamic editor = Model.Editor;
if (TempData.ContainsKey("CustomFormWidget.InvalidCustomFormState")) {
@ -7,18 +13,34 @@
// remove default Save/Publish buttons
editor.Zones["Sidebar"].Items.Clear();
var showPublishButton = Model.ContentItem.CustomFormPart.SavePublishContentItem;
// Read type definition to check if content is draftable
var typeDefinition = _contentDefinitionManager
.ListTypeDefinitions()
.Where(x => String.Equals(x.Name, Model.ContentItem.CustomFormPart.ContentType, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (typeDefinition != null) {
// Publish button has to be visible only if content is draftable AND is set to show the publish button
showPublishButton = showPublishButton &&
typeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable;
}
}
@using (Html.BeginFormAntiForgeryPost(Url.Action("Create", "Item", new { area = "Orchard.CustomForms", id = Model.ContentItem.Id }))) {
@Html.ValidationSummary()
@Html.ValidationSummary()
// Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
@Display(editor)
@Html.Hidden("returnUrl", Request.RawUrl, new { id = string.Empty });
@Html.Hidden("contentId", !string.IsNullOrWhiteSpace(Request.QueryString["contentId"]) ? Request.QueryString["contentId"] : "0", new { id = string.Empty });
<fieldset class="submit-button">
<button type="submit" name="submit.Save" value="submit.Save">@Model.ContentPart.SubmitButtonText</button>
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == true) {
<button type="submit" name="submit.Publish" value="submit.Publish">@Model.ContentPart.PublishButtonText</button>
}
</fieldset>
}
@Display(editor)
@Html.Hidden("returnUrl", Request.RawUrl, new { id = string.Empty });
@Html.Hidden("contentId", !string.IsNullOrWhiteSpace(Request.QueryString["contentId"]) ? Request.QueryString["contentId"] : "0", new { id = string.Empty });
<fieldset class="submit-button">
<button type="submit" name="submit.Save" value="submit.Save">@Model.ContentPart.SubmitButtonText</button>
@if (showPublishButton) {
<button type="submit" name="submit.Publish" value="submit.Publish">@Model.ContentPart.PublishButtonText</button>
}
</fieldset>
}