Jump to content

Set inputfield image minWidth via API inside repeaters has no effect


gebeer
 Share

Recommended Posts

Hello,

I am reusing a generic image field inside repeaters and need to modify the required minWidth based on the context of the image inputfield.

So I have a repeater with fields "images" and "images_layout". Based on the image layout I am modifying image requirements. This is working fine for Inputfield->description or Inputfield->notes. But not for Inputfield->minWidth. The UI does not give warnings if the image size is below the minWidth.
Here's my hook code:

// add image info to Bilder field inside matrix item depending on layout of images
// restrict images with wrong dimensions
wire()->addHookBefore('InputfieldImage::render', function (HookEvent $event) {
    /** @var InputfieldImage $imageInput */
    $imageInput = $event->object;
    // if ($this->user->isSuperuser()) return;
    $hasPage = $imageInput->hasPage;
    if (!$hasPage instanceof RepeaterMatrixPage) return; // this example uses repeater matrix but behaviour is same with repeaters
    if ($hasPage->template->name != 'repeater_content_blocks') return;
    $ifWrapper = $imageInput->getRootParent();
    if($ifWrapper) {
        $layoutInputfields = $ifWrapper->find('label="Layout Bilder"');
        if ($layoutInputfields->count) {
            $layoutInputfield = $layoutInputfields->eq(0);
            $layoutField = $layoutInputfield->hasField();
            $layout = ($hasPage->get($layoutField->name)->value);
            switch ($layout) {
                case 'stacked':
                case 'stacked169':
                    $dims = array('width' => 2560, 'height' => 1440);
                    break;
    
                case 'parallax':
                    $dims = array('width' => 1024, 'height' => 768);
                    break;
    
                default:
                    $dims = array('width' => 2560, 'height' => 1440);
                    break;
            }
            $imageInput->notes = "Bild-Dimensionen (Breite x Höhe) in px: {$dims['width']} x {$dims['height']}";
            $imageInput->minWidth = $dims['width']; // this setting is not respected in GUI
            $imageInput->maxWidth = $dims['width']; // this setting is not respected in GUI
            $imageInput->setMaxFilesize('1m'); // 1 MB // this setting is not respected in GUI
            $imageInput->maxReject = true;

            $event->object = $imageInput;
        }

    }
});

When I set the minWidth via images field settings, I get a warning when image is below minWidth

minwidthgui.png.846e5c4409f213a79c1759ed2037afa3.png

minwidthwarning.png.3faf53314fd6293ba128595e4786454f.png

But this way I can only set one value for all image input fields in different contexts.

When I set the minWidth through my hook to the individual image input fields, I do not get this warning.

I would have to use separate image fields for each different context which I want to avoid for maintainability and DB performance reasons.

The code in InputfieldImage.module  returns $minWidth as empty string when there is no value set by the GUI and this seems to be the culprit.

Maybe I need to hook into InputfieldImage::fileAdded to achieve what I want? But in there I also do not have the minWidth available that I set through my hook. So currently I am lost and need some pointers on how to solve this.

Link to comment
Share on other sites

Such things where fields need custom settings both on the frontend (page editor) and the backend (processInput) are sometimes not so easy to handle. Another example is customizing the toolbar of ckeditor fields based on the user role.

The problem is that hooking into the inputfields render might not have any effect when the inputfield's input is processed. And it might also not have an effect when assets are loaded. Maybe you can set the settings in getInputfield() like shown in the linked thread?

But for ckeditor fields even the getInputfield() hook is not enough when it comes to frontend editing... I think that are issues but I have not investigated further and chosen to go the easy route and create custom fields for every case... If you want to tackle that problem and find a better solution I'd be grateful ? 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...