fromholdio/silverstripe-minigridfield

Installs: 2 588

Dependents: 2

Suggesters: 1

Security: 0

Stars: 5

Watchers: 3

Forks: 3

Open Issues: 0

Type:silverstripe-vendormodule

1.7.0 2024-07-22 05:37 UTC

This package is auto-updated.

Last update: 2024-09-22 06:05:17 UTC


README

A field that preconfigures and gently themes a GridField to result in a "mini" inline grid field.

Two field types are included:

  • MiniGridField
  • HasOneMiniGridField - use this to manage an object in a has_one relation

Auto-magically handles (preconfigures, with a few flags available to devs) orderable rows, add new multi class, versioning.

Requirements

Installation

composer require fromholdio/silverstripe-minigridfield

Screenshots

Empty

MiniGrid

MiniGrid limited

HasOneMiniGrid

Complete Example

This is an example showing a has one, has many and many many:

use Fromholdio\MiniGridField\Forms\HasOneMiniGridField;
use Fromholdio\MiniGridField\Forms\MiniGridField;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\ORM\DataObject;

class TestAdmin extends ModelAdmin {
    private static $menu_title = 'Test';
    private static $url_segment = 'test';
    private static $managed_models = [
        Radio::class,
        Owner::class,
        Presenter::class,
        Listener::class,
    ];
}

class Radio extends DataObject {
    private static $db = ['Title' => 'Varchar'];
    private static $has_one = ['Owner' => Owner::class];
    private static $has_many = ['Presenters' => Presenter::class];
    private static $many_many = ['Listeners' => Listener::class];
    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->replaceField('OwnerID', HasOneMiniGridField ::create('Owner', 'Owner', $this));

        $fields->removeByName('Presenters');
        $fields->insertAfter('Title', MiniGridField::create('Presenters', 'Presenters', $this));

        $fields->removeByName('Listeners');
        $fields->insertAfter('Title', MiniGridField::create('Listeners', 'Listeners', $this));

        return $fields;
    }
}

class Owner extends DataObject {
    private static $db = ['Title' => 'Varchar'];
    private static $has_many = ['Radio' => Radio::class];
}

class Presenter extends DataObject {
    private static $db = ['Title' => 'Varchar'];
    private static $has_one = ['Radio' => Radio::class];
}

class Listener extends DataObject {
    private static $db = ['Title' => 'Varchar'];
    private static $belongs_many_many = ['Radios' => Radio::class];
}

Replacing GridField universally

For projects that you'd like to replace all GridFields with MiniGridField, you can use the Injector to do so in a few lines of config:

SilverStripe\Core\Injector\Injector:
  SilverStripe\Forms\GridField\GridField:
    class: Fromholdio\MiniGridField\Forms\MiniGridField

Thanks & Acknowledgements

There are a few more, can't find them right now, will list them here soon. I've worked hard on getting this working how I want it, but took direction from some of the wonderful peeps and their code above, thanks.