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

Image gallery not working


Go to End


6 Posts   1265 Views

Avatar
RobboNoCash

Community Member, 33 Posts

19 August 2015 at 2:41pm

Hi there is a error in this code.
I am trying to create a image gallery with multiple pictures.
Anyone know whats wrong with the code?

Thanks

<?php
class Gallery extends Page {
private static $db = array(
'Heading' => 'Text'
);

public static $has_many = array(
'Picture' => 'Image'
);

public function getCMSFields() {
$f = parent::getCMSFields();

$f->addFieldToTab('Root.Main', new TextField('Heading'), 'Content');

$f->addFieldToTab("Root.Images", new UploadField('Picture'));

return $f;

}

}

class Gallery_Controller extends Page_Controller {
}

Avatar
helenclarko

Community Member, 166 Posts

19 August 2015 at 3:42pm

Hi RobboNoCash,

Firstly, do you have any errors output?
Also, what are you expecting to see, just a heading textfield and an upload field for an image?

I see no major issues in your coding. However, I have cleaned up a few things.

<?php

class Gallery extends Page {
	private static $db = array(
		'Heading' => 'Text'
	);

	public static $has_many = array(
		'Picture' => 'Image'
	);

	public function getCMSFields() {
		$f = parent::getCMSFields();

		$f->addFieldToTab('Root.Main', new TextField('Heading', 'This is a heading'));

		$f->addFieldToTab("Root.Images", new UploadField('Picture', 'Upload and Attach a Picture'));

		return $f;

	}

}

class Gallery_Controller extends Page_Controller {}

There are a few gallery addons which already exist, have you looked at this one from Frank Mullenger?

http://addons.silverstripe.org/add-ons/frankmullenger/gallery

-helenclarko

Avatar
RobboNoCash

Community Member, 33 Posts

19 August 2015 at 3:54pm

There is one heading and many images.
Does anyone know what is wrong with the code??

Thanks

Avatar
helenclarko

Community Member, 166 Posts

19 August 2015 at 4:12pm

Edited: 19/08/2015 4:13pm

Hi RobboNoCash,

What are you expecting to see?
There doesnt seem to be any issues with your code from what I can see.
Have you created your template (.ss) file for your Gallery?

Once again, I recommend looking at a Gallery addon.
http://addons.silverstripe.org/add-ons/frankmullenger/gallery

-helenclarko

Avatar
RobboNoCash

Community Member, 33 Posts

19 August 2015 at 5:31pm

I am expecting to see a Heading and rows of images.
I have created a template for it too.
When I click the page in the admin console is says Internal Server Error and it does not load the page and the fields.
I can't seem to find any errors in my code either, but there must be a error because it is not loading.

Thanks

Avatar
Pyromanik

Community Member, 419 Posts

19 August 2015 at 9:08pm

Edited: 19/08/2015 9:10pm

many_many to image.
Also needs to be private static.