简体   繁体   中英

Unable to customize filter/ search form of ModelAdmin in SilverStripe 4.4.4

I am working on a SilverStripe project. Basically, I updated my project to SilverStripe version 4.4.4. After the upgrade, I found out that the search/ filter forms of the ModelAdmin were changed as in the screenshot below.

在此处输入图像描述

What I am trying to do now is that I am trying to customize the fields of the search/ filter form of the ModelAdmin following this lesson. https://www.silverstripe.org/learn/lessons/v4/introduction-to-modeladmin-1 .

I have a data object class that is linked to a model admin class. Following is the dummy code of the model admin class.

class EnquirySubmission extends DataObject
{
    private static $db = [
        //some hidden fields are here
    ];

    private static $has_one = [
        'Member' => Member::class
    ];

    private static $summary_fields = [
        'Member.Name'   => 'Member',
        //some hidden fields are here
    ];

    //some hidden code goes here

    public function searchableFields()
    {
        return [
            'Member.Name' => [
                'filter' => 'PartialMatchFilter',
                'title' => 'Member',
                'field' => \SilverStripe\Forms\DropdownField::create('Member.Name')
                 ->setSource(
                        Member::get()->map('ID','Email')
                    )->setEmptyString('-- Member --')
            ],
        ];
    }
}

As you can see in the code, I am customizing the filter/ search form by overriding the searchableFields method. But it does not work in the upgraded version of SilverStripe. What am I missing and how can I fix it?

Silverstripe and ModelAdmin are ace, but it is confusing why this date range search issue has required a tweak in every version so far. This is a complete example that I've just got working on 4.7.2 (latest stable at time of post)...

app/src/Test/MyDataObject.php

namespace MyVendor\MyNamespace;

use SilverStripe\Forms\DateField;
use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject {

    private static $db = [
        'Title'           => 'Varchar',
        'MyDateTimeField' => 'DBDatetime'
    ];

    private static $summary_fields = ['Title','MyDateTimeField'];

    public function updateAdminSearchFields($fields) {
        $fields->removeByName('MyDateTimeField');//needed as added in summary field
        $fields->push(DateField::create('MyDateTimeField:GreaterThanOrEqual', 'MyDateTimeField (Start)'));
        $fields->push(DateField::create('MyDateTimeField:LessThanOrEqual', 'MyDateTimeField (End)'));
    }
}

app/src/Test/MyAdmin.php

namespace MyVendor\MyNamespace;

use SilverStripe\Admin\ModelAdmin;

class MyAdmin extends ModelAdmin {

    private static $menu_title = 'MyAdmin';
    private static $url_segment = 'myadmin';
    private static $managed_models = [MyDataObject::class];

    public function getList() {
        $list = parent::getList();
        if ($params = $this->getRequest()->requestVar('filter'))
            if ($filters = $params[$this->sanitiseClassName($this->modelClass)])
                return $list->filter($filters);

        return $list;
    }
}

app/src/Test/MyAdminExtension.php

namespace MyVendor\MyNamespace;

use SilverStripe\ORM\DataExtension;

class MyAdminExtension extends DataExtension {
    public function updateSearchContext($context) {
        $class = $context->getQuery([])->dataClass();
        if (method_exists($class, 'updateAdminSearchFields'))
            (new $class)->updateAdminSearchFields($context->getFields());

        return $context;
    }
}

app/_config/mysite.yml

MyVendor\MyNamespace\MyAdmin:
  extensions:
    - MyVendor\MyNamespace\MyAdminExtension

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM