elliot-sawyer / silverstripe-linkfield

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

AllowedTypes #1

Closed simonwinter closed 3 years ago

simonwinter commented 6 years ago

Hi there,

When I was using https://github.com/sheadawson/silverstripe-linkable, there's an option to set allowed types per LinkField instance:

LinkField::create('ExampleLinkID', 'Link Title')->setAllowedTypes(array('URL','Phone'))

I see that https://github.com/gorriecoe/silverstripe-link does have an option to setAllowedTypes on the Link model.

What's the best way of using this functionality with this module? As an example, I want to limit users to only create SiteTree or URL links. I had to create a new class that extends Link with the allowed types set like so:

private static $allowed_types = [
        'SiteTree',
        'URL'
    ];

Ideally, I'd like to just pass the option to LinkField rather than extending Link each time ... am I doing something wrong here?

gorriecoe commented 6 years ago

Hey @simonwinter,

At this stage because linkfield is extending gridfield the only ways to limit types is via globally via yaml or to create a sub class as below:

<?php

use gorriecoe\Link\Models\Link;

class MyLimitedLink extends Link
{
    private static $allowed_types = [
        'SiteTree',
        'URL'
    ];
}

Add the relation to the sub class

<?php

class Page extends Sitetree
{
  private static $has_one = [
    'MyLink' => MyLimitedLink::class
  ];
}

Alternatively the reason the linkfield is separated from link is to give the opportunity for others to develop their own relationship modules. So there is opportunity for a simple port from linkable. It should be relatively straigt forward for someone to make a port and I will happily add it to suggested modules for link

simonwinter commented 6 years ago

Hi @gorriecoe,

Great work by the way.

Cool - extending Link is exactly what I've done, so all good. I think having the 2 modules separated is great & agree with your reasoning there.

It's simple enough to extend the class, though I can see a situation where a site has many uses of Link, & having to create a new class each time just to limit the available types could be a little frustrating.

As you say, it seems like something that can be added easily enough. If I get inspired, I may do just that!

Thanks, Simon

GuySartorelli commented 3 years ago

@elliot-sawyer This is addressed by #24. Since that has been merged in, this issue can be closed.