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.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Solved] 2 managed_models from the same dataobject in modeladmin


Go to End


8 Posts   1406 Views

Avatar
mi32dogs

Community Member, 75 Posts

31 January 2017 at 1:44pm

Ok I have created a dataobject where you can import a Ticket report, and it will spit out a filtered an formatted XLXS report according to the clients wishes. The filtering of the record is done by hidden them and not deleting them.

Now here is what I would like to have; one modeladmin with 2 tabs, one that shows all the current records and one that shows all the hidden records.
Is this possible? I know I can make 2 different modeladmins but I would like to do it in one if possible.

Here is my modeladmin:

class TicketReportAdmin extends ModelAdmin
{

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

    private static $url_segment = 'TicketReport';
    private static $menu_priority = 99;

    private static $menu_icon = 'images/menu-icons/star.png';
    private static $awesome_icon = "fa-list-ol";

    private static $menu_title = 'Ticket Report';

    public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);

        //This check is simply to ensure you are on the managed model you want adjust accordingly
        if ($this->modelClass == 'TicketReport' && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
            if ($gridField instanceof GridField) {
                $gridField->setTitle('Ticket Report');
                $gridField->getConfig()->getComponentByType('GridFieldPaginator')->setItemsPerPage(250);
                $gridField->getConfig()->addComponent(new GridFieldExportButton2('before'));
                $gridField->getConfig()->addComponent(new GridFieldDeleteAllButton('before'));
            }
        }
        return $form;
    }

}

Avatar
martimiz

Forum Moderator, 1391 Posts

8 February 2017 at 10:54am

I don't think you can use the same modelclass twice in ModelAdmin out of the box. it should be possible to extend ModelAdmin, but it would require some heavy tweaking.

Just thinking - maybe you could try to define a class HiddenTicketReport extends TicketReport, basically the same class, just with a different name. That would allow you to have two modelclasses with different titles on tabs, buttons and stuff (using singularname/pluralname and such).

Then you'd still have to define the default selection for both classes, but you would have to anyway. Don't know how this would interfere with relations you may have defined, but I just thought I'd mention the idea - who knows... :)

Avatar
mi32dogs

Community Member, 75 Posts

14 February 2017 at 9:56am

Thanks Martimiz, this works perfectly, Great solution

Avatar
mi32dogs

Community Member, 75 Posts

16 February 2017 at 5:41pm

Just for anybody that is looking for the same thing, I was runnning into so problems with releations, so i found a new solution on stackoverflow.

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.

so far this is working well the only downside is that it is loading all the gridfield tabs on initial load so it is a little slow.

Avatar
martimiz

Forum Moderator, 1391 Posts

16 February 2017 at 11:35pm

Great that you found a better way, which probably is the better one anyway :).

Just wondering where my dubious idea went wrong, as it seemed to work ... :)

Avatar
mi32dogs

Community Member, 75 Posts

17 February 2017 at 5:59am

It all was working fine, except for sorting $has_one columns, it will give you a dbconnection error, and I think this is because you have to override the getList function to have the HiddenTicketReport class to show TicketReport class data.

Avatar
martimiz

Forum Moderator, 1391 Posts

17 February 2017 at 6:12am

Ah... Guess there are no shortcuts :)

Avatar
mi32dogs

Community Member, 75 Posts

17 February 2017 at 6:15am

if there where no relations on the database it would have worked perfectly, I give it a A for effort