Widget's container/page

Topics: Writing modules
Sep 22, 2011 at 4:26 PM

I need to find out the container/page in which my widget is currently in when it is instantiated. I can only get the LayerPart in which the widget is in.

Eg, in the Handler, I override the OnLoaded event :

 private void PartLoaded(MyWidgetPart part)
{
// here I need to find out on which page/container this widget is
// in, 'Home','Portfolio' and so on
// so that I can act appropriately on some information

// this only gets the LayerPart that holds the widget
// but it doesnt have information about the page/container
var container = part.As().Container;
}

 

So is it possible?

Thanks.

Coordinator
Sep 22, 2011 at 7:38 PM

It is possible but a little hacky (because in fact there is no guarantee that there is such a thing as "the container/page"). In order to do that, you need the content item to be stored in some conventional place where the widget can look. If the specific content item that you are interested in set a reference to itself on the shape it generated from its driver, you can retrieve that through digging such as workContext.Layout.Content.Content.ContentItem. Attaching a debugger may be a good way to find a path that works. If that is not the case and the shape does not have a reference to the content item, you'll need to write code that adds it. One way to do it is to write a handler that adds it on the fly, for example on layout.CurrentContentItem.

Developer
Feb 28, 2013 at 6:15 PM
Edited Feb 28, 2013 at 6:15 PM
I came upon this page and wondered how I should go about this...

StackOverflow: Orchard cms adding current widget types to custom module migration

The answer provided there is exactly how.