Adding custom field to file form

Silverstripe 5

I have followed the docs exactly and still can’t seem to get my custom field to show up. The field is being added to the Files table but the FileFormFactoryExtension is not being hit. Here is what I have:

extensions.yml

SilverStripe\Assets\File:
  extensions:
    - InnisMaggiore\Documents\Extensions\FileExtension
    - SilverStripe\TextExtraction\Extension\FileTextExtractable
SilverStripe\AssetAdmin\Forms\FileFormFactory:
  extensions:
    -InnisMaggiore\Documents\Extensions\FileFormFactoryExtension

FileExtension.php

namespace InnisMaggiore\Documents\Extensions;

use SilverStripe\ORM\DataExtension;

class FileExtension extends DataExtension
{
    private static $db = [
        'isRFP' => 'Boolean',
    ];

}

FileFormFactoryExtension.php

namespace InnisMaggiore\Documents\Extensions;


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


class FileFormFactoryExtension extends Extension
{
    public function updateFormFields(FieldList $fields)
    {

        $fields->insertAfter(
            'Title',
            CheckboxField::create('isRFP', 'RFP')
        );
    }
}

I do have a many_many relationship setup between File and a DataObject which the docs do say to avoid. Could that somehow be causing my issue? Also using texttextraction and fulltextsearch-localsolr in this project.

@GuySartorelli I tried adding a space in the yml config after extensions as suggested but no luck still.

So it turns out just using a hyphen for the extension in the yml config for FileFormFactory was the issue. Once I specified that the extension is a class it worked. like this:

SilverStripe\AssetAdmin\Forms\FileFormFactory:
  extensions:
    class: InnisMaggiore\Documents\Extensions\FileFormFactoryExtension

Hope this helps someone!

Just to clarify why this works:
Where you’ve got class: , that is actually just an alias for your extension. You can name it anything you like and is completely optional. If an extension is aliased it allows you to override/replace an extension by reassigning that key.

So be careful here, if for whatever reason you create another extension on FileFormFactory, make sure you don’t give it the same key (class) as it will replace this one

1 Like

He suggested to add a space between the dash and the class name, not after extensions (whatever that does mean). He also provided the diff to show you how to do that. And in fact in the YAML you posted still has the same issue.

The solution you provided probably works by accident (if it works), as that syntax is used to extend extensions.

It looked like he was saying remove that line and move it down one. He didn’t specify space after the dash. I can see it now that your explaining further. Adding class works probably because I have a space after class: and it also works with a space after hyphen so yes the missing space was the issue and adding class isn’t necessary. Thanks for clearing that up!

This is all about the YML syntax, and the way that is transformed into data for the application. Think of these in terms of PHP arrays.

When you use dashes, you can think of this in the same way as a PHP numeric array, so:

My\Class\ClassName:
  extensions:
    -  My\Class\Extension
    -  My\Class\AnotherExtension

Can be pictured like this:

$extensions = [
  "My\Class\Extension",
  "My\Class\AnotherExtension"
];

Whereas, using the text: text syntax, eg.

My\Class\ClassName:
  extensions:
    extension1:  My\Class\Extension
    extension2:  My\Class\AnotherExtension

Can be thought of in terms of an associative array, eg:

$extensions = [
  "extension1" => "My\Class\Extension",
  "extension2" => "My\Class\AnotherExtension"
];

As you can see, if you were to use the same text on the left of the colon in your yml file, you’d only ever end up with a single entry in the array, since it would just get overwritten.

As you’ve discovered, the yml syntax is important, and something as simple as a missing space or new line can cause things to stop working, or be incorrectly transformed (even though your initial code was actually valid yml) - meaning that it wasn’t the format needed for the Silverstripe code to understand).