Documents AND archives on select options?


#1

Is it possible to use both “documents” and “archives” at the same time in select options?

selectfile:
	label: Select File
	type: select
	options: archives (AND) documents

…of course using :

   options: archives documents

…does not work

Thank you


#2

That’s not possible with the native select field. You may want to check out the Controlled List plugin or the brand new Relationship field. Both fields allow you to generate your options using a user defined function.


#3

Thank you,

In your oppinion, how complex would it be to implement a hardcoded or optional filter(function()) to the select field ?

I find it very hard to understand the fields architecture, but sometimes I am able to place the right code in the right place to produce some results.

I’d pay for custom fields videotutorials :wink:

cheers


#4

Pretty easy; basically, you just have to copy the select field, give it a different name and apply the filter (or a filter option), or rather, replace the options method with your own. Why don’t you want to use any of the suggested fields?


#5

Thank you,

Looking at the select field php I can’t figure out where are the options defined. Is it the validate function which works in par with the provided options? where are options such as ‘documents’ and ‘archives’ defined?

Oh and the Controlled List field would certainly work for this use case… but I’d love to add an option to a custom select :smile:

thanks again


#6

As I said, the options are defined in the options method:

public function options() {
    return FieldOptions::build($this);
  }

So instead of directly defining the options in the field method, this method calls upon the FieldOptions class (because these options are used with different fields). The field options class is defined in /panel/app/src/panel/form/fieldoptions.php. So this is a bit tricky, as you can’t just build upon that or rather - depending on whether or not you want to implement all the options or not - you would have to integrate the code from that class into your options method, or copy the FieldOptions Class, rename it and make that the basis for your modified FieldOptions class.

If you want a simple version, you can just hardcode your options within this method.


#7

Thank you,

In fieldoptions.php , line 207 the function items() contains a series of cases, and when documents or archives are passed as options, it seems to just pass them as methods of $page

public function items($page, $method) {
              ...
      case 'children':
      case 'grandchildren':
      case 'files':
      case 'images':
      case 'documents':
      case 'videos':
      case 'audio':
      case 'code':
      case 'archives':
        $items = $page->{$method}();  
        break;

Initially I thought… let’s just write a page method that gives me the extensions I need (zip,doc,pdf) ! but then I realized that I would still need to add a case for that method in that fieldoptions function for it to work, right?

And I can’t modify fieldoptions.php for it will be overwritten on upgrades.

If I wanted to add a custom class that is called by my custom select as I think you suggested, where would this class be placed ? Is this what a plugin is for ? Say one that could be called as such:

public function options() {
    return MyNewFieldOptions::build($this);
  }

Thanks for your help


#8

Exactly:

  • create a plugin
  • in your plugin file, require the MyFieldOptions class
  • also in the plugin file, register the new field
  • create a fields folder that holds the field
  • create a lib folder that holds the MyFieldOptions class (can also go into the plugin file, but it’s cleaner if it is separate)
  • (create a new page method)

Edit: Check out the boiler plugin: https://github.com/jenstornell/kirby-boiler-plugin