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

Help: I can't get SortableGridField to work with many_many RelationEditor ?


Go to End


7 Posts   1523 Views

Avatar
neilcreagh

Community Member, 136 Posts

21 January 2015 at 12:08am

I'm trying to add drag and drop sorting to a GridField Relation Editor. It's a bookshop and on the homepage they just want to choose a few books from the collection to display:
(It's working perfectly without SortableGridField, but doesn't have drag and drop sorting obviously).

I have it set-up like so:

public static $many_many = array(
	'HomeBooks' => 'Book',
	);
	
	public static $many_many_extraFields=array(
        'HomeBooks'=>array(
            'SortOrder'=>'Int'
        )
    );

At the moment I have my grid field set-up like this and it's working fine (without GridFieldSortableRows)

	$gridFieldConfig2 = GridFieldConfig_RelationEditor::create();
	$gridFieldConfig2->removeComponentsByType('GridFieldAddNewButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldEditButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldSortableHeader');
	$gridFieldConfig2->removeComponentsByType('GridFieldFilterHeader');   
	$gridfield2 = new GridField("HomeBooks", "Featured Books", $this->HomeBooks()->sort("ID DESC"), $gridFieldConfig2);		
	$fields->addFieldToTab('Root.FeaturedTitles', $gridfield2);

But when I change it to this I get an Internal Server Error on the page:

	$gridFieldConfig2 = GridFieldConfig_RelationEditor::create();
	$gridFieldConfig2->addComponent(new GridFieldSortableRows('SortOrder'));
	$gridFieldConfig2->removeComponentsByType('GridFieldAddNewButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldEditButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldSortableHeader');
	$gridFieldConfig2->removeComponentsByType('GridFieldFilterHeader');   
	$gridfield2 = new GridField("HomeBooks", "Featured Books", $this->HomeBooks()->sort('SortOrder'), $gridFieldConfig2);		
	$fields->addFieldToTab('Root.FeaturedTitles', $gridfield2);

Any help would be greatly appreciated.

Avatar
UndefinedOffset

Community Member, 30 Posts

21 January 2015 at 4:05am

Ha you were right yes I was subscribed to that post... forgot I was lol. Do you have any error message or anything? You may need to turn on logging to capture it http://beta.doc.silverstripe.org/en/developer_guides/debugging/error_handling/. It looks like you have configured the grid field but something else maybe happening. Also make sure you're either running the latest master or latest release (0.4.1).

Avatar
neilcreagh

Community Member, 136 Posts

21 January 2015 at 4:51am

Hey thanks for getting back to me (I'm glad my little ploy worked)
Ok I've attached the error message here...

Attached Files
Avatar
UndefinedOffset

Community Member, 30 Posts

21 January 2015 at 5:01am

Edited: 21/01/2015 5:01am

Ah I see the problem here, thanks to a limitation in the orm the sort method won't allow for table/relationship names. My recommendation is you change the name of the sort index column since in this instance the database query is joining against the Book table which I'm guessing also has a column called "SortOrder". The easy solution is to change the name of the Column in the many_many_extraFields to be something different and adjust your sort and GridFieldSortableRows call to reflect this change.

Avatar
neilcreagh

Community Member, 136 Posts

21 January 2015 at 5:18am

Edited: 21/01/2015 5:19am

No joy I'm afraid... Here's the changes I made and the new error log is attached. Any other ideas?

	public static $many_many_extraFields=array(
        'HomeBooks'=>array(
            'HomeOrder'=>'Int'
        )
    );
	

	$gridFieldConfig2 = GridFieldConfig_RelationEditor::create();
	$gridFieldConfig2->addComponent(new GridFieldSortableRows('HomeOrder'));
	$gridFieldConfig2->removeComponentsByType('GridFieldAddNewButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldEditButton');
	$gridFieldConfig2->removeComponentsByType('GridFieldSortableHeader');
	$gridFieldConfig2->removeComponentsByType('GridFieldFilterHeader');   
	$gridfield2 = new GridField("HomeBooks", "Featured Books", $this->HomeBooks()->sort('HomeOrder'), $gridFieldConfig2);		
	$fields->addFieldToTab('Root.FeaturedTitles', $gridfield2);

ps. I did dev/build and flush and I can see that the HomeOrder column was correctly inserted into the table

Attached Files
Avatar
UndefinedOffset

Community Member, 30 Posts

21 January 2015 at 5:52am

Was it also added to the Book table, or in your Page table? If it was do you have it defined on the book class or page class? Also what version of SilverStripe are you using 3.1? 3.0? master?

Avatar
neilcreagh

Community Member, 136 Posts

22 January 2015 at 3:00am

Got it! Yes it was also in the Book table - I stupidly picked HomeOrder which I had already tried on the Book page. Your initial recommendation was right: Changing the name of the Column in the many_many_extraFields to be something different from 'SortOrder' (and adjusting the sort and GridFieldSortableRows call to reflect this change).
Thank you so much for your help!!