3

I'd like to manage different filtered lists of the same DataObject in ModelAdmin. I have the DataObject "Message" which has a SentbyID and a SenttoID. In ModelAdmin I want to manage two lists. One list with all messages with a certain SentbyID and one list with messages with a certain SenttoID. Can I manage this two lists in different tabs, f.e. "Inbox" and "Outbox"? How can I do this?

I have this so far:

class MessageAdmin extends ModelAdmin {

    private static $menu_title = 'Messages';

    private static $url_segment = 'messages';

    private static $managed_models = array (
        'Message'
    );

    public function getList() {
        $currMember = Member::currentUserID();
        $list = Message::get()->filter(array('SenttoID' => $currMember));
        return $list;
    }

}
iraira
  • 315
  • 2
  • 13

2 Answers2

2

Overload getEditForm and define a new FieldList containing a TabSet. The SilverStripe Comments module provides a great example this in action, by showing different types of comments (Spam vs. Moderated) in separate tabs within the same ModelAdmin.

Have a look at https://github.com/silverstripe/silverstripe-comments/blob/2.1/code/admin/CommentAdmin.php

wmk
  • 4,598
  • 1
  • 20
  • 37
cryptopay
  • 1,084
  • 6
  • 7
  • Thank you! This is exactly what I was searching for. Works perfectly! – iraira Dec 22 '15 at 15:40
  • Comments doesn't use ModelAdmin but it's own subclass of LeftAndMain. If you don't need much ModelAdmin magic it's a good way to go. – wmk Jul 21 '17 at 09:32
1

You have to handle it through two distinct ModelAdmin, for example renaming MessageAdmin in ReceivedMessageAdmin, and creating a brand new SentMessageAdmin like the following:

class SentMessageAdmin extends ModelAdmin {

    private static $menu_title = 'Sent Messages';

    private static $url_segment = 'sent-messages';

    private static $managed_models = array (
        'Message'
    );

    public function getList() {
        $currMember = Member::currentUserID();
        $list = Message::get()->filter(array('SentbyID' => $currMember));
        return $list;
    }

}
g4b0
  • 929
  • 1
  • 8
  • 21
  • So there is no way to manage it in one ModelAdmin? I hoped to avoid too many menu items in the CMS left sidebar... – iraira Dec 17 '15 at 15:35
  • Maybe you can extend ModelAdmin functionality, but I think that it will be an overkill task... – g4b0 Dec 21 '15 at 09:41
  • Thank you for you support! I managed it with the Comments module example below. It was not possible with ModelAdmin but with extending LeftAndMain and a custom getEditForm. – iraira Dec 22 '15 at 15:43