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.

All other Modules /

Discuss all other Modules here.

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

Subsites - hack to filter page types by site


Go to End


1817 Views

Avatar
Quarx

Community Member, 12 Posts

20 April 2010 at 12:27am

I had to add a simple filtering by site type id (without this it will be a mess with 2 or 3 websites on one ss). It's not perfect but maybe someone will find it useful or will polish it further... This is how I did it.

1. subsites/javascript/LeftAndMain_Subsite.js

Modify onchange with the following code:

onchange: function() {
			if($('Form_AddPageOptionsForm_SubsiteID')) {
				$('Form_AddPageOptionsForm_SubsiteID').value = this.value;
			}
			var subsiteID = this.value;
			jQuery.ajax({
				  url: SiteTreeHandlers.controller_url + '/changesubsite?SubsiteID=' + this.value + '&ajax=1',
				  success: function(sitetree_res) {
						jQuery.ajax({
							  url: SiteTreeHandlers.controller_url + '/AddPageOptionsForm&subsiteID='+subsiteID+'&ajax=1',
							  success: function(responseText) {
									// Modify form with ajax response (we take only options, the reset remains unchanged) 
									var cur = jQuery('#Form_AddPageOptionsForm').attr('id','Form_AddPageOptionsForm_Temp');
									var cur_inner = jQuery('#Form_AddPageOptionsForm_PageType').attr('id','Form_AddPageOptionsForm_PageType_Temp');
									var mod = jQuery('<div style="display:none">'+responseText+'</div>').insertAfter("#Form_AddPageOptionsForm_Temp");
									var mod_inner = jQuery("#Form_AddPageOptionsForm_PageType").html();																		
									mod.remove();
									cur_inner.html(mod_inner);
									cur.attr('id','Form_AddPageOptionsForm');
									cur_inner.attr('id','Form_AddPageOptionsForm_PageType');
									
									// Fix for CMSMainLeft
									addpageclass.prototype.originalValues.length = 0;									
									jQuery("#Form_AddPageOptionsForm_PageType option").each(function(){	
										addpageclass.prototype.originalValues.push({
											'value': jQuery(this).val(),
											'label': jQuery(this).html()
										});
									})
									
									if ($('sitetree')) {
										$('sitetree').innerHTML = sitetree_res;
										SiteTree.applyTo($('sitetree'));
										$('sitetree').getTreeNodeByIdx(0).onselect();
										$('siteTreeFilterList').reapplyIfNeeded();
									}
							  	
							  }
							});
			}});			
		}
	},

2. cms/code/CMSMain.php

Modify AddPageOptionsForm() with the following code at the end:

		$out = new Form($this, "AddPageOptionsForm", $fields, $actions);
		if(Director::is_ajax() || $_GET["ajaxDebug"])
			return($out->forAjaxTemplate());
		return($out);

3. sapphire/core/SiteTree.php

In page_type_classes() at the following code:

			$current_subsite_id = Session::get('SubsiteID');
			$parent_subsite_id = $instance->stat('parent_subsite_id');
			if(isset($_GET['subsiteID']))$current_subsite_id=(int)$_GET['subsiteID'];
			if(isset($current_subsite_id) && isset($parent_subsite_id) && $parent_subsite_id != $current_subsite_id) {
				$kill_ancestors[] = $class;	
			}

after:

			if($ancestor_to_hide = $instance->stat('hide_ancestor')) {
				// note for killing later
				$kill_ancestors[] = $ancestor_to_hide;
			}

4. In all Pages where you want to limit page visibility to just one subsite add the following code to your class:

public static $parent_subsite_id = YOUR_SUBSITE_ID; //your main site has "0" id

Without parent_subsite_id your page will be visible for all subsites

I tested this hack on SS2.4rc1 and it worked for: Create Page, Behavior tab and even with allowed_children set up.
It's not perfect solution but I had to make it quickly since we I need to add a few subsites very soon and I have to move on to other tasks.