Trying to hide Content Parts on Content Type

荒凉一梦 提交于 2019-12-04 18:16:05

When a content part is created through the admin dashboard, there isn't really a shape to render it, only individual shapes for inner content fields...

So, try this

public override void Displaying(ShapeDisplayingContext context) {
  context.ShapeMetadata.OnDisplaying(displayedContext => {
    var shape = displayedContext.Shape;

    if (shape.ContentPart != null
      && shape.ContentPart.PartDefinition.Name == "PartName") {
      var workContext = _workContextAccessor.GetContext();
      var user = workContext.CurrentUser;

      if (user == null || !user.Has<UserRolesPart>()
        || !user.As<UserRolesPart>().Roles.Contains("RoleName")) {
        displayedContext.ChildContent = new System.Web.HtmlString("");
      }
    }
  });
}

See my answer on OrchardPros

http://orchardpros.net/tickets/6914

Best

Nulling the shape variable will just clear the local reference. Setting the following however should hide the shape:

displayedContext.ShapeMetadata.Position = "-";

Also FYI it's better not to check on roles the user have but rather create a custom permission, add that to the user role and then check for the permission through

IAuthorizationService.TryCheckAccess()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!