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 make a relation to pages in custom order?


Go to End


5 Posts   621 Views

Avatar
ian-wig

Community Member, 3 Posts

28 May 2013 at 4:29am

Edited: 28/05/2013 4:30am

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

Community Member, 254 Posts

28 May 2013 at 1:10pm

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

Community Member, 3 Posts

28 May 2013 at 5:57pm

Edited: 28/05/2013 6:27pm

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

Community Member, 254 Posts

29 May 2013 at 3:19pm

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

Avatar
ian-wig

Community Member, 3 Posts

30 May 2013 at 6:55am

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?