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

Auto Add new pages


Go to End


3 Posts   1283 Views

Avatar
landyman

Community Member, 11 Posts

6 October 2010 at 8:46am

Hello -
I am not sure if I can do this, so I thought I'd ask here.
I want the CMS to be able to add a Page automatically when other pages are added and certain conditions are met.

For example:
I have pages that include data for a city and state. I have city and state pages that act as directories for these products. Is there a way that I can add a page, have the CMS check if that city and state already exists, and if it doesn't, then add pages for the city and state? That way, a default one can be made, but a user can go and edit content for that city later if they want.

Thanks in advance!

Avatar
swaiba

Forum Moderator, 1899 Posts

6 October 2010 at 9:21pm

Hi,

I'd recommend overriding onAfterWrite function Page that you are adding, then that can call a function like the following to create the page if "certain conditions are met"... and make sure that the page has not already been created is one of those conditions

	function makePage($pg,$URLSegment,$Title,$strContent,$bShowInMenus,$bShowInSearch,$iParentID=-1)
	{
		$pg->URLSegment = $URLSegment;
		$pg->Title = $Title;
		$pg->MenuTitle = '';
		$pg->Content = $strContent;
		$pg->MetaTitle = '';
		$pg->MetaDescription = '';
		$pg->MetaKeywords = '';
		$pg->ExtraMeta = '';
		$pg->ShowInMenus = $bShowInMenus;
		$pg->ShowInSearch = $bShowInSearch;
		$pg->HomepageForDomain = '';
		$pg->ProvideComments = '';
		$pg->Sort = '';
		$pg->HasBrokenFile = false;
		$pg->HasBrokenLink = false;
		$pg->Status = '';
		$pg->ReportClass = '';
		$pg->CanViewType = 'Anyone';
		$pg->CanEditType = 'LoggedInUsers';
		$pg->ToDo = '';

		if ($iParentID!=-1)
			$pg->ParentID = $iParentID;

		$pg->write();

		$pg->writeToStage('Stage');
		$pg->publish("Stage", "Live");

		return $pg->ID;
	}

usage...

$this->makePage(
	new Page(),
	$strURLSegment,
	$strName,
	$strContent,
	true,
	true,
	$this->ID
);

Avatar
landyman

Community Member, 11 Posts

15 October 2010 at 6:39pm

Perfect. Thanks! I haven't added all the code yet, but this is definitely enough to get me going.