Orchard CMS - Extending Users with Fields - exposing values in Blog Post

只谈情不闲聊 提交于 2019-12-11 00:28:32

问题


I'd like to extend the users content definition to include a short bio and picture that can be viewed on every blog post of an existing blog. I'm unsure of what the best method to do this is.

I have tried extending the User content type with those fields, but I can't seem to see them in the Model using the shape tracing tool on the front end.

Is there a way to pass through fields on the User shape in a blog post? If so, what is the best way to do it?


回答1:


I also have done this a lot, and always include some custom functionality to achieve this.

There is a way to do this OOTB, but it's not the best IMO. You always have the 'Owner' property on the CommonPart of any content item, so in your blogpost view you can do this:

@{
    var owner = Model.ContentItem.CommonPart.Owner;
}

<!-- This automatically builds anything that is attached to the user, except for what's in the UserPart (email, username, ..) -->
<h4>@owner.UserName</h4>
@Display(BuildDisplay((IUser) owner))

<!-- Or, with specific properties: -->
<h1>@T("Author:")</h1>
<h4>@owner.UserName</h4>
<label>@T("Biography")</label>
<p>
    @Html.Raw(owner.BodyPart.Text)
</p>
<!-- <owner content item>.<Part with the image field>.<Name of the image field>.FirstMediaUrl (assuming you use MediaLibraryPickerField) -->
<img src="@owner.User.Image.FirstMediaUrl" />

What I often do though is creating a custom driver for this, so you can make use of placement.info and follow the orchard's best practices:

CommonPartDriver:

public class CommonPartDriver : ContentPartDriver<CommonPart> {

    protected override DriverResult Display(CommonPart part, string displayType, dynamic shapeHelper) {
        return ContentShape("Parts_Common_Owner", () => {
            if (part.Owner == null)
                return null;

            var ownerShape = _contentManager.BuildDisplay(part.Owner);
            return shapeHelper.Parts_Common_Owner(Owner: part.Owner, OwnerShape: ownerShape);
        });
    }
}

Views/Parts.Common.Owner.cshtml:

<h1>@T("Author")</h1>
<h3>@Model.Owner.UserName</h3>

@Display(Model.OwnerShape)

Placement.info:

<Placement>
  <!-- Place in aside second zone -->
  <Place Parts_Common_Owner="/AsideSecond:before" />
</Placement>



回答2:


IMHO the best way to have a simple extension on an Orchard user, is to create a ContentPart, e.g. "UserExtensions", and attach it to the Orchard user. This UserExtensions part can then hold your fields, etc. This way, your extensions are clearly separated from the core user.

To access this part and its fields in the front-end, just add an alternate for the particular view you want to override.

Is there a way to pass through fields on the User shape in a blog post?

Do you want to display a nice picture / vita / whatever of the blog posts author? If so: This could be your Content-BlogPost.Detail.cshtml - Alternate

@using Orchard.Blogs.Models
@using Orchard.MediaLibrary.Fields
@using Orchard.Users.Models
@using Orchard.Utility.Extensions
@{
    // Standard Orchard stuff here...
    if ( Model.Title != null )
    {
        Layout.Title = Model.Title;
    }

    Model.Classes.Add("content-item");

    var contentTypeClassName = ( (string)Model.ContentItem.ContentType ).HtmlClassify();
    Model.Classes.Add(contentTypeClassName);

    var tag = Tag(Model, "article");


    // And here we go:
    // Get the blogPost
    var blogPostPart = (BlogPostPart)Model.ContentItem.BlogPostPart;

    // Either access the creator directly
    var blogPostAuthor = blogPostPart.Creator;

    // Or go this way
    var blogPostAuthorAsUserPart = ( (dynamic)blogPostPart.ContentItem ).UserPart as UserPart;

    // Access your UserExtensions part
    var userExtensions = ( (dynamic)blogPostAuthor.ContentItem ).UserExtensions;

    // profit
    var profilePicture = (MediaLibraryPickerField)userExtensions.ProfilePicture;
}
@tag.StartElement
<header>
    @Display(Model.Header)
    @if ( Model.Meta != null )
    {
        <div class="metadata">
            @Display(Model.Meta)
        </div>
    }
    <div class="author">
        <img src="@profilePicture.FirstMediaUrl"/>
    </div>
</header>
@Display(Model.Content)
@if ( Model.Footer != null )
{
    <footer>
        @Display(Model.Footer)
    </footer>
}
@tag.EndElement

Hope this helps, here's the proof:



来源:https://stackoverflow.com/questions/32747276/orchard-cms-extending-users-with-fields-exposing-values-in-blog-post

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