Reputation: 27
In a Silverstripe (version 3) model admin, how can I get the collection of fields from a different model so as to add them to this model's admin?
I have tried this using FieldList::addFieldsToTab:
$loremIpsumTab = Tab::create('LoremIpsum');
$fields->fieldByName('Root')->insertAfter('Main', $loremIpsumTab);
$loremIpsumFields = (
$this->LoremIpsum()->getCMSFields()
->fieldByName('Root.Main')->Fields());
$fields->addFieldsToTab('Root.LoremIpsum', $loremIpsumFields);
That creates the tab correctly, but moves the fields incorrectly: all the fields from 'Root.Main' are moved, not only those for the LoremIpsum model.
I had assumed this would interrogate the related LoremIpsum model for its CMS fields:
$this->LoremIpsum()->getCMSFields()
->fieldByName('Root.Main')->Fields()
So how can I move only those fields for the LoremIpsum model?
Upvotes: 1
Views: 107
Reputation: 4626
Hello and welcome to StackOverflow. What do you want to acchieve?
It seems you want to edit a has_one relation dataobject from your other dataobject. There are ready-to-use and tested modules for this scenario, e.g. https://github.com/stevie-mayhew/hasoneedit/tree/3.x , cause even if you manage to display the fields, SilverStripe assumes those values belong to the current model and not to a relation. Then you'll have extra work to save it back etc...
Some fields in your current model and in the LoremIpsum model have the same name, e.g. ID, Title, Created. This causes problems in your code above, cause you can only have one Field for e.g. ID in a Form.
The "hasoneedit" module overcomes this by prefixing the relation's fields.
Upvotes: 1