7912 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » [solved] Select related page via dropdown (in popup)
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 2219 Views |
-
[solved] Select related page via dropdown (in popup)

10 January 2010 at 3:32am Last edited: 11 January 2010 2:26am
Hi again, and good day to everybody.
I'm trying to create a "Friends" or "Affiliates" section for a website, which is meant to store a number of entries divided into categories. I'm trying to find a clean and user-friendly way to do this with SS, since the basic model will repeatedly come to use in my next projects.
Coming from this thread
http://silverstripe.org/dataobjectmanager-module-forum/show/276517#post276517,i decided to try a different approach, because i was not happy with the way the data was being presented in the admin section. So rather than associating all the "Friends" with one Friend Page Type, and using Category Data Objects, i created several Friend Category Pages inside a Friend Holder, each storing their asssociated Friend Entries.
This gives me an uncluttered Admin section and allows for Drag & Drop Reordering of the Friends in each Category.
What i'm trying to achieve now is a way to, when creating or editing a Friend, select the Category Page Relationship for the Dataobject from a Dropdown field, which would allow to reassociate existing friends to a different Category later on.
Everything is working fine, the Dropdown shows a list of all existing Friend Category Pages, but when saving, the Relationship will not be written to the database. Also, when editing an existing Friend, the Dropdown does not show the correct Page when the Popup opens.
Here's my code:
### FreundeKategorie.php
<?php
class FreundeKategorie extends Page {
static $has_many = array (
'Freunde' => 'Freund'
);public function getCMSFields() {
$f = parent::getCMSFields();
$manager = new DataObjectManager(
$this, // Controller
'Freunde', // Source name
'Freund', // Source class
array('Name' => 'Name','Url' => 'Url'),
'getCMSFields_forPopup'
);
$f->addFieldToTab("Root.Content.Freunde",$manager);
return $f;
}
}class FreundeKategorie_Controller extends Page_Controller {
}
### Freund.php
<?php
class Freund extends DataObject {
static $db = array (
'Name' => 'Text',
'Url' => 'Text',
);static $has_one = array (
'Kategorie' => 'FreundeKategorie',
);function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push( new TextField( 'Name', 'Name' ) );
$fields->push( new TextField( 'Url', 'Url (http:// am Anfang nicht vergessen!)' ) );
// build dropdown menu for category (related page) selection
$oData = (DataObject::get('FreundeKategorie'));
if ($oData) $CategoriesSource = $oData->toDropDownMap('ID','Title');
$dropdown = new DropdownField('Kategorie', 'Kategorie', $CategoriesSource);
$fields->push ( $dropdown );
return $fields;
}}
Am i on the wrong track here? I am sorry if this question seems really basic, i'm just starting to get my around the way things work in SS.
Any help would be greatly appreciated!
All the best,
Andi -
Re: [solved] Select related page via dropdown (in popup)

10 January 2010 at 5:21am
Following the Instructions in this thread
http://www.silverstripe.org/archive/show/220502
i tried to replace
$dropdown = new DropdownField('Kategorie', 'Kategorie', $CategoriesSource);
with$dropdown = new DropdownField('KategorieID', 'Kategorie', $CategoriesSource);
but that just makes the dropdown field disappear altogether.
This thread
http://www.silverstripe.org/archive/show/139192also seems to deal with a similar issue, but i can't get pastie to show me the code..
Where am i wrong here. anybody have an idea?
-
Re: [solved] Select related page via dropdown (in popup)

10 January 2010 at 1:53pm
I quickly scanned through your posts, so I may be off, here, but it seems to me you're going about this the wrong way. Your field is disappearing because KategorieID already exists in your form, as a HiddenField. That hidden field is really important because it tells the controller how to relate the dataobject to its has_one relation (Kategorie)..
So you can't have it both ways.. you can't manage a dataobject on a given parent object and use that DOM to assign a record to a different parent.
Well, now that I think about it.. I take that back..
You MAY get away with it if you first do $fields->removeByName('KategorieID') and then add it back in as a dropdown. I don't exactly recall the sequence in which those extra hiddenfields are added, but give it a try... My gut tells me they're added at the very end, after your getCMSFields() function is called.
-
Re: [solved] Select related page via dropdown (in popup)

11 January 2010 at 2:15am Last edited: 11 January 2010 2:34am
Hi Uncle Cheese,
thanks again very much for helping me.. I tried$fields->removeByName('KategorieID');
then adding the dropdown, which didn't work. I also tried
$fields->replaceField('KategorieID', $dropdown );
but that also didn't do anything.
What seems to work though is to create a new Dropdown, and then replacing KategorieID onBeforeWrite like so:
// build dropdown menu for category (parent page) selection
$oData = (DataObject::get('FreundeKategorie'));
if ($oData) $CategoriesSource = $oData->toDropDownMap('ID','Title');
$dropdown = new DropdownField('NewKategorieID', 'Kategorie', $CategoriesSource, $this->KategorieID);
$fields->push ( $dropdown );then add the onBeforeWrite function like so:
public function onBeforeWrite() {
$this->KategorieID = $this->NewKategorieID;
parent::onBeforeWrite();
}I'm testing this now, but everything seems to be working okay. I can add a "Freund" DataObject on any "FreundeKategorie" Page, and dropdown select a Category to push it into another Page of the same type..
Thanks a bunch for pointing me in the right direction!
EDIT:
I just realized that i have to add an if clause in onBeforeWrite to see if the NewKategorieID is set, otherwise i will loose the relationship when onBeforeWrite is called on Drag&Drop reorderingpublic function onBeforeWrite() {
if ($this->NewKategorieID) {
$this->KategorieID = $this->NewKategorieID;
}
parent::onBeforeWrite();
}
| 2219 Views | ||
|
Page:
1
|
Go to Top |

