silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
722 stars 820 forks source link

Describe File->getCMSFields() API changes in 4.x #8051

Closed chillu closed 6 years ago

chillu commented 6 years ago

File->getCMSFields() can no longer be extended, you need FileFormFactory->updateFormFields(). Describe this change please:

Pull Requests

robbieaverill commented 6 years ago

Related: https://github.com/silverstripe/silverstripe-subsites/issues/296

At a slightly higher level, this also applies to any DataObject that would or could be used by the React form schema. User modifications to a getCMSFields() method that is rendered normally in the CMS via PHP would not be reflected in React form schema examples. This would be an issue for anyone who wanted to develop some module like a "React ModelAdmin" or something.

chillu commented 6 years ago

This is what the original reporter needed to do to get it working. Since it's not exactly trivial, it should be in the docs.

## app.yml

SilverStripe\Assets\File:
  extensions:
    - QI\Homes\Extensions\HomeFileExtension
SilverStripe\AssetAdmin\Forms\FileFormFactory:
  extensions:
    - QI\Homes\Extensions\HomeAssetFormFactoryExtension

## HomeFileExtension.php

<?php

namespace QI\Homes\Extensions;

use SilverStripe\ORM\DataExtension;

class HomeFileExtension extends DataExtension
{
    private static $db = [
        'Description' => 'Text',
    ];
}

## HomeAssetFormFactoryExtension.php

<?php

namespace QI\Homes\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextareaField;

class HomeAssetFormFactoryExtension extends Extension
{
    public function updateFormFields(FieldList $fields, $controller, $formName, $context)
    {
        $fields->insertAfter(
            'Title',
            TextareaField::create('Description', 'Description')
        );
    }
}