Showing posts with label Orchard. Show all posts
Showing posts with label Orchard. Show all posts

Thursday, January 3, 2013

Resolving a content item url in Orchard

Recently I needed to create a link to edit a content item in Orchard. Orchard uses a url like http://mysite/Home/Admin/Contents/Edit/123, where the id corresponds to the content item id.

My first thought was to hand-craft the url, replacing the content id as needed. But I found a better way: use the item's meta-data. Firstly inject the orchard IContentManager as usual, then call IContentManager.GetItemMetadata() The returned ContentItemMetadata contains MVC route's, for my particular case, EditorRouteValues had all the data I needed.

In my exploration of Orchard, I am continually finding myself having to dive into the code to work out how to do things. So here is 'how' I finally stumbled on the meta data solution.

Firstly I knew the Content Control Wrapper performed some 'magic' which added a link to a content item's edit.
Alas so-far I have no special trick for getting from a feature to the code that implements it. But eventually I found Orchard.Core/Contents/ControlWrapper.cs which implements the feature as a IShapeTableProvider. In here we see Content is wrapped with a Content_ControlWrapper shape.

Since nothing particularly enlightening is happening, I went to the view for the shape: Orchard.Core/Views/Content.ControlWrapper.cshtml. In the view the Html.ItemEditLinkWithReturnUrl() caught my eye.

This ItemEditLinkWithReturnUrl() is an extension, tracked down to Orchard.Framework/Mvc/Html/ContentItemExtensions.cs

Navigating to find the core ItemEditLink helper, bingo, I found how the content item meta-data is used for route values.

Wednesday, December 19, 2012

Using async/await (TPL) with Orchard

While working on a module for Orchard CMS I ran into some problems trying to glue async (Task) based code with (classic) synchronous code.

Here's the set-up:
  • Service TheMovieDb to call themovedb.org api.
  • Service MovieInfo service to integrate TheMovieDb with Orchard.
  • AdminController for front-end to call MovieInfo and coordinate views.

TheMoveDb uses Tasks, so I don't have to wait around for across-the-wire calls. MovieInfo uses Tasks, and integrates with orchard services. AdminController uses normal Action methods.

Being a TDD developer, all the pieces looked fine. But as soon as I put it together... splat.

It all starts with a deadlock (doesn't it always!). A quick google gets to Stephen Cleary (via SO) Don't Block on Async Code and Async and Await Armed with this I now at-least understand the problem.

Alas, at least in Orchard 1.6, async controllers are a no-go. It took a fair bit of searching, I finally found an SO article describing the problem. So (for now) I've settled on a sync / async solution.

Putting it all together:
  • TheMoveDb service stays as a normal async service.
  • MovieInfo service calls TheMoveDb with ConfigureAwait(false).
  • AdminController stay synchronous; blocks on calls to MovieInfo, waiting for Result.

There was one last gotcha; anything after the await in MovieInfo service executes on its own thread. So no access to orchard services (such as WorkContext etc). To get around this I just had to take care to no need the 'orchard friendly' thread context after the await.

Monday, November 12, 2012

Display summary for content item in Orchard.

Lately I've been building skills using Orchard CMS.
I ran into a problem trying to display a list of content items; in particular the in-built summary approach for Orchard didn't suite. The in-built approach takes the first 200 characters and displays them via the shape: Parts_Common_Body_Summary. This shape is created down inside the ~/Core/Common/Drivers/BodyPartDriver.cs, to be served up via the Parts.Common.Body.Summary.cshtml view template. Then; within the template; the body's html is used to create a excerpt of the first 200 chars, displayed as a p.

For me, I wanted to be able to specify the summary shape using html described in the content item itself. Initially I looked at just customizing the template view, I think this approach may have solved my immediate problem, but I would have had (yucky) logic inside my view template. Sounds like a great excuse for creating a Module :D

Since this was my first Orchard module, there was quite a learning curve to piece together how-to go about this problem. I can recommend the Pluralsight training video's to help you get started. Otherwise the Orchard Doco can help. Of course, nothing beats learning by tracing code and figuring it out yourself :D.

Normal content part driver's in Orchard create a shape, which is then served up via Orchard (don't forget placement) and displayed using a view template. Initially I was inclined to follow this pattern, and create my own "Parts_MySummary" shape for use. For better-or-worse, I opted to alter the existing Summary shape, I'm not 100% this is a good idea, but hey, if the fine folk over on the Orchard Project are going to make such a highly flexible and extensible CMS, then this is the kind of thing that'll happen :O.

To over-ride the in-built shape, I use my own IShapeTableProvider to hook into the display of the Parts_Common_Body_Summary, and with the magic that is IoC tada!

Since I was there, the module is built using 3 strategies for the summary:
  1. Default (fallback) to in-built
  2. First paragraph - extract the first p element from the body
  3. Explicit text - editor defines the summary text to use.
Ideally I would have made this more extensible; allowing for other strategies to also be used. But I couldn't work-out how to add shapes at run-time to represent different strategies. For now, if you want a different strategy: Add an ISummaryStrategy service and provide a better Part editor. I will have to re-visit when I learn more.

The final module is hosted on codeplex: http://kwdsummary.codeplex.com/

Monday, October 22, 2012

Accessing the Orchard current user content item

While working on a custom theme for my Orchard based site, I ran into a problem trying to leverage the current user content item. For my theme, I wanted to be able to present some additional information about the current user, from the header in my theme.

Orchard uses Theme modules to separate presentation from content, this separation is one of the (many) reasons I'm starting to really love Orchard. Using a combination of the Designer tools module, and VStudio, you can peruse the shapes displayed on your page. From this I easily replaced the User.cshtml view with my own, but I couldn't work out how to actually reach the current user content item; the default User.html (from ThemeMachine) uses WorkContent.CurrentUser, but this is the IUser not the content item itself!!??

Eventually I stumbled onto: http://orchard.codeplex.com/discussions/255594, and found the user content item is simply WorkerContent.CurrentUser.ContentItem. To keep my site clean, I opted to leverage the Profile module, and add my custom user data to the Profile content part. Then a little prep code in the view, and now I can leverage a custom DisplayName field in my view. I did have to rummage through the source code to find the best way to reach the data using the dynamic type, finally I settled on:

//http://orchard.codeplex.com/discussions/255594
//User controllable display name.
var displayName = String.Empty;
if(WorkContext.CurrentUser != null)
{
  dynamic user = WorkContext.CurrentUser.ContentItem;
  if(user.ProfilePart != null && 
    user.ProfilePart.Has(typeof(object), "DisplayName") &&
    user.ProfilePart.DisplayName.Value is string)
  {
    displayName = user.ProfilePart.DisplayName.Value.Trim();
  }
  
  if(String.IsNullOrWhiteSpace(displayName))
    { displayName = WorkContext.CurrentUser.UserName; }
}

A short list of C# coding issues.

Injecting services into entities It been bugging me for a while now that to use services in a entity can be a bit of pain. It usually starts...