Max_Salah
Max_Salah

Reputation: 2497

How to configure a rootPath in CQ Dialog depending on current page

I have the following CQ dialog.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    xmlns:nt="http://www.jcp.org/jcr/nt/1.0" jcr:primaryType="cq:TabPanel"
    activeTab="{Long}0" title="mContactConnect_dialogtitle" headerAsText="true"
    xtype="tabpanel">
    <items jcr:primaryType="cq:WidgetCollection">
        <tab1 jcr:primaryType="cq:Widget" anchor="100%" title="myTitle"
            xtype="panel">
            <items jcr:primaryType="cq:WidgetCollection">
                <subheadline jcr:primaryType="cq:Widget" fieldLabel="subheadline_label"
                    name="./subheadline" maxLength="80" xtype="textfield"/>
                <text jcr:primaryType="cq:Widget" fieldLabel="text_label" name="./text"
                    maxLength="150" xtype="textfield"/>
                <reference jcr:primaryType="cq:Widget" fieldLabel="reference_label"
                    name="./reference" forceSelection="true" xtype="pathfield" rootPath ="/content" />
            </items>
        </tab1>
    </items>
</jcr:root>

Now I would like to reference to certain rootPath depnding on current page. For this, I extend the PathField as follow:

myExtendedRootPath = CQ.Ext.extend(CQ.Ext.emptyFn, {
    init : function(widget) {
        var currentPath;
        var siteAdmin = CQ.Ext.getCmp("cq-siteadmin");

        if (siteAdmin) {
            currentPath = siteAdmin.getSelectedPages().shift().id;
        } else {CQ.Ext.
            currentPath = CQ.utils.WCM.getPagePath();
        }

        if (currentPath =="page1") {
            widget.treeRoot.name = "content/page1";
        }
        else if (currentPath =="page2") {
            widget.treeRoot.name = "content/page2";
        }
    }
});

CQ.Ext.reg("myExtendedRootPath", myExtendedRootPath.init());

I follow the function call with firebug. For this I set breakpoint on the line init : function(widget) {. When I step forward, the steppointer jump to the last line CQ.Ext.reg...

Why the init Function not call?

Upvotes: 1

Views: 7100

Answers (2)

apil.tamang
apil.tamang

Reputation: 2725

This is for all those who maybe interested in setting the defaultValue of a pathfield component dynamically. This worked for me to set the value to the current page-path:

(Follow the plugin src code as mentioned above). Then, use this:

widget.setValue(cq.utils.WCM.getPagePath()).

What was interesting was that I had to actually manually define a 'defaultValue' field in the dialog.xml before the line above successfully worked! Strange.. so my dialog would look as the follows:

<reference
    jcr:primaryType="cq:Widget"
    fieldLabel="reference_label"
    name="./reference"
    forceSelection="true"
    xtype="pathfield"
    defaultValue="/content/any/random/string"
    plugins="myExtendedRootPath" />

Upvotes: 0

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

  1. Register created object as a plugin. Replace the last line with:

    CQ.Ext.ComponentMgr.registerPlugin('myExtendedRootPath', myExtendedRootPath);
    
  2. Add registered plugin to the reference dialog field:

    <reference
        jcr:primaryType="cq:Widget"
        fieldLabel="reference_label"
        name="./reference"
        forceSelection="true"
        xtype="pathfield"
        rootPath ="/content"
        plugins="myExtendedRootPath" />
    
  3. Fix syntax error (unexpected CQ.Ext.):

    if (siteAdmin) {
        currentPath = siteAdmin.getSelectedPages().shift().id;
    } else {CQ.Ext. // <-- here
        currentPath = CQ.utils.WCM.getPagePath();
    }
    

Optionally, find a working example here.

Upvotes: 3

Related Questions