Scaffold ListBox multiple select in ModelAdmin Filter for DataObject with Enum

Currently, automatic scaffolding for search fields where there is an enumeration causes a dropout, allowing one choice to be made. I am interested in using existing filters to modify this to allow multiple options.

Given the following data object ...

class MyDataObject extends DataObject {
    static $db = array(
        'Name'      => "Varchar(255)",
        'MyEnum'    => "Enum('Option1,Option2,Option3','Option1')"
    );  
}

... and the next ModelAdmin ...

class MyModelAdmin extends ModelAdmin {
    static $mangaged_models = array(
        'MyDataObject',
    );  
    static $url_segment = 'mymodeladmin';
    static $menu_title = 'MyModelAdmin';
    static $menu_priority = 9;
}

... I'm looking for a module or a simple filter of some kind to put Enum in a multiple select list

a multiple choice list is defined as ...

  • Allows multiple selections
  • After entering some characters, sentences are offered.

- , . - (ExactMatchMultiFilter , , , ), , - , , .

class MyDataObject extends DataObject {
    static $db = array(
        'Name'      => "Varchar(255)",
        'MyEnum'    => "Enum('Option1,Option2,Option3','Option1')"
    );
    public static $searchable_fields = array (
        'MyEnum'    => array('filter' => 'ExactMatchMultiFilter')
    );
}

.

+4
1

, , , filter, , . , , , . , field, , , .

ListboxField , , .

, , CheckboxSetField. ( , , ModelAdmin)

:

class MyDataObject extends DataObject {
    static $db = array(
        'Name'      => "Varchar(255)",
        'MyEnum'    => "Enum('Option1,Option2,Option3','Option1')"
    );
    public static $searchable_fields = array (
        'MyEnum'    => array('field' => 'CheckboxSetField')
    );
}

, - , , , " ". , SilverStripe -, field, .

, , , . ModelAdmin, CheckboxSetField Enum.

class MyModelAdminExtension extends Extension {
    public function updateSearchForm($form) {

        $modelClass = $form->getController()->modelClass;

        foreach ($form->Fields() as $field) {
            if ($field->class == 'CheckboxSetField') {
                //We need to remove the "q[]" around the field name set by ModelAdmin
                $fieldName = substr($field->getName(), 2, -1);
                $dbObj = singleton($modelClass)->dbObject($fieldName);
                if ($dbObj->class == 'Enum') {
                    $enumValues = $dbObj->enumValues();
                    $field->setSource($enumValues);
                }
            }
        }
    }
}

ModelAdmin, Enum, CheckboxSetField, .

, ListboxField, ( , ). , ListboxField, Enum, , , .

class MyModelAdminExtension extends Extension {
    public function updateSearchForm($form) {

        $modelClass = $form->getController()->modelClass;

        foreach ($form->Fields() as $field) {
            if ($field->class == 'ListboxField') {
                //We need to remove the "q[]" around the field name set by ModelAdmin
                $fieldName = substr($field->getName(), 2, -1);
                $dbObj = singleton($modelClass)->dbObject($fieldName);
                if ($dbObj->class == 'Enum') {
                    $field->setMultiple(true);

                    $enumValues = $dbObj->enumValues();
                    $field->setSource($enumValues);
                }
            }
        }
    }
}

...

class MyDataObject extends DataObject {

    private static $db = array(
        'Name' => "Varchar(255)",
        'MyEnum' => "Enum('Option1,Option2,Option3','Option1')"
    );

    public static $searchable_fields = array (
        'MyEnum' => array('field' => 'ListboxField')
    );
}

, - ListboxField Enum.

, CheckboxSetField? , , . , , CheckboxSetField, , , ListboxField.


, , Enum HasOne. ModelAdmin, ( dbObject) . , ( relObject).

updateSearchForm :

public function updateSearchForm($form) {

    $modelClass = $form->getController()->modelClass;

    foreach ($form->Fields() as $field) {
        if ($field->class == 'ListboxField') {
            //We need to remove the "q[]" around the field name set by Model Admin
            $fieldName = substr($field->getName(), 2, -1);
            $dbObj = null;

            //Check if the field name represents a value across a relationship
            if (strpos($fieldName, '__') !== false) {
                //To use "relObject", we need dot-syntax
                $fieldName = str_replace('__', '.', $fieldName);
                $dbObj = singleton($modelClass)->relObject($fieldName);
            }
            else {
                $dbObj = singleton($modelClass)->dbObject($fieldName);
            }

            if ($dbObj != null && $dbObj->class == 'Enum') {
                $field->setMultiple(true);

                $enumValues = $dbObj->enumValues();
                $field->setSource($enumValues);
            }
        }
    }
}
+3

All Articles