Loudenvier
Loudenvier

Reputation: 8794

How to extend Orchard navigation module to add images to menu items

UPDATE: I've changed the original question drastically based on Bertrand's suggestions and my own findings. Now it provides an incomplete solution in its text instead of my own blind meanderings and commentary on Orchard, which were completely WRONG!

I need to display a menu using images instead of text, one standard, and another for when hovered/selected. The requirements for the site states that the end-user should be able to manage the menu item images. The standard navigation module now provides an HTML menu item, which is not what the end user wants. The customer wants a very simple, intuitive interface for configuring the sites many menus, and all menus are image-based.

Based on Bertrand's advice, and after realizing that Content Menu Item IS A CONTENT TYPE, I've created a new Content Part in the Admin Interface (not by code, I only want to write code for parts and content types when ultimately needed... I really want to see how far I can go with Orchard just by using the admin interface and templating/CSSing).

So, I've created a Menu Image Part, with two Content Picker fields added to it: Image and Hover Image. Then I've added this part to the Content Menu Item in the Manage Content Items admin interface.

Since I didn't write a Driver for it, the Model passed to the menu item template does not have an easily accessible property like @Model.Href... I've overriden the MenuItemLink-ContentMenuItem.cshtml with the following code so far:

@using Orchard.Core.Common.Models
@using Orchard.ContentManagement
@{
    var contentManager = WorkContext.Resolve<IContentManager>();
    var itemId = Model.Content.ContentItem.ContentMenuItemPart.Id;
    ContentItem contentItem = contentManager.Get(itemId);
    ContentField temp = null;
    var menuImagePart = contentItem.Parts.FirstOrDefault(p => p.PartDefinition.Name == "MenuImagePart");
    if (menuImagePart != null)
    {
        temp = menuImagePart.Fields.First();
    }
}

<span>@temp</span>
<a href="@Model.Href">@Model.Text</a>

This yields the expected title for the Menu in a link, with a span before it with the following text:

Orchard.Fields.Fields.MediaPickerField 

So all the above code (get the current content manager and the id of the ContentItem representing the ContentMenuItemPart, then use the content manager to get ContentItem itself, then linqing over its Parts to find the MenuImagePart (I can't use Get to get it because it requires a type and the MenuImagePart is not a type, it was created in the admin interface), then finally getting the first field for debugging purposes (this should be the Image field of the MenuImagePart I've created...)... all the above code actually got me to the Media Picker Field on my Meny Image Part...

What I'm not being able to do, and what makes me certainly a lot obtuse and stupid, is to find a way to read the MediaPickerField URL property! I've tried casting it to MediaPickerField, but I can't access its namespace from inside my template code above. I don't even know which reference to add to my theme to be able to add the following directive to it:

@using Orchard.Fields.Fields

Upvotes: 0

Views: 3734

Answers (1)

Loudenvier
Loudenvier

Reputation: 8794

I've finally succeeded in this task (thanks to Bertrand's direction).

UPDATE: And thanks again to Bertrand I've polished the solution which was running in circles, querying content items from the content manager when they were already available on the Model... now I'm leveraging the dynamic nature of content item, etc. And I'm finally satisfied with this solution.

It was necessary to create a new Content Part called Menu Image, then add this to the Content Type named Content Item Menu, and finally overriding the Content Item Menu template. This last part was the really tricky one. If it was not for Bertrand's directions the code bellow would have been smelly and daunting. The template ended up as follow:

@using Orchard.Utility.Extensions;
@using System.Dynamic
@{

/* Getting the menu content item
***************************************************************/

var menu = Model.Content.ContentItem;

/* Creating a unique CSS class name based on the menu item
***************************************************************/

// !!! for some reason the following code throws: 'string' does not contain a definition for 'HtmlClassify'
//string test = menu.ContentType.HtmlClassify();
string cssPrefix = Orchard.Utility.Extensions.StringExtensions.HtmlClassify(menu.ContentType);
var uniqueCSSClassName = cssPrefix + '-' + Model.Menu.MenuName;

/* Adds the normal and hovered styles to the html if any
***************************************************************/

if (menu.MenuImagePart != null)
{

    if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.Image.Url))
    {
    using(Script.Head()){
    <style>
        .@uniqueCSSClassName {
            background-image: url('@Href(menu.MenuImagePart.Image.Url)');
            width: @{@menu.MenuImagePart.Image.Width}px;
            height: @{@menu.MenuImagePart.Image.Height}px;
            display: block;
        }
    </style>
    }
    }
    if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.HoverImage.Url))
    {
    using(Script.Head()){
    <style>
        .@uniqueCSSClassName:hover {
            background-image: url('@Href(menu.MenuImagePart.HoverImage.Url)');
            width: @{@menu.MenuImagePart.HoverImage.Width}px;
            height: @{@menu.MenuImagePart.HoverImage.Height}px;
        }
    </style>    
    }
    }
}
}    
<a class="@uniqueCSSClassName" href="@Model.Href">@Model.Text</a>

The only thing that I didn't understand is why I can't use HtmlClassify as an extension method with menu.ContentItem.HtmlClassify() and have to resort to calling the method as a standard static method (see the line with the comment `// !!! for some reason the following code throws´...)

Thanks again Bertrand!

Upvotes: 2

Related Questions