JustAndrei
JustAndrei

Reputation: 859

Composite C1: How to make the page tree to display UrlTitle instead of Title (Solution)

When Composite C1 displays the tree of sites and pages in the Content perspective, it uses the page Title property as the node label. This is not always comfortable, especially when the pages are translated to different cultures, so you can't even read those titles and quickly find the page you are looking for. The idea is to use the UrlTitle as the label, so every tree node would represent a part of the page URL. See the solution below.

Upvotes: 1

Views: 105

Answers (1)

JustAndrei
JustAndrei

Reputation: 859

I didn't want to recompile the assemblies, so my hack is ugly, but affects only javascript and aspx.

  1. Edit /Composite/scripts/compressed/top.js.

    Find there SystemTreeNodeBinding.prototype.onBindingAttach=function(){ and inject the following code at the beginning of this function:

    if(window.treeNodeProcessor)window.treeNodeProcessor(this.node);
    

    Now you can modify the tree node before it is displayed; you just need to create your global function treeNodeProcessor.

  2. Edit /Composite/top.aspx (top.aspx.cs is affected as well, so save them together).

    At the end of the head element add your javascript:

    <script type="text/javascript">
        function byKey(key) {
            return function(p) {
                return p.Key == key;
            }
        }
    
        window.treeNodeTitles = {
    <% WriteTreeNodeTitles(); %>
            "": ""
        };
    
        window.treeNodeProcessor = function(node) {
            if(node._data.PropertyBag) {
                var uri = node._data.PropertyBag.find(byKey('Uri')).Value;
                node._data.Label = window.treeNodeTitles[uri] || node._data.Label;
            }
        }
    
    </script>
    

    Unfortunately, there's no UrlTitle in the node object passed to treeNodeProcessor. However, every page node has PropertyBag that stores a value like ~/page(a9d30645-02f7-4412-bd4e-6f3a02782481) under key Uri. So, you have to query the UrlTitle on your own, what is made in method WriteTreeNodeTitles (see below).

  3. Edit /Composite/top.aspx.cs.

    Add new method:

    protected void WriteTreeNodeTitles()
    {
        using (var conn = new DataConnection())
        {
            foreach( string line in conn.Get<IPage>().Select( p => "            \"~/page(" + p.Id.ToString().ToLower() + ")\": \"" + p.UrlTitle + "\",\r\n" ) )
            {
                Response.Write( line );
            }
        }
    }
    

    You have to add few usings, of course:

    using System.Linq;
    using Composite.Data;
    using Composite.Data.Types;
    

So, now your top.aspx contains the mapping of page guid-like urls to UrlTitles, and the treeNodeProcessor function that uses that mapping for modifying your page tree.

Upvotes: 1

Related Questions