ContentPart with title but no route

Topics: Customizing Orchard, Writing modules
Jul 11, 2011 at 4:03 PM

Hello all,

I've implemented a contentpart e.g MyContentThing but in the admin system when I'm listing items of this type they are all titled MyContentThing. I need to be able to distinguish between them in the admin site. If I add RoutePart that gives me a title and hey presto it looks fine but that also exposes my content as routable and it shouldnt be. 

How can I get them listed with a title in the admin site correctly without using RoutePart?




Jul 11, 2011 at 5:39 PM

I'm not 100% sure, but I think if you create a "SummaryAdmin" display for your content type which pulls the title/displays it, that would be a way to get around it.

Jul 11, 2011 at 9:33 PM

thanks could you elaborate on that? i'm not sure what you mean by summaryadmin display? thanks

Jul 11, 2011 at 9:56 PM

You would want to create a view for your part that basically describes how it should be displayed in summary modes in the admin.  Create a file like /MyModule/Views/Parts.MyContentThing_SummaryAdmin.cshtml.  You can see the implementation for the routable title here

Then you would edit the file for your module to say something like this:

    <Match DisplayType="SummaryAdmin">
        <Place Parts_MyContentThing_SummaryAdmin="Header:5"/>

Jul 12, 2011 at 9:21 AM

thanks nice idea but it doesn't seem to work :(

Jul 12, 2011 at 10:15 AM

In your part's driver, override the "GetContentItemMetadata" method and set the property "metadata.DisplayText". Here is an example:

protected override void GetContentItemMetadata(YourPart part, ContentItemMetadata metadata) {
    metadata.DisplayText = part.Title;

Jul 12, 2011 at 10:33 AM

Brilliant, that works a treat. thanks.

Jul 12, 2011 at 12:52 PM

Sorry for the misinformation, I was just guessing.

Jul 14, 2011 at 5:11 PM

OK so I have a similar issue - I have another ContentPart which is also a widget - this gets rendered really badly in the admin content list. Any ideas why? 

Jul 14, 2011 at 6:14 PM

Did you try what was suggested in previous comments?

Jul 14, 2011 at 6:24 PM

Both methods, neither work in this case. Its something to do with the contentitem being a widget. I just get an Edit link and the title of the item as an H1 which looks minging to say the least.

Jul 14, 2011 at 6:26 PM

This is the list item which is rendered -how do I override the template?

<li>    <div class="widget-control">
        <div class="manage-actions"><a href="/Admin/Widgets/EditWidget/70?ReturnUrl=%2FAdmin%2FContents%2FList">Edit</a></div>
    <article class="widget-user-help widget">

        <h1>Help with club screenings</h1>
    <div class="content-localization">
    <div class="add-localization"><a href="/Admin/Localization/Translate/70">+ New translation</a></div>

Jul 14, 2011 at 6:30 PM

What does your summary-admin template for your part look like? Can you give more information about your widget, placement and template (like, any ;)?

Jul 14, 2011 at 6:38 PM

ok I'm having a rethink, I'm not even sure I need a custom content type to do what I want. I just need an arbitrary piece of HTML content, localizable and managed from the CMS which is formatted to a certain template (it needs a jQuery accordian on it). This appears on a lot of pages in the site but the content is different on most pages. Please advise - was I going down the right route? 

I copied the summary admin template and the placement file verbatim from above but inserted some extra markup into the summary template so I could see if it rendered it - which it didn't.

Jul 15, 2011 at 2:15 AM

We've had to come up with some interesting work arounds for this "in order for an item to have a title it must have a routablepart" scenario.

I hope the title can get separated out in a future version of Orchard.

For instance we created a list of html slides, which were only added to a single page, but in order for us to manage them in Orchard so they weren't all named "Slide", we had to add a route. A route gave us a title to select in Orchard, but it also added <h1> tags on everything, made the single slide objects accessible via a separate url, and added it to our sitemap, which we really didn't want. 

We ended up removing titles from placement in our theme, and also creating our own sitemap provider which we can select explicit content types, instead of any that have a RoutablePart. 

Jul 15, 2011 at 11:43 AM

bertrand - still having a nightmare with this is terms of summaryadmin. by using placement I can tell it to render a particular shape so if I have 

<Match DisplayType="SummaryAdmin">
   <Place Parts_UserHelp="Content:1" />

but there is another template getting rendered as well above it. without shape tracing in the admin its impossible to tell what is happening. To be clear - I have a CustomType (called UserHelp) which has a UserHelpPart and a WidgetPart. My template for that example there is in Parts/UserHelp.cshtml but I've tried various combinations of template names for a SummaryAdmin override to no effect. Should it be Parts.UserHelp_SummaryAdmin.cshtml as mentioned above? If so what does that translate into in the placement info - Parts_UserHelp_SummaryAdmin I would guess? If I successfully override that template it should just render whats in there right so if literally had <p>Blah</p> it would render that for each item in the list?

Jul 15, 2011 at 8:51 PM

The scenario you described two posts above seems to fit well with creating a widget.

Well, yes, it should be the full name of the shape that your driver built.

Jul 15, 2011 at 11:24 PM

OK so as I suggested above I've built a custom ContentType for the localizable content and a separate Widget which deals with the rendering of that ContentType on the site. Basically I have a UserHelp ContentType which has a BodyPart, LocalizationPart and the UserHelpPart (this contains a url pattern or page name where this help should appear). This part is creatable and editable from Orchard admin as usual. For the widget I created another ContentType of UserHelpWidget. The driver for this interrogates WorkContextAccessor to determine the current URL. From that I can use the ContentManager to query and pull out the UserHelp which matches the URL we're on. I can then also interrogate the LocalizationService to see if we need a translated version instead depending on the current culture. Finally its a case of pushing the UserHelp ContentItem into UserHelpWidget which is being rendered.

So two questions:

1. How do I get the site's default culture? I don't want to be checking if there's a localized version of every time unless the culture is not the default.

2. How do I render the BodyPart from my UserHelp type in the UserHelpWidget? At the moment I do this in the driver

var contentItem =
                        uhp => uhp.UrlPattern.Contains(currentrequest)).
var bodytext = new HtmlString(contentItem.As<BodyPart>().Text);
                var title = contentItem.As<UserHelpPart>().Title;
                return ContentShape("Parts_UserHelpWidget",
                                    () => shapeHelper.Parts_UserHelpWidget(
                                        DisplayExpanded: part.DisplayExpanded,
                                        Title: title,
                                        BodyText: bodytext));
which means I get the text in my view but it seems like I should be able to just pass the whole UserHelp contentItem shape and have the template for UserHelpWidget deal with it? 

Jul 26, 2011 at 1:01 AM

1. workContext.CurrentSite.SiteCulture.

2. that actually looks about right.