arillo/silverstripe-multiselectfield

A sortable multiple select field for managing many-to-many relations

Installs: 1 151

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 4

Forks: 2

Open Issues: 1

Type:silverstripe-vendormodule

1.0.7 2024-02-09 21:18 UTC

This package is auto-updated.

Last update: 2024-11-09 22:48:11 UTC


README

A drag & drop sortable select field for managing many-to-many relations.

Inspired by https://github.com/kinglozzer/silverstripe-multiselectfield which breaks in SilverStripe 4.12+

field

Installation:

Composer:

$ composer require arillo/silverstripe-multiselectfield

Download:

Simply clone or download this repository and put it in a folder called 'multiselectfield' in your SilverStripe installation folder, then run dev/build.

Example:

The field currently only supports many-to-many relations. The constructor requires an argument for a field to sort on, which you need to define in your $many_many_extraFields.

The following is an example of how to use MultiSelectField in a simple many-to-many relationship with a "Sort" extra field:

use Arillo\MultiSelectField\MultiSelectField;

class Department extends DataObject
{
    private static $db = [
        'Name' => 'Varchar',
    ];

    private static $many_many = [
        'StaffMembers' => 'StaffMember',
    ];

    private static $many_many_extraFields = [
        'StaffMembers' => [
            'Sort' => 'Int',
        ],
    ];

    /**
     * @return FieldList
     */
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $staffField = MultiSelectField::create(
            'StaffMembers',
            'Staff members',
            $this,
            'Sort'
        );
        $fields->addFieldToTab('Root.Main', $staffField);

        return $fields;
    }
}
class StaffMember extends DataObject
{
    private static $db = [
        'Name' => 'Varchar',
    ];

    private static $many_many = [
        'Departments' => 'Department',
    ];
}