Get field value of current Page in a file.create:after Hook plugin

Hello Community,

how can i get a field value of the current page inside of a hook?
i’ve tried $page->voiceid()->text()->value() without success

	'hooks' => [
		'file.create:after' => function ($file,$page) {
        	voiceImage($file,$page->voiceid()->text()->value());
		},

You can access the parent of a file (i.e. the page the file belongs to) with $file->page(). This gives you the page object and with it you have access to all fields of that page.

thx, but i’m too stupid to get my field.

how do i need to access my text field content from a field called “voiceid” i’ve tested this
$file->page()->content()->voiceid()->text()->value()
but it didn’t worked.

$file->page()->voiceid()

Or if you want to make sure to get a string

$file->page()->voiceid()->value()

You can also go via the content object (this is necessary if you use field names that clash with Kirby method names

$file->page()->content()->voiceid()->value()

or

$file->page()->content()->get('voiceid')->value()

Thanks alot for those examples, would be great to find them already in the documentation.

They are all documented…

Of course, you won’t find your field names in the documentation.

You will also find example template output for each field type in the reference, for example for the structure field:

Hello Sonja,

i’ve tried with all of the recommended solutions to get the field. but with the code below and all other way to get the filed i always get the error “Call to a member function content() on null”

	'hooks' => [
		'file.create:after' => function ($file) {
        	voiceImage($file,$file->page()->content()->get('voiceid')->value());
		},

my Blueprint looks like this

fields:
  voiceid:
    label: voiceid
    type: text

do you got any idea why this isn’t working.
The hook is placed in a plugin and works perfect without getting the voiceid.

That’s rather surprising. A file always belongs to a page and a page should always have a content object.
Is the error really caused in the hook?

Hello Sonja,

i’ve found the reason why it doesn’t work. i’ve uploaded the files to another folder so there was no folder with pagename and therefore no page object in the File (i’m using virtual pages from DB)).

After removing the uploads Parent everything worked as expected and the Page Object was available.

    uploads: 
      #parent: site
      template: voiceimage

Btw. ofcause it is documented in the docs, but not so nicely and detailed like you told it to me :slight_smile: . Thanks again for the great support.