Assets // Added a custom field // How to use it in search?

Hi there,

I am starting over with silverstripe 4 (have used version 3 a few years ago).

And just figured out how to add a custom field with a “DataExtension” to the assets (file or image). E.g. to add a “Copyright” field.

Great so far.

But how can I use the new field in the admin search?

I tried to use the “searchable_fields”-array but no success so far.

Would be great if someone could post a rough blueprint and/or some relevant keywords
how this could be achived. If it is possible at all.

Kind regards

Steve

Can you share what you tried? Defining private static $searchable_fields in your DataExtension should just work.

Thank you for your reply.

I followed this example (with a fresh install of silverstripe 4.10):

I only added one more field “Copyright” (Varchar).

Both custom fields show up in the edit form, and I can store data.
But now I am stuck to make these new fields searchable.

app/src/MyFileExtension.php

<?php
namespace MyProject;

use SilverStripe\ORM\DataExtension;

class MyFileExtension extends DataExtension
{
    private static $db = [
        'Description' => 'Text',
        'Copyright' => 'Varchar',
];

    private static $searchable_fields = [ 'Description', 'Copyright' ];
}

app/src/MyFormFactoryExtension.php

<?php
namespace MyProject;

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

class MyFormFactoryExtension extends Extension
{
    public function updateFormFields(FieldList $fields)
    {
        $fields->insertAfter(
            'Title',
            TextareaField::create('Description', 'Description')
        );

        $fields->insertAfter(
            'Title',
            TextField::create('Copyright', 'Copyright')
        );
    }
}

app/_config/app.yml

SilverStripe\Assets\File:
  extensions:
    - MyProject\MyFileExtension
SilverStripe\AssetAdmin\Forms\FileFormFactory:
  extensions:
    - MyProject\MyFormFactoryExtension

Sorry, I realized just now you were talking about SilverStripe\Assets\File: this changes things.

Customizing the search form is quite easy:

SilverStripe\AssetAdmin\Forms\FileSearchFormFactory:
  extensions:
    - MyFileSearchFormExtension
MyFileSearchFormExtension.php
<?php

use SilverStripe\Core\Extension;
use SilverStripe\Forms\TextField;

class MyFileSearchFormExtension extends Extension
{
    public function updateFormFields(&$fields, $controller, $name, $context)
    {
        $fields->push(TextField::create('Copyright', 'Copyright'));
    }
}

The problem is making use of that in real searches: there seems to be a mix of client JavaScript and server GrapQL that has changed quite a bit since the 4.0 release. Unfortunately I’m not fond of that part of SilverStripe so I cannot give you any advice.

Thank you anyway.

The task seems surprisingly difficult.
Maybe I need to consider another option.
But I’m going to dive into the GraphQL stuff first.