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.

Data Model Questions /

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

Problem with Extension of ContentController


Go to End


5 Posts   3286 Views

Avatar
Optic Blaze

Community Member, 190 Posts

15 June 2015 at 5:27am

Hi,

I have a gallery module that i want decorate sitetree with so that all my pages have the photo gallery function in them (i.e blog, product page...they all must have the gallery module)

As i see it, this comprises of a 2 part solution.
1) GalleryPage extends DataExtension....this modifies the CMS etc. [b} I have this working already [/b}
2) GalleryPage_Controller extends Extension (and in my config file i add in Object::add_extension('ContentController', 'GalleryPage_Controller'); [b} This is not currently working and this is where i need help [/b}

Herewith the code i am currently using:
My _config.php file

 
// Extend the page class
SiteTree::add_extension('GalleryPage');
Object::add_extension('ContentController', 'GalleryPage_Controller');

My GalleryPage.php file

 
class GalleryPage extends DataExtension {
	
	private static $db = array(
	'GalFolder' => 'Varchar(100)'
	);
	
	private static $has_one = array(
	);
	
	// One gallery page has many gallery images
	private static $has_many = array(
    'GalleryImages' => 'GalleryImage'
  	);

	
	// Set Permissions
	function canEdit($Member = null){if(permission::check('EDIT_GALLERY')){return true;}else{return false;}}
	function canCreate($Member = null){if(permission::check('EDIT_GALLERY')){return true;}else{return false;}}

	// Get existing CMS Fields
	public function getCMSFields() {
   	$this->extend('updateCMSFields', $fields);
 	return $fields;
	}

	// Update CMS with new fields
	public function updateCMSFields(FieldList $fields) {
	$gridFieldConfig = GridFieldConfig_RecordEditor::create(); 
	$gridFieldConfig->addComponent(new GridFieldBulkUpload());
	$gridFieldConfig->addComponent(new GridFieldBulkManager());	
	
	// Creates field where you can type in the folder name --- IT WILL CREATE IN ROOT OF ASSET DIRECTORY!!!
	$fields->addFieldToTab("Root.ImageGallery", new TextField('GalFolder','Folder name')
	);
		
	// Used to determine upload folder
	if($this->owner->GalFolder!='' || $this->owner->GalFolder!=NULL) {
	// Specify the upload folder 
	$uploadfoldername = $this->owner->GalFolder;
	$gridFieldConfig->getComponentByType('GridFieldBulkUpload')
	->setUfSetup('setFolderName', $uploadfoldername)
	->setUfConfig('sequentialUploads', true);
	}
	else {
	$gridFieldConfig->getComponentByType('GridFieldBulkUpload')
	->setUfSetup('setFolderName', 'Gallery-Images')
	->setUfConfig('sequentialUploads', true);
	}
	
	// Customise gridfield
	$gridFieldConfig->removeComponentsByType('GridFieldPaginator'); // Remove default paginator
	$gridFieldConfig->addComponent(new GridFieldPaginator(20)); // Add custom paginator
	$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder')); 
	$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); // We only use bulk upload button			

	// Creates sortable grid field
	$gridfield = new GridField("GalleryImages", "Image Gallery", $this->owner->GalleryImages()->sort("SortOrder"), $gridFieldConfig);
	$fields->addFieldToTab('Root.ImageGallery', $gridfield);
	return $fields;
		
	}
	// Check that folder name conforms to assets class standards. remove spaces and special charachters if used
	function onBeforeWrite() {
		$this->owner->GalFolder = str_replace(array(' ','-'),'-', preg_replace('/\.[^.]+$/', '-', $this->owner->GalFolder));
	parent::onBeforeWrite();
	}
}

class GalleryPage_Controller extends Extension implements PermissionProvider  {
	private static $allowed_actions = array (
	);
		
	public function init() {
	parent::init();
	
	//Load  CSS requirements
	Requirements::css("ss3Gallery/css/bootstrap-image-gallery.css");
	Requirements::css("ss3Gallery/css/blueimp-gallery.min.css");

	//Load  Javascript requirements
	Requirements::javascript("ss3Gallery/js/jquery.blueimp-gallery.min.js");
	Requirements::javascript("ss3Gallery/js/bootstrap-image-gallery.min.js");
	Requirements::javascript("ss3Gallery/js/blueimp-gallery-fullscreen.js");

	// Call the blue imp jquery
	Requirements::customScript("
	
	document.getElementById('links').onclick = function (event) {
event = event || window.event;
var target = event.target || event.srcElement,
	link = target.src ? target.parentNode : target,
	options = {index: link, event: event},
	links = this.getElementsByTagName('a');
blueimp.Gallery(links, options);
};
	");
		}	
		 
		//Add permission check boxes to CMS
		public function providePermissions() {
		return array(
		  "VIEW_GALLERY" => "View Gallery Pages",
		  "EDIT_GALLERY" => "Edit Gallery Pages",
		);
		}
		
		// Set sort order for images
		public function GetGalleryImages() {
		return $this->owner->GalleryImages()->sort("SortOrder");
		echo "Hi Shaun";
		}
}

Avatar
Devlin

Community Member, 344 Posts

15 June 2015 at 8:55pm

Edited: 15/06/2015 9:16pm

Just skimming through the code.

Not tested anything, but a couple of thoughts:
- 'GalleryPage->getCMSFields' can be removed. The extension hook is named 'updateCMSFields'
- 'ContentController->init', the extension hook is named 'contentcontrollerInit'
- 'parent::init()' is unnecessary in an Extension and will produce ugly errors -- method 'init' does not exist on 'Extension'
- 'parent::onBeforeWrite();' is unnecessary and will produce errors -- DataExtension->onBeforeWrite() is abstract
- 'return $fields' is unnecessary
- I don't think 'providePermissions' has a extension hook...

Edit:
- 'ContentController->init', the extension hook is named 'contentcontrollerInit'
Further: the hook is actually part of the related DataObject and not of ContentController. Rename your 'init' method to 'contentcontrollerInit' and move the method to your GalleryPage Extension.

Avatar
Optic Blaze

Community Member, 190 Posts

16 June 2015 at 7:50am

Edited: 16/06/2015 7:51am

Thanks. I still cant get the requirements to load. I made the other changes to the GalleryPage class and then the following to the controller:

 

class GalleryPage_Controller extends Extension {
		
	public function contentcontrollerInit() {
	
	//Load  CSS requirements
	Requirements::css("ss3Gallery/css/bootstrap-image-gallery.css");
	Requirements::css("ss3Gallery/css/blueimp-gallery.min.css");

	//Load  Javascript requirements
	Requirements::javascript("ss3Gallery/js/jquery.blueimp-gallery.min.js");
	Requirements::javascript("ss3Gallery/js/bootstrap-image-gallery.min.js");
	Requirements::javascript("ss3Gallery/js/blueimp-gallery-fullscreen.js");

	// Call the blue imp jquery
	Requirements::customScript("
	
	document.getElementById('links').onclick = function (event) {
event = event || window.event;
var target = event.target || event.srcElement,
	link = target.src ? target.parentNode : target,
	options = {index: link, event: event},
	links = this.getElementsByTagName('a');
blueimp.Gallery(links, options);
};
	");
		}	

}

And the config file looks like this
// Extend the page class
SiteTree::add_extension('GalleryPage');
Page_Controller::add_extension('GalleryPage_Controller');

Avatar
Devlin

Community Member, 344 Posts

16 June 2015 at 7:52pm

Edited: 16/06/2015 7:55pm

'contentcontrollerInit' should be part of GalleryPage. There is no hook for it in the controller. The hook in the controller refers to the DataObject.

::yaml
SiteTree:
  extensions:
    - PageExtension

::php
class PageExtension extends DataExtension {
	public function contentcontrollerInit() {
		Requirements::customScript("alert('me');");
	}
}

Avatar
Pyromanik

Community Member, 419 Posts

16 June 2015 at 9:44pm