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.

General Questions

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

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

how to make a relation to pages in custom order?


Reply

5 Posts   290 Views

Avatar
ian-wig

28 May 2013 at 4:29am (Last edited: 28 May 2013 4:30am), Community Member, 3 Posts

I have got several "Project"-Pages and want to show some of them in a custom order on my "Home"-Page.

Therefore I use the SortableGridField-Module in combination with the GridFieldConfig_RelationEditor:

Home-Class

public static $has_many = array(
'Highlights' => 'Project'
);

function getCMSFields()
{
$fields = parent::getCMSFields();
$conf=GridFieldConfig_RelationEditor::create();
$conf->addComponent(new GridFieldSortableRows('HighlightSortOrder'));
$gridField = new GridField('Highlights', 'Highlight Projects', $this->Highlights(), $conf);
$fields->addFieldToTab("Root.Main", $gridField);

return $fields;
}

Project-Class

public static $db = array(
'HighlightSortOrder'=>'Int'
);

public static $default_sort = 'HighlightSortOrder';

It seems to work sometimes, but just after using the preview-Button in the admin-area.
There is also the problem, that all "Project"-Pages which are linked as Highlights, are now Subpages of the "Homepage" in the Sitetree.

Have you got an idea how to fix these problems or is there a better approach?

Thanks!

Avatar
Bambii7

28 May 2013 at 1:10pm Community Member, 254 Posts

Hi ian-wig
try putting the reverse relationship on the project class

public static $db = array(
'HighlightSortOrder'=>'Int'
);
public static $has_one = array('Home' => 'HomeClass');

public static $default_sort = 'HighlightSortOrder';

Avatar
ian-wig

28 May 2013 at 5:57pm (Last edited: 28 May 2013 6:27pm), Community Member, 3 Posts

Hi Bambii7,

thank you very much, that fixes the subpage-problem.

Unfortunately the template still doesn't show up the Highlights on the Homepage (even when I flushed):

<% loop $Highlights %>
...
<% end_loop %>


They appear when I click on Preview from the administration, but as soon as I log off, they disappear again.

Have you got an idea?

Avatar
Bambii7

29 May 2013 at 3:19pm Community Member, 254 Posts

Hmmm have you checked the permissions on the ProjectClass? Perhaps they are set to be visible only to logged in users?

Avatar
ian-wig

30 May 2013 at 6:55am Community Member, 3 Posts

Thanks again, but there are no special permissions used.

I figured out, that the Project always shows up in the Highlights, when the Project-Page is published after the relation was made in the GridField of the HomePage. The database relation between Home and Project (Home-Id) is only set in the Project-Table and not in the Project_Live-Table, which causes that strange behaviour, I guess.
I hope you undestand what the problem is ;)

Any solutions for publishing the grid-relation-change directly to Project-Table and Project_Live-Table?