Skip to main content

Get Linked Components TBB

This weeks "TBB of the Week" is Get Linked Components TBB. I don't know who exactly wrote it, so I'm not taking credit for it. It is part of the Templating Base project, IIRC.

Name
Get Linked Components TBB
Type
·    Template in .NET Assembly
Description
Used to:
·    Extract Component links from Page metadata or from Component fields;
·    Create Package items from the identified Components;
Notes:
When placed on a Page Template, this generic TBB identifies Component Links from the Page’s Metadata section.
When placed on a Component Template, it identifies Component Links in the current Component’s fields (including RTF and Component Link fields) and metadata fields.
The TBB pushes each identified Component into the Package.
Parameters
n/a
Applicable to
Component Templates and Page Templates

This is a very powerful TBB that I use in almost all my projects. The issue it solves is the following: whenever there a Component Link appears in Component's RTF or link field (or Page metadata), it is impossible using Dreamweaver syntax alone, to access the linked Component fields directly. Instead, this TBB should be used to place the linked Component in the Package and then access its fields.

The Code

[TcmTemplateTitle("Get Linked Components TBB")]
public class GetLinkedComponents : ITemplate {

    private Engine _engine = null;
    private Package _package = null;
    private TemplatingLogger _log = null;
    //-1 = not set, 0 = component, 1 = page
    private int _renderContext = -1;

    /// <summary>
    /// Executes the transformation
    /// </summary>
    /// <param name="engine">Templating engine (context for the template code)</param>
    /// <param name="package">Transformation context (contains both the inputs and the outputs of the transformation)</param>
    void ITemplate.Transform(Engine engine, Package package) {
        _engine = engine;
        _package = package;
        _log = TemplatingLogger.GetLogger(this.GetType());

        if (IsPage) {
            //Add linked components from metadata
            _log.Info("Scanning Page Metadata for link fields...");
            AddLinkedComponents(GetContextItemFields("meta"), "MetaData.", 0);
        } else {
            //Add both linked components from standard schema and metadata fields
            //_log.Info("Scanning Component for link fields...");
            AddLinkedComponents(GetContextItemFields("link"), string.Empty, 0);
            //_log.Info("Scanning Component Metadata for link fields...");
            AddLinkedComponents(GetContextItemFields("meta"), "MetaData.", 0);
            //Fix for RenderComponentField
            Item MainComponent = _package.GetByName("Component");
            _package.Remove(MainComponent);
            _package.PushItem("Component", MainComponent);
        }
    }

    /// <summary>
    /// Add linked components to the package from the given fields
    /// </summary>
    /// <param name="fields">The ItemFields to search for component links</param>
    /// <param name="packageKeyPostfix">Postfix to add to package item name</param>
    private void AddLinkedComponents(ItemFields fields, string packageKeyPrefix, int depth) {
        if (fields != null) {
            Hashtable itemFieldCounter = new Hashtable();
            foreach (ItemField itemField in fields) {
                String itemFieldName = itemField.Name.ToString();

                if (!itemFieldCounter.ContainsKey(itemFieldName)) {
                    itemFieldCounter[itemFieldName] = 0;
                } else {
                    itemFieldCounter[itemFieldName] = (int)itemFieldCounter[itemFieldName] + 1;
                }

                if (itemField is ComponentLinkField) {
                    ComponentLinkField field = itemField as ComponentLinkField;

                    if (depth > 0) {
                        _log.Info("Found 1 deep: " + field.Name);
                    }

                    if (field != null && ((field.Definition.MaxOccurs == 1 && field.Value != null) || field.Values != null)) {
                        // --------------------------
                        Item item = null;
                        if (field.Definition.MaxOccurs == 1 && field.Value != null) {
                            //If the field is single value, add it to the package as a component
                            //_log.Info("Found single value link field: " + field.Name);
                            item = _package.CreateTridionItem(ContentType.Component, field.Value.Id);
                        } else {
                            //Otherwise create a uri list of all values
                            IList<TcmUri> uriList = new List<TcmUri>();
                            foreach (Component linkedComp in field.Values) {
                                uriList.Add(linkedComp.Id);
                            }
                            if (uriList.Count > 0) {
                                //_log.Info("Found multivalue link field: " + field.Name);
                                item = _package.CreateComponentUriListItem(ContentType.ComponentArray, uriList);
                            }

                        }
                        if (item != null) {
                            _package.PushItem(packageKeyPrefix + field.Name, item);
                        }
                        // --------------------------
                    }
                }
                    /* */
                else if (itemField is EmbeddedSchemaField) {
                    EmbeddedSchemaField field = itemField as EmbeddedSchemaField;
                    ItemFields fieldValues = field.Values as ItemFields;
                    /* * /
                    if (field.Definition.MaxOccurs == 1 && field.Value != null)
                    {
                        //If the field is single value, add it to the package as a component
                        //_log.Info("Found single value Embedded field: " + field.Name + " -- " + field.Value.ToString());
                        //item = _package.CreateTridionItem(ContentType.Component, field.Value);
                    }
                    else
                    /* */
                    if (field.Values.Count > 0) {
                        //Otherwise create a uri list of all values
                        foreach (ItemFields linkedComp in field.Values) {
                            foreach (ItemField iField in linkedComp) {
                                String lItemFieldName = itemFieldName + "." + iField.Name.ToString();

                                if (!itemFieldCounter.ContainsKey(lItemFieldName)) {
                                    itemFieldCounter[lItemFieldName] = 0;
                                } else {
                                    itemFieldCounter[lItemFieldName] = (int)itemFieldCounter[lItemFieldName] + 1;
                                }

                                String newPackageKeyPrefix = packageKeyPrefix + lItemFieldName + itemFieldCounter[lItemFieldName];

                                // pass the fieldValues if its not null, and go only 1 deep
                                //_log.Info("Possible: " + newPackageKeyPrefix);
                                //_log.Info("???: " + field.Values.ToString());
                                //_log.Info("Found object: " + itemField.GetType().ToString());

                                if (iField is ComponentLinkField) {
                                    ComponentLinkField convIField = iField as ComponentLinkField;

                                    if (convIField != null && ((convIField.Definition.MaxOccurs == 1 && convIField.Value != null) || convIField.Values != null)) {
                                        Item convItem = null;
                                        if (convIField.Definition.MaxOccurs == 1 && convIField.Value != null) {
                                            //If the field is single value, add it to the package as a component
                                            //_log.Info("Found single value link field: " + field.Name);
                                            convItem = _package.CreateTridionItem(ContentType.Component, convIField.Value.Id);
                                        } else {
                                            //Otherwise create a uri list of all values
                                            IList<TcmUri> convUriList = new List<TcmUri>();
                                            foreach (Component convLinkedComp in convIField.Values) {
                                                if (convLinkedComp != null) {
                                                    convUriList.Add(convLinkedComp.Id);
                                                    _log.Info("Found multivalue link field: " + convLinkedComp);
                                                }
                                            }
                                            if (convUriList.Count > 0) {
                                                convItem = _package.CreateComponentUriListItem(ContentType.ComponentArray, convUriList);
                                            }

                                        }
                                        if (convItem != null) {
                                            _package.PushItem(newPackageKeyPrefix, convItem);
                                        }
                                    }
                                } else if (iField is EmbeddedSchemaField) {
                                    AddLinkedComponents(((EmbeddedSchemaField)iField).Value, newPackageKeyPrefix, depth + 1);
                                }
                            }
                        }

                    }
                    /* */

                } // end else if (itemField is EmbeddedSchemaField)
                else {
                    //_log.Info("Found itemField: " + itemField.Name.ToString());
                }
            }
        }
    }

    /// <summary>
    /// Get the ItemFields for the context item
    /// </summary>
    /// <returns></returns>
    private ItemFields GetContextItemFields(string type) {
        ItemFields fields = null;
        if (IsPage) {
            Item item = _package.GetByName("Page");
            if (item != null) {
                string pid = item.GetValue("ID");
                Page page = _engine.GetObject(pid) as Page;
                if (page.Metadata != null)
                    fields = new ItemFields(page.Metadata, page.MetadataSchema);
            }
        } else {
            Item item = _package.GetByName("Component");
            if (item != null) {
                string cid = item.GetValue("ID");
                Component comp = _engine.GetObject(cid) as Component;
                if (type == "meta" && comp.Metadata != null) {
                    fields = new ItemFields(comp.Metadata, comp.MetadataSchema);
                } else if (type == "link") {
                    fields = new ItemFields(comp.Content, comp.Schema);
                }
            }
        }
        return fields;
    }

    /// <summary>
    /// True if the rendering context is a page, rather than component
    /// </summary>
    private bool IsPage {
        get {
            if (_renderContext == -1) {
                if (_engine.PublishingContext.ResolvedItem.Item is Page)
                    _renderContext = 1;
                else
                    _renderContext = 0;
            }
            if (_renderContext == 1)
                return true;
            else
                return false;
        }
    }
}

Comments

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven