Use image field with a custom textarea button

Hi there,

I added an “Insert Image” button to the textarea button panel to ease inserting images with the (image: filename) syntax. It works like a charme if you define a select field for the image and populate the options with the query method. It does not work, however, if you use the Kirby core image field with thumbnail preview.

The error message is the following:

Fatal error: Method ImageField::__toString() must not throw an exception, caught Error: Call to a member function image() on null in [...]\kirby\toolkit\lib\brick.php on line 0

The actual call to the member function happens at \panel\app\fields\image\image.php::20.

As far as I tracked it down, $this->page is not set, but I cannot figure out, where it is set for image fields in the edit page view.

Does anyone know another way to use image fields in modals, when calling them from a textarea button?

Cheers

That reminds me that I had exactly the same issue a couple of weeks ago but then didn’t have the time to look into it and completely forgot about it… :thinking:

Alright I tracked it down:

The issue is that the panel fields get their page model from a method invoked by the blueprint class. That is obviously not called, when your field is not instanciated as a blueprint field. You simply have to set the page attribute yourself, when you define your image field in the form:

// site/fields/textarea/forms/image.php
// [...]
return function($page, $textarea) {

  $form = new Kirby\Panel\Form(array(
    'image' => array(
      'label'       => 'fields.textarea.buttons.image.label',
      'type'        => 'image',
      'autofocus'   => 'true',
      'required'    => 'true',
      'page'       => $page
    )
  ));

// [...]

Problem solved! Hope that will help somebody else in the future :wink:

1 Like

Thanks for sharing your findings :slight_smile:, @benzin.