Created
January 31, 2018 20:36
-
-
Save skttl/3518e279057c80b9d96ae327ae8b4b48 to your computer and use it in GitHub Desktop.
Member access for grid content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inherits UmbracoViewPage<dynamic> | |
@using Umbraco.Web.Templates | |
@using Newtonsoft.Json.Linq | |
@* this is my custom grid rendering | |
Note the function IsAreaVisible at line 69, which takes the UmbracoHelper (for getting the current member) and the area (for reading the visibility-setting) | |
In the rendering, IsAreaVisible is used at line 50 | |
*@ | |
@* | |
Razor helpers located at the bottom of this file | |
*@ | |
@if (Model != null && Model.sections != null) | |
{ | |
var oneColumn = ((System.Collections.ICollection)Model.sections).Count == 1; | |
if (oneColumn) | |
{ | |
foreach (var section in Model.sections) | |
{ | |
<div id="content"> | |
@foreach (var row in section.rows) | |
{ | |
@renderRow(row, true); | |
} | |
</div> | |
} | |
} | |
else | |
{ | |
foreach (var s in Model.sections) | |
{ | |
foreach (var row in s.rows) | |
{ | |
@renderRow(row, false); | |
} | |
} | |
} | |
} | |
@helper renderRow(dynamic row, bool singleColumn) | |
{ | |
<div @RenderElementAttributes(row) class="row"> | |
@foreach (var area in row.areas) | |
{ | |
if (IsAreaVisible(Umbraco, area) || UmbracoContext.Current.InPreviewMode) | |
{ | |
<div @RenderElementAttributes(area) class="col [email protected]"> | |
@foreach (var control in area.controls) | |
{ | |
if (control != null && control.editor != null && control.editor.view != null) | |
{ | |
<text>@Html.Partial("grid/editors/base", (object)control)</text> | |
} | |
} | |
</div> | |
} | |
} | |
</div> | |
} | |
@functions { | |
public static bool IsAreaVisible(UmbracoHelper umbraco, dynamic area) | |
{ | |
var isVisibleToMembersValue = ""; | |
JObject cfg = area.config; | |
if (cfg != null) | |
{ | |
foreach (JProperty property in cfg.Properties()) | |
{ | |
if (property.Name == "isVisibleToMembers") | |
{ | |
isVisibleToMembersValue = property.Value.ToString(); | |
} | |
} | |
} | |
var isLoggedIn = umbraco.MemberIsLoggedOn(); | |
var isVisibleToMembers = (isVisibleToMembersValue == "") || (isVisibleToMembersValue == "Show for all users") || (isLoggedIn && isVisibleToMembersValue == "Show only for logged in members") || (!isLoggedIn && isVisibleToMembersValue == "Show only for not logged in members"); | |
return isVisibleToUsers; | |
} | |
public static MvcHtmlString RenderElementAttributes(dynamic contentItem) | |
{ | |
var attrs = new List<string>(); | |
JObject cfg = contentItem.config; | |
if (cfg != null) | |
{ | |
foreach (JProperty property in cfg.Properties()) | |
{ | |
attrs.Add(property.Name + "=\"" + property.Value.ToString() + "\""); | |
} | |
} | |
JObject style = contentItem.styles; | |
if (style != null) | |
{ | |
var cssVals = new List<string>(); | |
foreach (JProperty property in style.Properties()) | |
cssVals.Add(property.Name + ":" + property.Value.ToString() + ";"); | |
if (cssVals.Any()) | |
attrs.Add("style=\"" + string.Join(" ", cssVals) + "\""); | |
} | |
return new MvcHtmlString(string.Join(" ", attrs)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add this setting to your grid datatype | |
{ | |
"label": "Visibility for members", | |
"key": "isVisibleToMembers", | |
"view": "radiobuttonlist", | |
"applyTo": "cell", // could also be "row" | |
"prevalues": [ | |
"Show for all users", | |
"Show only for logged in members", | |
"Show only for not logged in members" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment