Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Automatically Creating Child Pages


Go to End


27 Posts   8787 Views

Avatar
Xurk

Community Member, 50 Posts

2 November 2011 at 11:52pm

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()

Avatar
Xurk

Community Member, 50 Posts

3 November 2011 at 12:20am

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)?

Avatar
Xurk

Community Member, 50 Posts

29 August 2012 at 10:34pm

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! :)

Go to Top