elliot-sawyer / silverstripe-linkfield

Adds a Linkfield for gorriecoe/silverstripe-link
BSD 3-Clause "New" or "Revised" License
9 stars 25 forks source link

Update hasonefield to 3.0.4 #15

Closed elliot-sawyer closed 4 years ago

elliot-sawyer commented 4 years ago

Tested with reference project, 4.5.1. Page.php:


namespace {

    use gorriecoe\Link\Models\Link;
    use gorriecoe\LinkField\LinkField;
    use SilverStripe\CMS\Model\SiteTree;

    class Page extends SiteTree
    {
        private static $db = [];

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

        private static $many_many = [
            'ManyLinks' => Link::class
        ];

        private static $many_many_extraFields = [
            'ManyLinks' => [
                'Sort' => 'Int' // Required for all many_many relationships
            ]
        ];

        public function getCMSFields()
        {
            $fields = parent::getCMSFields();

            $fields->addFieldsToTab(
                'Root.Links',
                [
                    LinkField::create(
                        'TestLink',
                        'One link',
                        $this
                    ),

                    LinkField::create(
                        'ManyLinks',
                        'Many links',
                        $this
                    )
                ]
            );

            return $fields;
        }
    }
}

mysite.yml:

gorriecoe\Link\Models\Link:
  belongs_many_many:
    Page : Page.ManyLinks
gorriecoe commented 4 years ago

I'm happy if you want merge this, as I'm confident your test should prove it works.