Programmable field problem

Hi, I am trying to make one field programmable, but I am not doing something right.

This is my field (blueprints/fields/product-cover.php).

<?php

$minwidth = page()->parent()->productImagewidth()->isNotEmpty() ? page()->parent()->productImagewidth() : '800';
$minheight = page()->parent()->productImageheight()->isNotEmpty() ? page()->parent()->productImageheight() : '800';

return [
    'type' => 'image-clip',
    'clip' => [
      'minwidth' => $minwidth,
      'minheight' => $minheight,
      'ratio' => 'fixed'
    ],
    'multiple' => 'false',
    'layout' => 'cards',
    
    'image' => [
        'cover' => 'true'
      ],
];

The error I get is
Call to a member function productImagewidth() on null

I think it has something to do with parent() part, it is not getting that. But it also doesn’t work with this
site()->children()->findBy('intendedTemplate', 'shop') instead of page()->parent().

Any help is highly appreciated.

Have you tried to hardcode the page?

page('shop')

As I wrote in the intro of the recipe, there are certain limitations, e.g. you can never get the current page or so, that’s why querying the parent of page() will not work.

Yes, hardcode works, but it is not a good solution.

Uh, this sounded like such a great idea.

Oh, sorry!

It works with site()->children()->findBy('intendedTemplate', 'shop') :rocket::rocket::rocket:

I was missing toInt() for the number field. :smile:

Also you can use shorthand when you want set default values:

$minwidth = $page->productImagewidth()->or(800)->int();
3 Likes

Thanks, @ahmetbora, even better :muscle: I always forget about that shorthand.