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

Translatable page with 1-to-many data objects


Go to End


4 Posts   2371 Views

Avatar
Martin Pales

Community Member, 19 Posts

9 October 2009 at 9:27pm

Hi,

I'm trying to set up a home page with several exchanging headlines. I created a Headline dataobject (with title, text, image) and added the has_many relationship to the homepage.

Everything works fine, but I have a small problem with CMS. The homepage is translatable and headlines are shared among those translated homepages (for each I can only select different headlines).

What I would like to see in the CMS, is a separate list of headlines for each translated homepage.
Any clue how to do this?

Harl

Avatar
Ingo

Forum Moderator, 801 Posts

10 October 2009 at 8:46pm

How are you managing the headlines? If you have a has_many to a page, you just have to filter for this page id (which implies a certain locale). For example, a ComplexTableField with the fieldname set to the name of your relationship will do this automatically.

Avatar
Martin Pales

Community Member, 19 Posts

11 October 2009 at 1:19am

Edited: 11/10/2009 1:26am

Thanks for the quick reply!

However, I don't fully understand what do you mean. Can you please be more specific and give me more details?

In the meantime I have changed the relationship to many-many since I would like to share the same headline among pages.

I would like to restrict the headlines availablity/selection in ManyManyDataObjectManager / ManyManyComplexTableField.
E.g. having 'english' headlines available for selection for english pages and german headlines for german pages.

Does my headline data object store locale when it is created somehow or should I make it translatable as well?

Thanks.

Harl

It looks like this:

class Headline extends DataObject {
	static $db = array(
		'Title' => 'Varchar',
		'Text' => 'HTMLText',
	);

	static $has_one = array(
		'Image' => 'Image',
	);

	static $belongs_many_many = array(
		'Pages' => 'Page'
	);

	function getThumbnail() {
		$Image = $this->Image();
		if ($Image) {
			return $Image->CMSThumbnail();
		}
		else {
			return null;
		}
	}

	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push(new TextField('Title'));
		$textField = new SimpleTinyMCEField('Text', 'Text', array(), 3);
		$textField->setButtons(array(
			'bold,italic,underline,|,link,unlink,anchor'
		));
		$fields->push($textField);
		$fields->push(new ImageField('Image'));
		
		return $fields;
	}

}

class Page extends SiteTree {
	static $db = array(
	);
	
	static $has_one = array(
	);

	static $many_many = array(
		'Headlines' => 'Headline'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();

		// headlines tab
		$headlinesTableField = new ManyManyDataObjectManager(
			$this,
			'Headlines',
			'Headline',
			array(
				'Title' => 'Title',
				'Text' => 'Text',
				'Thumbnail' => 'Image',
			),
			'getCMSFields_forPopup'
		);
		$headlinesTableField->setAddTitle('a Headline');
 
 		$fields->addFieldToTab('Root.Content.Headlines', $headlinesTableField);

		return $fields;
	}

}

Avatar
Ingo

Forum Moderator, 801 Posts

12 October 2009 at 9:01am

Not sure about ManyManyDataObjectManager, its a third party module. If you use ComplexTableField, you should get the relation setting for manymany out of the box (although you wont be able to choose existing elements, making it practically a has_many relationship). The trick is to filter for the current elements with $headlinesTableField->setCustomSourceItems($this->Headlines());

> Does my headline data object store locale when it is created somehow or should I make it translatable as well?
Your relationship is already set to a specific page ID, every page in every language has its own ID, so no Locale required :)