Panel hook to file page object don't work

I got an issue with one of the filters, or an object.

What it does

When upload an image to a Kirby page in the panel it uploads it and create a text-file with some debug info.

What don’t work

It says here how to get the page object…

http://forum.getkirby.com/t/how-can-i-get-the-parent-object-of-a-file/897

…but I don’t get this to work…

$file->page()->slug()

Example

Uncomment the row with slug() to make the example work.

<?php
kirby()->hook('panel.file.upload', 'run');

function run( $file ) {
	$path = kirby()->roots()->content();

	$content = '';
	$content .= $file->page()->slug() . "\n";
	$content .= $file->name();

	file_put_contents( $path . '/test.txt', $content );
}
  • How can I save the file page slug to the text-file?
  • Is there an easier way to debug hooks than this?

EDIT:

I made a workaround, but it’s not so nice:

$file_bits = array_reverse( explode( '\\', $file->root() ) );
$slugs['parent'] = $file_bits[1];
$slugs['grandparent'] = $file_bits[2];

The error logs tell me that the page() method is undefined, so somehow this method is not available for some reason.

[Edit] Maybe just the methods of the media class are accessible here.

This what you want ?

kirby()->hook('panel.file.upload', function($file) {
        $file->page()->update([
            'test' => 'DONE'
        ]);
    });

It´s on the way…

1 Like

Great! Should I set this to solved when there is a task on Github or leave it open until it is really fixed?

Probably leave it open until solved… for the folks to see.

Set to fixed on development branch on Github.