Skip to content

Commit

Permalink
Added checks to properly display CustomForm content items at front end (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
AndreaPiovanelli authored Jan 26, 2024
1 parent 4043df7 commit 417af34
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,70 @@
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;
}

public Localizer T { get; set; }

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))
return null;
} else {
contentItem = _orchardServices.ContentManager.New(part.ContentType);
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
return null;
}

if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
if (contentItem == null || contentItem.ContentType != part.ContentType)
return null;
}

if (contentItem == null || contentItem.ContentType != part.ContentType)
return null;
if (!contentItem.Has<ICommonPart>()) {
return null;
}

if (!contentItem.Has<ICommonPart>()) {
return null;
return ContentShape("Parts_CustomForm_Wrapper", () => {
return shapeHelper.Parts_CustomForm_Wrapper()
.Editor(_editorBuilderWrapper.BuildEditor(contentItem))
.ContentPart(part);
});
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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))
Expand All @@ -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>
}
Original file line number Diff line number Diff line change
@@ -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")) {
Expand All @@ -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>
}

0 comments on commit 417af34

Please sign in to comment.