Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » ModelAdmin search customization

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

Page: 1
Go to End
Author Topic: 1742 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    ModelAdmin search customization Link to this post

    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.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: ModelAdmin search customization Link to this post

    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

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: ModelAdmin search customization Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: ModelAdmin search customization Link to this post

    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

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: ModelAdmin search customization Link to this post

    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

    1742 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.