Best way to extend Admin/Users view with extra column from other Part

Topics: Customizing Orchard
Dec 9, 2012 at 8:17 PM

Hi,

I'm working on a project that adds a custom part (LearnerPart) to a UserPart with an ActivatingFilter in the handler. 

Now I would like to add a Credits column to the Admin/Users view with information from my custom LearnerPart.

I succeeded by overwriting the Orchard .Users/Admin/Index.cshtml view in my theme and adding this: 

...

var service = WorkContext.Resolve<IOrchardServices>();
var cmgr = service.ContentManager;

...

@foreach (var entry in Model.Users)
            {
                var userPart = cmgr.Get<UserPart>(entry.User.Id); // is this the best way to get to the UserPart from the UserPartRecord?
                var learnerPart = userPart.ContentItem.As<LearnerPart>(); // and to get the LearnerPart?
                int credits = 0;
                if (learnerPart != null)
                {
                    credits = learnerPart.Credits;
                }

 

It works but I feel like there should be a better way to do this.

I have two questions:

  1. Is this code I wrote the right way to get the information I need into this View? Or is there a better/preferred/faster/easier way?
  2. Is this the best way to do this? Or should I maybe not use the existing Users view but create my own Admin View from the Learner Module? Or do it in some other place?

 

Thank you for your time.

David

 

Coordinator
Dec 9, 2012 at 8:24 PM

That part of the code is old and not very extensible. Your best bet is probably to create your own admin screen.

Dec 9, 2012 at 9:32 PM

Thank you Bertrand, I can do that.  But then I will have the User information in 2 places: my own module and the existing Users screen. Is there a way my own module could 'add' information to the existing admin view? Or replace this view from the module itself instead of from the Theme?

And the other thing, this code being old, what would be the new way of doing the thing I did here? So getting access to the LearnerPart when I have obtained the UserPartRecord from the Model?

Coordinator
Dec 9, 2012 at 9:52 PM
Edited Dec 9, 2012 at 9:52 PM

You can replace it by taking over its route, with a higher priority.

I don't know how we'd redo it today. It would require some design.

Dec 9, 2012 at 10:24 PM

Ok, thanks, I will try to do that then. Do you happen to know of a module I could use as an example of how to take over a route?

And I don't understand what you mean by 'design' in your second answer, sorry for that. Maybe it's a language thing for I'm not speaking English every day.
I know a View isn't the right place to do this kind of query-things, but I didn't know how else to solve it.

Let me ask it in a different, more general way: Is there a direct method to get access to the ContentPart of a ContentPartRecord? So not having to go through the ContentManager, like I do here?

Coordinator
Dec 9, 2012 at 11:26 PM

No, I don't have an example of that specifically.

What I mean is that we would have to think about it.

No, there is no way to go from record to part. You should never need to do that.

Dec 10, 2012 at 7:16 AM

Ok great, thank you. I will do it like this then. Found some routing examples on StackOverflow.