Upon running /dev/build, the ‘Copyright’ field is created as expected, but when opening a file in the CMS, the corresponding TextField isn’t rendered.
In fact, my updateCMSFields method doesn’t even get called (tried putting a ‘die()’ statement in there).
Hi @schellmax I encountered this issue too (see this comment). The new assets admin doesn’t call getCMSFields() as you pointed out, instead a factory is used to build the editing form. It’s counter-intuitive and I’m not sure why it was done that way… but there is probably a good reason?
Anyway, you can hook in to SilverStripe\AssetAdmin\Forms\AssetFormFactory::getFormFields with the updateFormFields() extension hook to add a field to the file editing form.
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:
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.
using a second extension seems to be the only way then.
this is what i came up with, inspired by the silverstripe-focuspoint module code @JonoM mentioned:
class ImageFormFactoryExtension extends Extension
{
public function updateFormFields(FieldList $fields)
{
$fields->insertAfter(
'Title',
TextField::create('Copyright', 'Copyright')
);
}
}
so there is not only AssetFormFactory but also ImageFormFactory, which i used, as i need this for images only.
I tried this and it works. Couldn’t find a full working example so I did what @schellmax did and looked at the code for the silverstripe-focuspoint module. 2 new files and an edit to my main yml file.
Image extension ImageExtension.php :
<?php
namespace MyCoolProject;
use SilverStripe\ORM\DataExtension;
class ImageExtension extends DataExtension {
private static $db = ["caption" => "Text"];
}
Form Factory extension ImageFormFactoryExtension.php :
<?php
namespace MyCoolProject;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextareaField;
class ImageFormFactoryExtension extends Extension {
public function updateFormFields(FieldList $fields) {
$fields->insertAfter(
"Name",
TextareaField::create("caption", "Image caption")->setRows(3)
);
}
}