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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to set up TableField


Go to End


2 Posts   2075 Views

Avatar
klikhier

Community Member, 150 Posts

6 April 2011 at 7:42pm

I would like to add a TableField to a tab in the CMS to simple add a list of hyperlinks (title + link) to a page. Problem: in my current setup when I change/add a hyperlink on a page, the change is reflected on all other pages as well. What should I change to prevent this from happening?

 
//mysite/code/Hyperlink.php

<?php

class Hyperlink extends DataObject
{
	static $db = array (
    'Title' => 'Text',
    'Link' => 'Text'
	);

	static $has_one = array (
		'CaseExamplePage' => 'CaseExamplePage'
	);

	public function getCMSFields_forPopup()
	{
		return new FieldSet(
      new TextField('Title'),
      new TextField('Link')
		);
	}

}

?>

//mysite/code/CaseExamplePage.php
 
<?php
class CaseExamplePage extends Page {

	public static $has_many = array(
    'Hyperlinks' => 'Hyperlink'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		
		// Add Hyperlinks tab
		$tblf = new TableField(
			//$this,
			'Hyperlinks',
			'Hyperlink',
			array('Title' => 'Title', 'Link' => 'Link'),
			array('Title' => 'TextField', 'Link' => 'TextField'),
			null,
			"Hyperlink.CaseExamplePageID",
			$this->ID
		);
		$fields->addFieldToTab('Root.Content.Hyperlinks', $tblf);

(...)

Avatar
Jonathan Hyatt

Community Member, 11 Posts

16 May 2012 at 7:48am

Check the parameters of the TableField constructor. I think it should be:

      $tblf = new TableField( 
         'Hyperlinks', 
         'Hyperlink', 
         array('Title' => 'Title', 'Link' => 'Link'), 
         array('Title' => 'TextField', 'Link' => 'TextField'), 
         "CaseExamplePageID", 
         $this->ID 
      ); 

Jonathan.