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

ModelAdmin search customization


Go to End


5 Posts   2849 Views

Avatar
micahsheets

Community Member, 165 Posts

26 June 2009 at 11:08am

If I have a managed model that I want to restrict which of the DataObjects in the model can be searched for. For example I have a DataObject Student that has_one school so that some of the students have school A and some have school B. I want the ModelAdmin that is managing Students to only be able to search for students that are in School A and not School B. I would think that I could have a hidden field in the SearchForm that has the School A statically defined. I don't know how to accomplish. I want the search to work as if the person searching has put School A in the School search form but I don't actually want the person to see the form or be able to put anything in it.

I hope this makes sense.

Avatar
Bambii7

Community Member, 254 Posts

10 July 2009 at 12:05pm

Edited: 10/07/2009 12:05pm

On the student dataobject there are a lot of parameters that might help to include. I'm not sure if it'll solve the issue but try adding

static $searchable_fields = array(
'school '
);

static $defaults = array(
"school " => 'A'
);

And then I think you'll need to over write the modeladmin search form with $this->fields->removeByName('school ');

I'm not that familiar with SilverStripe, let alone ModelAdmin, so this is guessing form what I've read so far

Avatar
Nicolaas

Forum Moderator, 224 Posts

28 July 2010 at 8:22pm

I just tried to override SearchForm like this:

class MyAdmin extends ModelAdmin{

function SearchForm() {
die("yyyyyy");
$form = parent::SearchForm();
}

}

But MyAdmin ran as normal. This seems very strange behaviour to me.

Avatar
swaiba

Forum Moderator, 1899 Posts

29 July 2010 at 12:49am

Edited: 29/07/2010 12:54am

shouldn't that be in the overridden ModelAdmin_CollectionController instead of the ModelAdmin class Nicolaas?

I have overridden the form myself, but I am just clueless on how the SearchContext stuff works. I wish it were as simple as the regular site search as that allows you to play with a DataObjectSet to add/remove as you wish... but I have no idea how to get passed the...

$context = singleton($this->modelClass)->getDefaultSearchContext();

...part. is there someone that could shed some light on this please? I have check the documentation and read the source, but the lights haven't come on yet!

Barry

Avatar
Nicolaas

Forum Moderator, 224 Posts

29 July 2010 at 12:54am

probably yes, because it sure did not work ....

But since then I have shifted my attention to DataObject::scaffoldSearchFields and this seems to be very useful.

Cheers

Nicolaas