Call to a member function update() on null

#1

Inside a hook, I’m loading an image from external source, after that I want to apply a specific template on it. Triggering the hook twice works, but is only necessary because triggering once gives the error mentioned above.

I even sleep() between these two steps, and it definitely happens in spite the image being there. That’s my code:

// Download takes like 2s, no problem ..
$object->downloadCover($fileName); // full name

sleep(15);

$page->image($fileName)->update(['template' => 'image']);

any suggestions, ideas?

// Edit: It seems to check the moment being executed if the file is there, when started and file already exists, it completes the task even though in reality the image is being overwritten (thus, ‘new’)

#2

Try pumping it through createfile(). I had a similar thing with my audio plugin since it rips album covers from MP3s and then sets it up with the meta file and stamps it with a template and more.

Not tested but something like this should do it… i guess your using a page create hook?


'setImage' => function() {
	$this->createFile([
		'source'   => $object->downloadCover($fileName),
		'filename' => $fileName,
		'template' => 'image',
		'content'  => [
				'alt' => 'Your Alt Text blah blah'
		]
	]);

Then i think if you trigger that method from inside the page create hook, you can ditch what you have already.

On a side note, you might need to check the filename from the external source to make sure it doesnt contain anything dodgy names. I bet there is something somewhere inside Kirby that will sanitise the name for you. You be lucky and createFile() does it for you.

#3

That’s alright, since the image URL goes like this: https://somepage.com/?isbn= and then interational standard book number, so I’m pretty free to chose whatever name I want.

Thx for ur input, I went with @bnomei’s Janitor plugin and now I’m pressing a button to download the image, and if the image exists, second click on the button immediately after first one applies template an stuff. Pretty neat :slight_smile:

1 Like
#4

Sure, but i think the above will work and you wont have to press buttons :slight_smile:

#5

… but how would I be passing the ISBN? When creating the page, it’s not there yet …

#6

Well your getting that from a third party service arn’t you? You can probably use file_get_contents().

$coverimage = file_get_contents('http://www.example.com/someimage.png');

And then pump that though createFile()

'setImage' => function() {
	$this->createFile([
		'source'   => $coverimage,
		'filename' => $fileName,
		'template' => 'image',
		'content'  => [
				'alt' => 'Your Alt Text blah blah'
		]
	]);
#7

Will try, thanks!

What’s the difference to using cURL, pro/con?

#8

file_get_contents: less options, requires allow_url_fopen configuration to be set (it usually is), might be slower than cURL.

Kirby provides Remote::get()/Remote::request() to make cURL requests much easier, https://getkirby.com/docs/reference/@/http/remote