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.

Customising the CMS /

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

Can I put a TreeMultiselectField into a ComplexTableField


Go to End


9 Posts   3096 Views

Avatar
LinseyM

Community Member, 99 Posts

3 March 2010 at 3:54am

Hi there,

Looking for some advice.

I want to create a new DataObject called "RelatedContent"

This will take the form of a new tab in the CMS, available from any page, that allows the Admin to add links to related content (pages) within the site.

E.G. If they were editing a page about "Document Translation into Urdu" then they might want to link to Case Study pages that relate to this.

I had planned to create a simple data object along the lines of:

<?php
class RelatedContent extends DataObject {
static $db = array(
'DisplayTitle' => 'Text',
'PageURL' => '???',
);

static $has_one = array(
);

static $default_sort = "DisplayTitle ASC";
}
?>

This would then be invoked in the CMS with:

class Page extends SiteTree {
....
static $has_many = array(
'RelatedContents' => 'RelatedContent'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$RelatedContentsTable = new ComplexTableField($this, 'RelatedContents', 'RelatedContent', array('DisplayTitle'=> 'Show.Link.As', 'PageURL'=> 'Links.To.Page'));
$fields->addFieldToTab('Root.Content.RelatedContent', $RelatedContentsTable);
return $fields;
}

}

However, I want the PageURL component to be a dropdown in the CMS with links to all the pages in the site tree (and not a textbox, so that the clients isnt copy/pasting links)... basically just mimicing thebehaviour when you add a link from the WYSIWYG editor. I know that there is this "TreeMultiselectField" but I can't work out how to use it to achieve this.

Any help appreciated, thank you :)

Avatar
Juanitou

Community Member, 323 Posts

3 March 2010 at 7:06am

Edited: 03/03/2010 7:10am

Hi Linsey!

EDIT: It’s not a DataObject you need, but a pointer to SiteTree, I think, so a relation between Pages…

I’m not sure it’s the best way to do it, or even if it works, but try on the following basis:

Page.php:

class Page extends SiteTree {
	// Pages can have many related content
	public static $many_many = array(
		'RelatedContent' => 'RelatedContent'
	);

	function getCMSFields() {
		$fields = parent::getCMSFields();

		/* Add Related Content tab
		** It's a ManyManyComplexTableField
		*/
		$rcTablefield = new ManyManyComplexTableField(
			$this,
			'RelatedContent',
			'RelatedContent',
			array(
				'Title' => 'DisplayTitle'
			));
		$fields->addFieldToTab('Root.Content.RelatedContent', $rcTablefield );
	}
}

RelatedContent.php:

class RelatedContent extends SiteTree {
	static $db = array(
		'DisplayTitle' => 'Text',
	);

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

I’m quite sure that it will break around the ManyManyComplexTableField constructor, but you should debug it quite easily.

Hope it helps,
Juan

Avatar
Juanitou

Community Member, 323 Posts

3 March 2010 at 7:47am

I think I could be misleading you, I’m sorry, I did not read you were looking for a TreeMultiselectField. Maybe you could simply make a has-many "RelatedContent" => "SiteTree", without DataObject. Have you tried it?

Avatar
Willr

Forum Moderator, 5523 Posts

3 March 2010 at 9:26am

As a general note: Any has_many relationship needs a has_one on the other end. So LinseyM your orignal code snippet would need ..

class RelatedContent extends DataObject { 
   static $db = array( 
      'DisplayTitle' => 'Text'
   );

   static $has_one = array( 
     'Page' => 'SiteTree'
   );
...

But that does raise a point like Juanitou touched on - do you want a has_many (so related content can only be related to one page) or can can related content attach to multiple pages. I think tree multiselectfield should handle many_many relations but not 100% sure.

Avatar
LinseyM

Community Member, 99 Posts

3 March 2010 at 10:38am

Hi there,

Thanks for the feedback so far...

Will:
OK, let me think... I reckon I want a many/many relationship, so say there were 6 case studies in total... some might be referenced by more than one page, some might only be linked to once, and some not at all... just as a really simple example, to make sure I am explaining it correctly:

e.g.

- Interpreting Page has links to related content pages:
Case Study 1
Case Study 3
Case Study 5

- Translation Page has links to related content pages:
Case study 3

- Copywriting Page has link to related content pages:
NONE

- Typesetting Page has link to related content pages:
Case Study 1
Case Study 5
Case Study 6

Juanitou:
I am only assuming that its a treeMultiselect that I need - there might be a better option that I don't know about!

Avatar
LinseyM

Community Member, 99 Posts

3 March 2010 at 10:43am

Should have added - reason i wanted to use data objects and not just do virtual / redirect pages in a 'submenu' is that in the future I might want to expand this to link to external websites, pdf files etc... (and if there are a lot of related items on a lot of pages, then the site tree could get far too big and unmanageable). So, bacl to the main point, where the link is an internal site link, I'd like them to be able to select the page (case study) from the site tree dropdown menu, as they are used to doing that when creating links in the wysiwyg editor.

Many thanks!

Linsey

Avatar
LinseyM

Community Member, 99 Posts

5 March 2010 at 4:13am

Edited: 05/03/2010 4:14am

Hi there,

Can you offer any further help on how I can get the site tree into a dropdown format on my complextablefield in the CMS?

Been messing about with it so much that I've just tied myself in knots! It's a many/many, but it doesn't seem to work - just get errors... :)

Thanks

Avatar
Juanitou

Community Member, 323 Posts

8 March 2010 at 6:33am

Hi Linsey!

I’m not going to be very helpful here. I’ve been testing with the code proposed in the following thread: http://www.silverstripe.org/data-model-questions/show/256148

With the code proposed by Mo, you cannot make each SiteTree page to belong to another one, I don’t know why. Maybe the last post could help you.

If you want to relate, for example, Case Studies to other pages, you could use:

Page.php

class Page extends SiteTree {
	
	public static $db = array(
	);
	
	public static $many_many = array(
		'RelatedContent' => 'CaseStudyPage' 
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$rcTablefield = new ManyManyComplexTableField(
			$this,
			'RelatedContent',
			'CaseStudyPage',
			array(
				'Title' => 'CaseStudyPage'
			));
		// Remove the Add link, you can add new Case Studies  as CaseStudyPages
		$rcTablefield->setPermissions(array('show'));
		$fields->addFieldToTab('Root.Content.RelatedContent', $rcTablefield );
		return $fields;
	}
}

CaseStudyPage

class CaseStudyPage extends Page {
	
	public static $db = array(
	);
	
	public static $belongs_many_many = array(
		'BelongingPages' => 'Page' 
	);
	
}

class CaseStudyPage_Controller extends Page_Controller {
}

There’s something ugly about this, but it works.

Best regards,
Juan

Go to Top