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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Virtual Pages problems


Go to End


3 Posts   2276 Views

Avatar
ho0m3s

Community Member, 4 Posts

5 January 2014 at 5:33am

Just started to use SilverStripe 3.1.2. The cms/framework is very powerful and gives me a lot of flexibility.
That being said i seem to have hit a relative big problem for my current project.

VirtualPages.

It seems as if i can only add Virtual Pages at the root of the SiteTree. When i try to add a virtual pages in a children tree node if the properties $allowed_children is not set then i can add virtual pages. Since i want to restrict users to only being able to create Page 1, Page 2 and Virtual Page by setting the $allowed_children property of the child tree node class.
when i create a page, at the children tree node, I get an error when selecting virtual page: "Page type "Page" not allowed as child of this parent page"

here is the current code:

static $allowed_children = array('*Page','CartusHolder','*VirtualPage','CartusPage');

if I set the $allowed_children = array('SiteTree') or don't set it at all, I am then able to create Virtual Pages no problems,

Any help would be appreciated.

Avatar
ho0m3s

Community Member, 4 Posts

7 February 2014 at 5:00am

the fix is to update the sitetree.php method validate and add an if statement.

look for this:
if(!in_array($subject->ClassName, $allowed)) {
...
}

and wrap it in a parent if statement like this:
if(!($this instanceof VirtualPage)){
if(!in_array($subject->ClassName, $allowed)) {
.....
}
}


public function validate() {
		$result = parent::validate();

		// Allowed children validation 
		$parent = $this->getParent();
		if($parent && $parent->exists()) {
			// No need to check for subclasses or instanceof, as allowedChildren() already 
			// deconstructs any inheritance trees already.
			$allowed = $parent->allowedChildren();
			$subject = ($this instanceof VirtualPage) ? $this->CopyContentFrom() : $this;
		
		

			if(!($this instanceof VirtualPage)){//UPDATED BY MARK using solution found on https://github.com/silverstripe/silverstripe-cms/issues/773
				if(!in_array($subject->ClassName, $allowed)) {
					
					$result->error(
						_t(
							'SiteTree.PageTypeNotAllowed', 
							'Page type "{type}" not allowed as child of this parent page', 
							array('type' => $subject->i18n_singular_name())
						),
						'ALLOWED_CHILDREN'
					);
				}
			}
			
		}
		
		// "Can be root" validation
		if(!$this->stat('can_be_root') && !$this->ParentID) {
			$result->error(
				_t(
					'SiteTree.PageTypNotAllowedOnRoot', 
					'Page type "{type}" is not allowed on the root level', 
					array('type' => $this->i18n_singular_name())
				),
				'CAN_BE_ROOT'
			);
		}
		//print_r($result);
		//exit;
		return $result;
	}

Avatar
martimiz

Forum Moderator, 1391 Posts

7 February 2014 at 7:12am

Could be that this was already raised as an issue on Github 8 months ago, with another possible solution.

https://github.com/silverstripe/silverstripe-cms/issues/773

Maybe you could have a look and possibly create a pull request? :)