Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit 00ed7f9

Browse files
Fixes issues #36 by exposing a DefaultDocTypeGridEditorSurfaceControllerResolver on which you can register a default DocTypeGridEditorSurfaceController to use for all requests. It will try looking for a more specific controller first, but if one isn't found, and a default is set, it will look for am appropreatly named action on the default controller, and if one can't be found, default to "Index". If you set a DefaultDocTypeGridEditorSurfaceController, then the Html helper will ALWAYS use that controller for every call to it.
1 parent cc52c0e commit 00ed7f9

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/Our.Umbraco.DocTypeGridEditor/Our.Umbraco.DocTypeGridEditor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<Compile Include="Web\Controllers\DocTypeGridEditorSurfaceController.cs" />
238238
<Compile Include="Web\Extensions\HtmlHelperExtensions.cs" />
239239
<Compile Include="Web\Extensions\UmbracoHelperExtensions.cs" />
240+
<Compile Include="Web\Mvc\DefaultDocTypeGridEditorSurfaceControllerResolver.cs" />
240241
</ItemGroup>
241242
<ItemGroup>
242243
<None Include="app.config" />

src/Our.Umbraco.DocTypeGridEditor/Web/Extensions/HtmlHelperExtensions.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Web.Mvc;
33
using System.Web.Mvc.Html;
44
using Our.Umbraco.DocTypeGridEditor.Extensions;
5+
using Our.Umbraco.DocTypeGridEditor.Web.Mvc;
56
using Umbraco.Core;
67
using Umbraco.Core.Models;
78
using Umbraco.Web;
8-
using Content = System.Web.UI.WebControls.Content;
99

1010
namespace Our.Umbraco.DocTypeGridEditor.Web.Extensions
1111
{
@@ -29,6 +29,8 @@ public static HtmlString RenderDocTypeGridEditorItem(this HtmlHelper helper,
2929
previewViewPath = previewViewPath.TrimEnd('/') + "/";
3030

3131
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
32+
33+
// Try looking for surface controller with action named after the editor alias
3234
if (!editorAlias.IsNullOrWhiteSpace() && umbracoHelper.SurfaceControllerExists(controllerName, editorAlias, true))
3335
{
3436
return helper.Action(editorAlias, controllerName, new
@@ -39,6 +41,7 @@ public static HtmlString RenderDocTypeGridEditorItem(this HtmlHelper helper,
3941
});
4042
}
4143

44+
// Try looking for surface controller with action named after the doc type alias alias
4245
if (umbracoHelper.SurfaceControllerExists(controllerName, content.DocumentTypeAlias, true))
4346
{
4447
return helper.Action(content.DocumentTypeAlias, controllerName, new
@@ -49,6 +52,43 @@ public static HtmlString RenderDocTypeGridEditorItem(this HtmlHelper helper,
4952
});
5053
}
5154

55+
// See if a default surface controller has been registered
56+
var defaultController = DefaultDocTypeGridEditorSurfaceControllerResolver.Current.GetDefaultControllerType();
57+
if (defaultController != null)
58+
{
59+
var defaultControllerName = defaultController.Name.Substring(0, defaultController.Name.LastIndexOf("Controller"));
60+
61+
// Try looking for an action named after the editor alias
62+
if (!editorAlias.IsNullOrWhiteSpace() && umbracoHelper.SurfaceControllerExists(defaultControllerName, editorAlias, true))
63+
{
64+
return helper.Action(editorAlias, defaultControllerName, new
65+
{
66+
dtgeModel = content,
67+
dtgeViewPath = viewPath,
68+
dtgePreviewViewPath = previewViewPath
69+
});
70+
}
71+
72+
// Try looking for a doc type alias action
73+
if (umbracoHelper.SurfaceControllerExists(defaultControllerName, content.DocumentTypeAlias, true))
74+
{
75+
return helper.Action(content.DocumentTypeAlias, defaultControllerName, new
76+
{
77+
dtgeModel = content,
78+
dtgeViewPath = viewPath,
79+
dtgePreviewViewPath = previewViewPath
80+
});
81+
}
82+
83+
// Just go with a default action name
84+
return helper.Action("Index", defaultControllerName, new
85+
{
86+
dtgeModel = content,
87+
dtgeViewPath = viewPath,
88+
dtgePreviewViewPath = previewViewPath
89+
});
90+
}
91+
5292
// Check for preview view
5393
if (!string.IsNullOrWhiteSpace(previewViewPath)
5494
&& helper.ViewContext.RequestContext.HttpContext.Request.QueryString["dtgePreview"] == "1")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using Our.Umbraco.DocTypeGridEditor.Web.Controllers;
3+
using Umbraco.Core;
4+
using Umbraco.Core.ObjectResolution;
5+
6+
namespace Our.Umbraco.DocTypeGridEditor.Web.Mvc
7+
{
8+
/// <summary>
9+
/// A resolver used to resolve the default SurfaceController that is used to render any front-end
10+
/// Umbraco partial when using Nested Content.
11+
/// </summary>
12+
public class DefaultDocTypeGridEditorSurfaceControllerResolver : SingleObjectResolverBase<DefaultDocTypeGridEditorSurfaceControllerResolver, Type>
13+
{
14+
/// <summary>
15+
/// Constructor accepting the default SurfaceController
16+
/// </summary>
17+
/// <param name="value"></param>
18+
public DefaultDocTypeGridEditorSurfaceControllerResolver(Type value)
19+
: base(value)
20+
{
21+
ValidateType(value);
22+
}
23+
24+
/// <summary>
25+
/// Sets the default SurfaceController type
26+
/// </summary>
27+
/// <param name="controllerType"></param>
28+
public void SetDefaultControllerType(Type controllerType)
29+
{
30+
ValidateType(controllerType);
31+
Value = controllerType;
32+
}
33+
34+
/// <summary>
35+
/// Returns the Default SurfaceController type
36+
/// </summary>
37+
/// <returns></returns>
38+
public Type GetDefaultControllerType()
39+
{
40+
return Value;
41+
}
42+
43+
/// <summary>
44+
/// Ensures that the type passed in is of type SurfaceController
45+
/// </summary>
46+
/// <param name="type"></param>
47+
private void ValidateType(Type type)
48+
{
49+
if (!type.IsOfGenericType(typeof(DocTypeGridEditorSurfaceController<>)))
50+
{
51+
throw new InvalidOperationException("The Type specified (" + type + ") is not of type " + typeof(DocTypeGridEditorSurfaceController<>));
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)