Skip to content

Commit 417af34

Browse files
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.
1 parent 4043df7 commit 417af34

File tree

6 files changed

+135
-39
lines changed

6 files changed

+135
-39
lines changed

src/Orchard.Web/Modules/Orchard.CustomForms/Drivers/CustomFormPartDriver.cs

+35-25
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,70 @@
1111
using Orchard.Core.Contents.Settings;
1212
using Orchard.Security;
1313
using Orchard.Localization;
14+
using Orchard.CustomForms.Services;
1415

1516
namespace Orchard.CustomForms.Drivers {
1617
public class CustomFormPartDriver : ContentPartDriver<CustomFormPart> {
1718
private readonly IContentDefinitionManager _contentDefinitionManager;
1819
private readonly IOrchardServices _orchardServices;
1920
private readonly IAuthorizationService _authService;
21+
private readonly IEditorBuilderWrapper _editorBuilderWrapper;
2022

2123
public CustomFormPartDriver(
2224
IContentDefinitionManager contentDefinitionManager,
2325
IOrchardServices orchardServices,
24-
IAuthorizationService authService) {
26+
IAuthorizationService authService,
27+
IEditorBuilderWrapper editorBuilderWrapper) {
28+
2529
_contentDefinitionManager = contentDefinitionManager;
2630
_orchardServices = orchardServices;
2731
_authService = authService;
32+
_editorBuilderWrapper = editorBuilderWrapper;
33+
2834
T = NullLocalizer.Instance;
2935
}
3036

3137
public Localizer T { get; set; }
3238

3339
protected override DriverResult Display(CustomFormPart part, string displayType, dynamic shapeHelper) {
3440
// this method is used by the widget to render the form when it is displayed
41+
// Display CustomForm_Wrapper shape only if shape type is Detail.
42+
if (displayType.Equals("Detail")) {
43+
int contentId = 0;
44+
var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
3545

36-
int contentId = 0;
37-
var queryString = _orchardServices.WorkContext.HttpContext.Request.QueryString;
46+
if (queryString.AllKeys.Contains("contentId")) {
47+
int.TryParse(queryString["contentId"], out contentId);
48+
}
3849

39-
if (queryString.AllKeys.Contains("contentId")) {
40-
int.TryParse(queryString["contentId"], out contentId);
41-
}
50+
ContentItem contentItem;
51+
if (contentId > 0) {
52+
contentItem = _orchardServices.ContentManager.Get(contentId);
4253

43-
ContentItem contentItem;
44-
if (contentId > 0) {
45-
contentItem = _orchardServices.ContentManager.Get(contentId);
54+
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.EditContent, contentItem))
55+
return null;
56+
} else {
57+
contentItem = _orchardServices.ContentManager.New(part.ContentType);
4658

47-
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.EditContent, contentItem))
48-
return null;
49-
} else {
50-
contentItem = _orchardServices.ContentManager.New(part.ContentType);
59+
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
60+
return null;
61+
}
5162

52-
if (part.UseContentTypePermissions && !_orchardServices.Authorizer.Authorize(Core.Contents.Permissions.CreateContent, contentItem))
63+
if (contentItem == null || contentItem.ContentType != part.ContentType)
5364
return null;
54-
}
5565

56-
if (contentItem == null || contentItem.ContentType != part.ContentType)
57-
return null;
66+
if (!contentItem.Has<ICommonPart>()) {
67+
return null;
68+
}
5869

59-
if (!contentItem.Has<ICommonPart>()) {
60-
return null;
70+
return ContentShape("Parts_CustomForm_Wrapper", () => {
71+
return shapeHelper.Parts_CustomForm_Wrapper()
72+
.Editor(_editorBuilderWrapper.BuildEditor(contentItem))
73+
.ContentPart(part);
74+
});
6175
}
62-
63-
return ContentShape("Parts_CustomForm_Wrapper", () => {
64-
return shapeHelper.Parts_CustomForm_Wrapper()
65-
.Editor(_orchardServices.ContentManager.BuildEditor(contentItem))
66-
.ContentPart(part);
67-
});
76+
// Returning null avoids rendering the edit shape for current custom form.
77+
return null;
6878
}
6979

7080
protected override DriverResult Editor(CustomFormPart part, dynamic shapeHelper) {

src/Orchard.Web/Modules/Orchard.CustomForms/Orchard.CustomForms.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
<Compile Include="Rules\CustomFormEvents.cs" />
147147
<Compile Include="Rules\IRulesManager.cs" />
148148
<Compile Include="Security\AuthorizationEventHandler.cs" />
149+
<Compile Include="Services\EditorBuilderWrapper.cs" />
150+
<Compile Include="Services\IEditorBuilderWrapper.cs" />
149151
<Compile Include="ViewModels\CustomFormIndexViewModel.cs" />
150152
<Compile Include="ViewModels\CustomFormPartEditViewModel.cs" />
151153
</ItemGroup>
@@ -231,4 +233,4 @@
231233
</PropertyGroup>
232234
<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'))" />
233235
</Target>
234-
</Project>
236+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Orchard.ContentManagement;
2+
3+
namespace Orchard.CustomForms.Services {
4+
public class EditorBuilderWrapper : IEditorBuilderWrapper {
5+
6+
private readonly IContentManager _contentManager;
7+
8+
public EditorBuilderWrapper(
9+
IContentManager contentManager) {
10+
11+
_contentManager = contentManager;
12+
}
13+
14+
public dynamic BuildEditor(IContent content) {
15+
return _contentManager.BuildEditor(content);
16+
}
17+
18+
public dynamic UpdateEditor(IContent content, IUpdateModel updateModel) {
19+
return _contentManager.UpdateEditor(content, updateModel);
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Orchard.ContentManagement;
2+
3+
namespace Orchard.CustomForms.Services {
4+
public interface IEditorBuilderWrapper : IDependency {
5+
dynamic BuildEditor(IContent content);
6+
dynamic UpdateEditor(IContent content, IUpdateModel updateModel);
7+
}
8+
}

src/Orchard.Web/Modules/Orchard.CustomForms/Views/Item/Create.cshtml

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
@using Orchard.ContentManagement
22
@using Orchard.Utility.Extensions
33
@using Orchard.ContentManagement.Aspects;
4+
@using Orchard.ContentManagement.MetaData;
5+
@using Orchard.ContentManagement.MetaData.Models;
6+
@using Orchard.Core.Contents.Settings;
7+
48
@{
9+
IContentDefinitionManager _contentDefinitionManager = WorkContext.Resolve<IContentDefinitionManager>();
10+
511
ContentItem customForm = Model.ContentItem;
612
string returnUrl = Model.ReturnUrl;
713
var metadata = customForm.ContentManager.GetItemMetadata(customForm);
@@ -14,6 +20,18 @@
1420

1521
var submitButtonText = String.IsNullOrEmpty(Model.ContentItem.CustomFormPart.SubmitButtonText) ? T("Submit").Text : Model.ContentItem.CustomFormPart.SubmitButtonText;
1622
var publishButtonText = String.IsNullOrEmpty(Model.ContentItem.CustomFormPart.PublishButtonText) ? T("Publish").Text : Model.ContentItem.CustomFormPart.PublishButtonText;
23+
24+
var showPublishButton = Model.ContentItem.CustomFormPart.SavePublishContentItem;
25+
// Read type definition to check if content is draftable
26+
var typeDefinition = _contentDefinitionManager
27+
.ListTypeDefinitions()
28+
.Where(x => String.Equals(x.Name, Model.ContentItem.CustomFormPart.ContentType, StringComparison.OrdinalIgnoreCase))
29+
.FirstOrDefault();
30+
if (typeDefinition != null) {
31+
// Publish button has to be visible only if content is draftable AND is set to show the publish button
32+
showPublishButton = showPublishButton &&
33+
typeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable;
34+
}
1735
}
1836

1937
@Display(New.Parts_Title().Title(displayText))
@@ -29,8 +47,22 @@
2947
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == false || Model.ContentItem.CustomFormPart.SaveContentItem == true) {
3048
<button type="submit" name="submit.Save" value="submit.Save">@submitButtonText</button>
3149
}
32-
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == true) {
50+
@if (showPublishButton) {
3351
<button type="submit" name="submit.Publish" value="submit.Publish">@publishButtonText</button>
3452
}
3553
</fieldset>
54+
55+
56+
<div class="edit-item-secondary group">
57+
@if (Model.Actions != null) {
58+
<div class="edit-item-actions">
59+
@Display(Model.Actions)
60+
</div>
61+
}
62+
@if (Model.Sidebar != null) {
63+
<div class="edit-item-sidebar group">
64+
@Display(Model.Sidebar)
65+
</div>
66+
}
67+
</div>
3668
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
@{
1+
@using Orchard.ContentManagement.MetaData;
2+
@using Orchard.ContentManagement.MetaData.Models;
3+
@using Orchard.Core.Contents.Settings;
4+
5+
@{
6+
IContentDefinitionManager _contentDefinitionManager = WorkContext.Resolve<IContentDefinitionManager>();
7+
28
dynamic editor = Model.Editor;
39

410
if (TempData.ContainsKey("CustomFormWidget.InvalidCustomFormState")) {
@@ -7,18 +13,34 @@
713

814
// remove default Save/Publish buttons
915
editor.Zones["Sidebar"].Items.Clear();
16+
17+
var showPublishButton = Model.ContentItem.CustomFormPart.SavePublishContentItem;
18+
19+
// Read type definition to check if content is draftable
20+
var typeDefinition = _contentDefinitionManager
21+
.ListTypeDefinitions()
22+
.Where(x => String.Equals(x.Name, Model.ContentItem.CustomFormPart.ContentType, StringComparison.OrdinalIgnoreCase))
23+
.FirstOrDefault();
24+
if (typeDefinition != null) {
25+
// Publish button has to be visible only if content is draftable AND is set to show the publish button
26+
showPublishButton = showPublishButton &&
27+
typeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable;
28+
}
1029
}
1130

1231
@using (Html.BeginFormAntiForgeryPost(Url.Action("Create", "Item", new { area = "Orchard.CustomForms", id = Model.ContentItem.Id }))) {
13-
@Html.ValidationSummary()
32+
@Html.ValidationSummary()
1433
// Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
15-
@Display(editor)
16-
@Html.Hidden("returnUrl", Request.RawUrl, new { id = string.Empty });
17-
@Html.Hidden("contentId", !string.IsNullOrWhiteSpace(Request.QueryString["contentId"]) ? Request.QueryString["contentId"] : "0", new { id = string.Empty });
18-
<fieldset class="submit-button">
19-
<button type="submit" name="submit.Save" value="submit.Save">@Model.ContentPart.SubmitButtonText</button>
20-
@if (Model.ContentItem.CustomFormPart.SavePublishContentItem == true) {
21-
<button type="submit" name="submit.Publish" value="submit.Publish">@Model.ContentPart.PublishButtonText</button>
22-
}
23-
</fieldset>
24-
}
34+
@Display(editor)
35+
36+
@Html.Hidden("returnUrl", Request.RawUrl, new { id = string.Empty });
37+
@Html.Hidden("contentId", !string.IsNullOrWhiteSpace(Request.QueryString["contentId"]) ? Request.QueryString["contentId"] : "0", new { id = string.Empty });
38+
39+
<fieldset class="submit-button">
40+
41+
<button type="submit" name="submit.Save" value="submit.Save">@Model.ContentPart.SubmitButtonText</button>
42+
@if (showPublishButton) {
43+
<button type="submit" name="submit.Publish" value="submit.Publish">@Model.ContentPart.PublishButtonText</button>
44+
}
45+
</fieldset>
46+
}

0 commit comments

Comments
 (0)