Help – with Layout Part?

Topics: General, Troubleshooting, Writing modules, Writing themes
Jun 6, 2015 at 7:22 AM
Hi all, I wish to add a Layout Part to a custom content type to create a slide of a carousel, first and foremost, how does one render/display this Layout content once it is created in my type? Do I have to create a driver and access it somehow through there? Essentially, how does one display the content found in the layout canvas?
Also, does anyone know of a guide / how to / tutorial – demo in existence other than this brief write-up: http://www.ideliverable.com/blog/orchard-layouts?
Thanks for any support / guidance in advance, Liv :)
Developer
Jun 6, 2015 at 9:19 AM
You don't have to do anything extra once you have attached the LayoutPart to your custom content type.
Is there anything specific you'd like to know more about?
Jun 7, 2015 at 1:18 AM
Edited Jun 7, 2015 at 2:02 AM
Thanks for the reply, Sipke -
I'm trying to re-extend David Haydens "FeaturedItemSlider". I did so in the past by adding a whole host of fields to the FeaturedItemRecord relating to css etc., to have some control over layout - this worked, but was not user friendly if you didn't know the layout design.

So, I thought I would abolish most of that, and use Layouts instead - to get a WYSIWYG approach for those creating a slide in the admin.

David's code uses bootstrap carousel, and basically loops through all FeaturedItems via:
https://github.com/sunkist/FeaturedItemSlider/blob/master/Sunkist.FeaturedItemSlider/Views/Parts/FeaturedItems.cshtml
to create the carousel.

I have added a LayoutPart to the FeaturedItemPart, but I'm struggling to display the layout in the above alternate view?

His original Driver:
https://github.com/sunkist/FeaturedItemSlider/blob/master/Sunkist.FeaturedItemSlider/Drivers/FeaturedItemSliderWidgetPartDriver.cs

Do I have to populate the viewmodel [passed as FeaturedItems to the contentShape] with the layoutPart, and if so how? I wont go on with any of my other attempts [as I will most likely confuse you and myself ;) ]
I really wish to extend this module, instead of going down a taxonomy/projections route - to purposely get my hands dirty, so any advice/help suggestions you could offer would be greatly appreciated.

Sorry for the long post embarrassed but supremely appreciative of your support, big thanks Liv
Jun 10, 2015 at 12:30 PM
...anyone want to have a shot at pointing me in the right direction :b Big Thanks, Liv
Jun 11, 2015 at 8:22 AM
Hi, please note, this was solved at http://orchardpros.net/tickets/8047 - "yay!"
@sfmskywalker: how elaborate can layouts be? Any restrictions...thanks Liv
Developer
Jun 11, 2015 at 10:31 AM
Glad to hear you got it sorted!
Not sure I understand your question regarding layouts. Can you elaborate please?
Jun 11, 2015 at 12:13 PM
Sorry Sipke, what I was trying to ascertain, I've subsequently answered myself with a lot of experimentation (got to say, I'm pretty excited about using Layouts 'properly' in my module). Although, I am struggling to do just that. Currently whilst testing, I've been using a static id to access/return the layout in my driver:
protected override DriverResult Display(FeaturedItemSliderWidgetPart part, string displayType, dynamic shapeHelper) {

        int slideNumber = 0;

        var featuredItems = _contentManager.Query<FeaturedItemPart, FeaturedItemPartRecord>("FeaturedItem")
            .Where(fip => fip.GroupName == part.GroupName)
            .OrderBy(fi => fi.SlideOrder)
            .List()
            .Select(fi => new FeaturedItemViewModel {
                ColorPicked = fi.ColorPicked,
                LinkUrl = fi.LinkUrl,
                SeparateLink = fi.SeparateLink,
                LinkText = fi.LinkText,
                ImagePath = ((MediaLibraryPickerField)fi.Fields.Single(f => f.Name == "Picture")).MediaParts.FirstOrDefault() == null ? "" : ((MediaLibraryPickerField)fi.Fields.Single(f => f.Name == "Picture")).MediaParts.First().MediaUrl,
                SlideNumber = ++slideNumber
            }).ToList();

        var fiLayout = _contentManager.Query<LayoutPart, LayoutPartRecord>()
            .Where(x => x.ContentItemRecord.Id == """""STATIC ID""""")
            .List()
            .SingleOrDefault();

        var group = _contentManager.Query<FeaturedItemGroupPart, FeaturedItemGroupPartRecord>("FeaturedItemGroup")
            .Where(fig => fig.Name == part.GroupName)
            .List()
            .SingleOrDefault();

        var elements = _layoutManager.LoadElements(fiLayout);
        var layout = _elementDisplay.DisplayElements(elements, fiLayout, displayType: displayType);

        return ContentShape("Parts_FeaturedItems",
            () => shapeHelper.Parts_FeaturedItems(
                FeaturedItems: featuredItems, 
                ContentPart: part, 
                Group: group,
                Layout: layout));
    }
...but now I wish to implement it properly, I'm confused about how I'm going to reference the layout associated with the FeaturedItem (LayoutPart is attached to FeaturedItemPart) as I can't seem to find an obvious (to me) relationship between the two in the db? Sorry to go slightly off topic, but I've hit a fairly decent brick wall. Any thoughts, or pointers you could offer? Thanks in advance, Liv
Jun 12, 2015 at 1:44 AM
Yep - back on topic, I still can't display Layouts properly, and it is looking unlikely: see http://orchardpros.net/tickets/8053
    protected override DriverResult Display(FeaturedItemSliderWidgetPart part, string displayType, dynamic shapeHelper) {

        //var fiLayout = _contentManager.Query<LayoutPart, LayoutPartRecord>()
        //    .Where(x => x.ContentItemRecord.Id == 23)
        //    .List()
        //    .SingleOrDefault();

        int slideNumber = 0;     

        var featuredItems = _contentManager.Query<FeaturedItemPart, FeaturedItemPartRecord>("FeaturedItem")
            .Where(fip => fip.GroupName == part.GroupName)
            .OrderBy(fi => fi.SlideOrder)
            .List()
            .Select(fi => new FeaturedItemViewModel {
                LinkUrl = fi.LinkUrl,
                SeparateLink = fi.SeparateLink,
                LinkText = fi.LinkText,
                //LayoutPart = _elementDisplay.DisplayElements(_layoutManager.LoadElements(fi.ContentItem.As<LayoutPart>()), fi.ContentItem.As<LayoutPart>(), displayType: displayType),
                LayoutPart = fi.ContentItem.As<LayoutPart>(),
                ImagePath = ((MediaLibraryPickerField)fi.Fields.Single(f => f.Name == "Picture")).MediaParts.FirstOrDefault() == null ? "" : ((MediaLibraryPickerField)fi.Fields.Single(f => f.Name == "Picture")).MediaParts.First().MediaUrl,
                SlideNumber = ++slideNumber
            }).ToList();

        var group = _contentManager.Query<FeaturedItemGroupPart, FeaturedItemGroupPartRecord>("FeaturedItemGroup")
            .Where(fig => fig.Name == part.GroupName)
            .List()
            .SingleOrDefault();

        //var elements = _layoutManager.LoadElements(fiLayout);
        //var layout = _elementDisplay.DisplayElements(elements, fiLayout, displayType: displayType);

        return ContentShape("Parts_FeaturedItems",
            () => shapeHelper.Parts_FeaturedItems(
                FeaturedItems: featuredItems, 
                ContentPart: part, 
                Group: group
                /*Layout: layout*/));
    }
...is it impossible? thanks, Liv
Jun 12, 2015 at 9:03 AM
Ha - got it sorted with http://orchardpros.net/tickets/8053, again -- LOVE THAT SITE and the two that helped me!
Developer
Jun 12, 2015 at 9:21 AM
:)