File fields dependant on file type?

Is it possible to define file fields for pdf’s, but other (or no) file fields for images in the panel?

Please tell me how :slight_smile:

Check out the selector plugin, it allows you to filter files by type.

I’m sorry; I think I didn’t elaborate enough. This is my use case:

I have a set of pdf’s on a multilingual website. I would like to have checkboxes on each pdf to manage on which language they should be displayed.

This field should not be visible on any other file (such as images), as it would be confusing for editors.

I hope it is a bit clearer now? It is not really about filtering files by type, but more about showing a field (the language checkboxes) only on files of some type (pdf or document) in the panel.

Thanks!

Ah, ok, we are talking about file metadata fields here. This is not possible out of the box. You should be able to achieve this via a custom field that extends the checkboxes field.

Yes, sorry for the confusion and thanks for you reply.

But I still can’t see how I would “bind” that new custom field to “pdf’s” only?

Here’s a quick example, using the file function from the focus field:

<?php

class DocumentField extends CheckboxesField {

  // load current file
  public function file() {
    if (!empty(panel()->route->arguments[1])) {
      $fileName = urldecode(panel()->route->arguments[1]);
      return $this->page()->file($fileName);
    }
    return NULL;
  }



  public function label() {

    if(!$this->label) return null;
      if ($this->file()->type() == 'document') {
      $label = new Brick('label', $this->i18n($this->label));
      $label->addClass('label');
      $label->attr('for', $this->id());

      if($this->required()) {
        $label->append(new Brick('abbr', '*', array('title' => l::get('required', 'Required'))));
      }

      return $label;
    } else return NULL;

  }


  public function item($value, $text) {
    if ($this->file()->type() == 'document') {
      $item = parent::item($value, $text);
      $item->replaceClass('input-with-radio', 'input-with-checkbox');
      return $item;
    }
  }

}

This just checks for documents in general, you’d have to adapt that to filter for pdf only, if you really need that. But it should give you an idea.

1 Like