Reputation: 2200
I have a "promo" widget which wraps the built in Universal Page Viewer web part. Editors can add promo widgets to their pages in order to link to any other page in the site. The promo widget they add will render with the name of the linked page and the teaser image for that page.
The widget is configured to show the Path field so that the editor can pick the page they're promoting, and has the default value of the TransformationName field set to "CMS.MenuItem.Promo". The TransformationName
field is marked as non-editable.
I'd really like the editor to be able to point their promo widget at any page in the site and not have to worry about what type of node they're pointing to, so I'd like to make the CMS.MenuItem.Promo transformation support any page type.
My idea is to add a custom transformation method like:
namespace CMS.Controls
{
public partial class CMSTransformation
{
public string GetTeaser()
{
TreeNode node = this.get_the_current_node_somehow...();
Page page = new PageFactory().GetPage(node);
return page.GetTeaserRelativeURL();
}
Then I can just include the following in the transformation and editors don't need to care about what type of node they have:
<img src="<%# GetTeaser() %>" />
I've already got PageFactory
setup and working (it's used by some other code) - all it does is switch on node.ClassName
and return a new Page object:
public Page GetPage(TreeNode node)
{
switch (node.ClassName)
{
case CMS_Event.PageType:
return new CMS_Event(node);
case CMS_MenuItem.PageType:
return new CMS_MenuItem(node);
// etc
e.g. CMS_MenuItem is just:
public class CMS_MenuItem : Page
{
public const string TeaserField = "MenuItemTeaserImage";
private readonly TreeNode node;
public CMS_MenuItem(TreeNode node)
{
this.node = node;
}
public override string GetTeaserRelativeURL()
{
var guid = node.GetValue<Guid>(TeaserField, Guid.Empty);
return AttachmentURLProvider.GetAttachmentUrl(guid, node.NodeAlias);
}
Is it possible to get a TreeNode
object inside a transformation method? If so I can just use my existing code to get the teaser image regardless of the type of node I have.
If it's not possible to get a TreeNode
in the transformation method, then what's the cleanest way to get out a field regardless of the current page type?
I'm aware of the GetNotEmpty()
and GetColumnName()
transformation methods, but it seems pretty ugly to have to pass a list of 12 column names every time I try and get a field out.
Upvotes: 0
Views: 218
Reputation: 415
I think it would be easier to have a specific transformation per page type. You could have a transformation with the same code e.g. Promo and have this transformation for every page type. In the settings of your widget/web part you would initialize the property via a macro. The macro would simply get the displayed page type code name. The default value for the given property could look something like:
{%my_macro_to_get_page_type_code_name%}.promo
I think you would also be able to get the code name via standard K# language, but I personally haven't done this. An alternative is to use a custom form control, which could be initialized however you like.
Upvotes: 4