Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Automatically Creating Child Pages

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3 4
Go to End
Author Topic: 4511 Views
  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Automatically Creating Child Pages Link to this post

    Adding to this discussion with a related question, does anyone have any experience with manipulating the display of the SiteTree in the left CMS pane after creating a new Page in the manner which has been discussed in this thread?

    I have a certain class which performs several actions in the onAfterWrite() function (a DataObject in the DataObjectManager popup), amongst which the creation of a new page in the SiteTree which is linked to the saved DataObject. Everything is going fine (thanks to the posts in this thread!), but I'd like for the SiteTree to "refresh" itself so that the created Page is instantly visible to the user, without having to reload the whole CMS.

    This has to be possible somehow, as the same thing happens (indirectly) whenever a page in the SiteTree is (e.g.) duplicated. But I've been searching through classes for about half an hour so far and haven't stumbled upon anything which seems to do the trick.

    I've noticed that the duplicate() method in the CMSMain class uses the LeftAndMain class' returnItemToUser() method. That method seems to have a bit of custom JavaScript which manipulates the SiteTree. I've tried passing my newly created Page to the returnItemToUser() method, I've tried copy pasting bits of its JavaScript into my onAfterWrite() method (using Requirements::customScript()). None of this does the trick though.

    I've had a look in the tree.js file but can't seem to locate any straight-forward "refresh"-type function in there either (which I find odd). Finally, looking at the API for the LeftAndMain class, the following methods appear useful but I can't get them to work for me either:

    • ForceReload()
    • addTreeNodeJS()
    • ajaxupdateparent()

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Automatically Creating Child Pages Link to this post

    Okay, one step closer

    By mucking around in Firebug I've produced some JavaScript which does exactly what I want, which I've added to my onAfterWrite() method as seen below:

    public function onAfterWrite() {
    // A bunch of code ...

    Requirements::customScript("
    var newNode = $('sitetree').createTreeNode(".$oPage->ID.", '".$oPage->Title."', 'Page');
    var parentNode = $('sitetree').getTreeNodeByIdx(".$this->PageID.");
    parentNode.appendTreeNode(newNode);
    ");

    // A bunch of code ...

    parent::onAfterWrite()
    }


    As said, when I execute those three lines of JavaScript in Firebug it works perfectly. Unfortunately, this is never executed after clicking the "Save" button in the DOM popup. Does that have to do with the fact that the popup is still open? Or is it just not possible to include JavaScript in the onAfterWrite() method as the scope is entirely different? If so, does anyone have any suggestions how I could set this up to be executed whenever a DataObject is created (causing a Page to be created)?

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Automatically Creating Child Pages Link to this post

    Coming back to this almost a year later - for a different project - I thought I'd share this with anyone else trying to manipulate the SiteTree pane in the CMS.

    For the project we're currently working on, we want to copy the contents of a SiteTree node in a Subsite template to a node in the SiteTree of another Subsite. We've extended the LeftAndMainDecorator in order to add process a button we added to the CMSActions and that is where the duplication and other actions are handled.

    In order to get the SiteTree pane of the current Subsite to refresh itself, we've added the following to our function:

    Requirements::customScript("
    $('sitetree').reload();
    ");


    Yes, it's that simple!

    4511 Views
Page: 1 2 3 4
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.