Layer rule based on Field?

Topics: Customizing Orchard, General
Jun 8, 2011 at 12:25 PM

Is it possible to have a layer rule that checks the field-values for a contenttype to determine whether or not it should be shown? I've had some trouble finding out how layer rules work at all. :)

My issue is that I have a site that has a column with pushes, ads and whathaveyou, and I want to be able to switch on or off that column per page (that is, contenttype item).

I'm open to any other solution that would grant me the same result, but I feel this would be the simplest way.

Jun 8, 2011 at 12:56 PM

Layers can't work that way - they don't have access to the current content item.

What you need instead is Paperclips, which is a feature I wrote to do this kind of work. In fact it's probably far more suitable for what you need, for a variety of reasons.

It's part of the Mechanics module. Basically it lets you create connections from a given content item to any other content items, and then add PaperclipPart to those connectors to define which zones they should get pushed to on Detail view.

It works equally well for pushing both content and widgets to zones, since widgets are just content anyway :) You can also have per-user and per-site widgets or content, by creating connections to the user or site content items. Then there are events you can hook into (ISocketEventHandler and IConnectorEventHandler) if you need to code any custom display rules, filter the connectors, etc.

Mechanics is available on the gallery (but you also need to install Origami first), or you can get the latest code for both modules and see the documentation at http://scienceproject.codeplex.com. The latest version has a number of fixes and improvements which will up on the gallery later after a bit more bugfixing.

The documentation isn't complete yet so if you need any help with it just ask :)

Jun 8, 2011 at 1:00 PM

Oh - there is a simpler option. You could use my LayoutSelector module instead, and have a second version of Layout.cshtml that just excludes that column - then choose which of the two layouts you want for each content item. But Paperclips is all-round better since it gives you total control over what goes where without duplicating the layout.

Jun 9, 2011 at 7:45 AM

I'm testing out the Mechanics module with Connectors and PaperclipParts right now, but I can't get it to work the way I want to exactly.

 

I have a widget with a container that I want to connect with a Page. I added sockets to both as well as the PaperclipPart, and added a new ContentType with the ConnectorPart added. I also did add Allowed Content Left (Page) and Allowed Content Right (ContainerWidget). I can now select a ContainerWidget to a Page, but my problem is that it's added just below the content of the Page, within the same zone, even though "AsideSecond:20" is specified in both of the PaperclipParts (that is, both on Page and on ContainerWidget).

Confusing? What am I doing wrong? :/

Jun 9, 2011 at 7:52 AM

Ahh, I was confused. Removed the PaperclipParts from Page and ContainerWidget and added one to the ContentType with the ConnectorPart. Works grand now. :) Thanks!

Jun 9, 2011 at 8:19 AM

Hm. However. Is there a way to "remove" a connection made? Seems like it can only be replaced, not removed.

Jun 9, 2011 at 9:23 AM

Cool :) Glad it's working! Yes, that's the main concept to grasp - the Connector type is being used to define metadata across the connection, so it's saying "when these two items are joined, push this one to that zone". So you could for instance push the same item to different zones using different connectors on different pages.

To remove a connection, tick the checkbox next to it, and select "Remove" from the drop-down list. Actually you might need the latest version from Codeplex for that if you don't have it already. The official gallery update will be today. There's also a serious performance improvement; with the old version, things would get exponentially slower as more connections were made (although it shouldn't be a problem for just a few paperclips).

After the immediate update, I'll be doing a bit of work improving the UI; it needs some basic AJAX behaviour, and a decent search combo instead of the basic drop-down list.

Jun 9, 2011 at 10:07 AM

Sweet, I'll be looking for the updates then :)

I noticed something weird though, I get an error message in seemingly unrelated places in the admin. The message I get is: 

"There are no defined Connectors that support this content type. Consider creating a Connector type by adding the ConnectorPart to a new or existing content type."

One example of this is "Admin/Users/Edit/x".

 

Am i doing something odd, or is this a bug?

Jun 9, 2011 at 11:04 AM

It's a known bug - someone else also reported it. I originally put the message in as a warning when you have a SocketsPart with no valid right-connectors. So you have SocketsPart on your User type there, but there aren't any connector editors to display. But it's a bug because there are situations where this is perfectly valid. I'll probably just remove the message altogether.