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.

Archive /

Our old forums are still available as a read-only archive.

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

Image list associated to a page


Go to End


2 Posts   1968 Views

Avatar
colymba

Community Member, 26 Posts

4 August 2008 at 1:25pm

Hi,
I am building my first website with SilverStripe and came across the following problem.

One of my template needs to be associated with a list of images that would be uploaded and managed via the CMS.
I used an HasManyComplexTableField as following to add and manage images:

class projectPage extends Page
{
	static $db = array
	(
		.....
	);	
	static $has_one = array
	(		
	);	
	static $has_many = array
	(
		'Images' => 'ProjectImage'
	);
   
      function getCMSFields()
     {
		$fields = parent::getCMSFields();			
                .....		
		
		$tablefield = new HasManyComplexTableField(
			$this,
			'Images',
			'ProjectImage',
			array(
				'Title' => 'Image Title'
			),
			'getCMSFields_forPopup'
		);
		$tablefield->setAddTitle( 'an Image' );		
		$fields->addFieldToTab( 'Root.Content.Images', $tablefield );
				
		return $fields;
	}

}

Here is the ProjectImage class:

class ProjectImage extends DataObject
{
	static $db = array
	(
		'Title' => 'Varchar'
	);
	
	static $has_one = array
	(
		'Project' => 'projectPage',
		'Image' => 'Image'
	);	
	
	function getCMSFields_forPopup()
	{
		$fields = new FieldSet();
		$fields->push( new TextField( 'Title' ) );
		$fields->push( new ImageField( 'Image' ) );
		return $fields;
	}
} 

This all works quite OK, I manage to display the list of images back on the page via <% control Images %> but I can't get it to display the images themselves. The $Title works fine on the other hand.
I tried using $Images, $Images.URL or $URL, but nothing seem to work. If someone could help me with that, it would be much appreciated.

Also, I was wondering some little things, to make this all work and look better....

Is it possible to add a preview of the picture within the HasManyComplexTableField on the CMS page? I tried to add the Image object to the field list of the table, but this makes the all thing crash.

Is it also possible to "link" the list of images to only one page (the one where they have been added from) rather than linked to the Page Type. So far if a new page is created, this page will also display the list of images added on any other pages (this can be messy to manage if for example, each projectPage contain 10+ images and there is 20+ projectPages...)

Maybe this is not the best way to achieve what I am trying to do. And I would be happy to hear any better solution.
I know this might be a lot to ask all at once, and I hope it all make sense.

Thanks for you help - Thierry

Avatar
colymba

Community Member, 26 Posts

5 August 2008 at 7:38am

never mind this... just solved my little problem: just had to use $Image in the template as it is the Image object child of the $Images list... anyway.. I'm happy with that...

Still, I would love to know if there is a way to associate some images of the list to a certain page, and so only display in the CMS the list of images associated to that page.

I have also been trying to use the setFolderName method in order to get those pictures saved in separate folders depending on the page. But this return an error in the CMS. I tried with existing and non-existing folders, different path, nothing works. How does this method works? Where do we specify the path from?

Thanks.