Zauberfisch / silverstripe-easy-linkfield

SilverStripe inline link field that allows adding one or multiple links to any object and saves into a single DB field
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Allow extending of AbstractLink #4

Open silverstripesk opened 11 months ago

silverstripesk commented 11 months ago

Hi Zauberfisch,

currently it is not possible to extend AbstractLink with custom field - getCMSFields() method can't be updated, so it is not possible add new field.

I need to add Button style to every link type...

Zauberfisch commented 11 months ago

just to make sure I understand correctly, you want to create a AbstractLinkExtension with updateCMSFields() to change the CMS fields?

Hmm, supporting updateCMSFields would probably be a good idea. I haven't looked into it, but it's probably a bit more complicated because of how the fields are duplicated in the list. Might take some time until I fix this.


For now, as a work around you'll have to create a new class for each link type that you want to modify. It's not clean, but it works.

# /app/_config/extensions.yml
# this will replace the default link types "internal" and "external" with new classes
# or use "internalButton: 'app\InternalLink'" instead if you want to create a new type to use both the default internal link and the new one
zauberfisch\LinkField\LinkListField:
  link_types:
    internal: 'app\InternalLink'
    external: 'app\ExternalLink'
<?php

declare(strict_types=1);

namespace app;

class InternalLink extends \zauberfisch\LinkField\Link\InternalLink {
    public function getCMSFields(): FieldList {
        $fields = parent::getCMSFields();
        $fields->insertBefore(
            'NewTab',
            (new DropdownField('ButtonStyle', $this->fieldLabel('ButtonStyle'), [
              "red" => "Red Button",
              "green" => "Green Button",
            ]))->setEmptyString("Default Button")
        );
        return $fields;
    }
}
<?php

declare(strict_types=1);

namespace app;

class ExternalLink extends \zauberfisch\LinkField\Link\ExternalLink {
    public function getCMSFields(): FieldList {
        $fields = parent::getCMSFields();
        $fields->insertBefore(
            'NewTab',
            (new DropdownField('ButtonStyle', $this->fieldLabel('ButtonStyle'), [
              "red" => "Red Button",
              "green" => "Green Button",
            ]))->setEmptyString("Default Button")
        );
        return $fields;
    }
}
silverstripesk commented 11 months ago

yes, this is exactly what I need.

Just replacing method getCMSFields() with below one should be enough:

public function getCMSFields(): FieldList {
    $fields =  new FieldList([
        new FieldGroup([
                new TextField('Title', $this->fieldLabel('Title')),
                new CheckboxField('NewTab', $this->fieldLabel('NewTab')),
            ]),
        ]);
        $this->extend('updateCMSFields', $fields);
        return $fields;
    }

Extension:

class EasyLinkExtension extends DataExtension
{
    private static $fields = [
        'Style' => 'Varchar(64)'
    ];

    public function updateCMSFields(FieldList $fields)
    {
        $fields->insertBefore('NewTab', \SilverStripe\Forms\DropdownField::create('Style', 'Style', ['aaa'=>'bbb']));
    }
}
silverstripesk commented 11 months ago

ok, my code can't save/get data from the field, as the set and get methods are not ready for $fields added via extension. This needs more work in core of module.