How to add custom fields to files


#1

Hi together,

is there a possibility to add custom fields like a file description or something else?

This would be also awesome for SEO reasons. Then you could add custom titles and alternative texts to an image for example.

Are there any ways to implement such fields.


Multiple files list on a content
#2

Check this out: http://getkirby.com/docs/panel/blueprints/file-settings#file-fields :smile:


#3

Custom fields for files are defined in the blueprints:

http://getkirby.com/docs/content/media/#adding-meta-data-to-your-files

http://getkirby.com/docs/panel/blueprints/file-settings


#4

Thanks, i completly overlooked that. :slight_smile:


#5

Thanks! Maybe You can tell, how can I access those custom text fields?
Looking at cheat sheets under $file …
in cheat sheet listed commands works, for example:

‘text’ => (string)$image->filename()

trying similar, but this dosnt work:

‘text’ => (string)$image->text()
or
’text’ => (string)$image->fields()->text()

p.s. trying to add file custom fields for exporting as JSON


#6

Let’s assume you have defined a custom field “caption”, then you can access the value of this field with:

<?php 
    $image = $page->images()->first();
    $caption = $image->caption();
    echo $caption;
?>

#7

Thanx! But it is not working for me, because Im trying to get file text in a bit different approach.

I am trying to add file textfield with other image data (like height, width etc), You can see in this example: http://forum.getkirby.com/t/export-all-site-content-as-json/264


#8

You can use $image->caption()->toString()


#9

Thanks! My bad (overlooked string in the huge amount of JSON data), it works both ways:

$image->caption()->toString()

and also

(string)$image->caption()


#10

One important thing though - if I only want a “Alternative Text” text field, or a “Featured Image ?” toggle field, to only show up for certain file types, in this case images -that isn’t possible right? :grin:


#11

Not that I know of, but that would be good feature, sth. like:

files:
 type: 
  - image
    fields:
      title:
        label: Title
        type: text
      alt_text:
        label: Alt Text
        type: text
  - document  
     fields:
        label: Title
        type: text
...

Different file fields for different file types
#12

Yeah exactly @texnixe - but there might also be cases where not all “kirby file types” should be handled equally, like jpg vs. psd or code files .js vs .html - that would need filtering by extension - so maybe also a slightly more flexible solution - but your nice simplistic example, was also the first thing that came to my mind :thumbsup:


#13

Doesn’t sound like anything Kirby’s core/panel will be able to do soon (my personal perception), especially when foxussing not on Kirby’s file types, but such fine detailed ones.

Idea:
Does the Kirby builder plugin work as file field? https://github.com/TimOetting/kirby-builder

If so, you could have a blueprint like:

files:
  fields:
    title:
      label: Title
      type: text
    filetypes:
      label: Filetypes
      type: builder
      fieldsets:
        jpg:
          label: jpg
          fields:
            …
        psd:
          label: psd
          fields:
            …

And then in your template:

<?php 
$filetype = $page->filetype()->toStructure(); 

if ($filetype->_fieldset() == 'jpg') {
…
} elseif (($filetype->_fieldset() == 'psd') {
…
} else {
…
}

?>

Completely untested, just an idea I had.


#14

I was in desperate need for something like this. I don’t know if there is a solution for this today in the core. At least I did not find anything.