Robin S Posted October 1, 2017 Share Posted October 1, 2017 Suppose you have an images field and you want editors to upload a specific number of images to that field. Using a hook in /site/ready.php you can display a field error in Page Edit if the number of images in the field does not match the required number. Just like the standard "required" behaviour, the requirement does not prevent the field being saved if the number of images is not correct so you would still want to check the image count in your template. $wire->addHookAfter('InputfieldImage::processInput', function(HookEvent $event) { $inputfield = $event->object; // Only for this field if($inputfield->hasField != 'images') return; // Only in ProcessPageEdit if($this->process != 'ProcessPageEdit') return; $page = $this->process->getPage(); // Only for this template if($page->template == 'home') { if(count($inputfield->value) !== 4) $inputfield->error("Please upload exactly 4 images to this field"); } }); 10 Link to comment Share on other sites More sharing options...
jsantari Posted February 12, 2020 Share Posted February 12, 2020 This is an interesting hook. I'm looking for a way to limit the number of images a user with a specific role can upload. I'm using FEEL as a front-end editor for a membership/directory type site. I want for example someone with a basic-member role to only be allowed 2 images, silver-member 5 etc.. Is this possible? It would need to not allow them to add additional images but they would be able to still edit/delete an image if they were at their max limit. Link to comment Share on other sites More sharing options...
Robin S Posted February 12, 2020 Author Share Posted February 12, 2020 8 hours ago, jsantari said: I want for example someone with a basic-member role to only be allowed 2 images, silver-member 5 etc. To do this you would set the "maxFiles" property of the inputfield dynamically. In the field settings set "0" for "Maximum files allowed" (i.e. no limit). In /site/ready.php: $wire->addHookBefore('InputfieldImage::processInput', function(HookEvent $event) { /* @var InputfieldImage $inputfield */ $inputfield = $event->object; $process = $event->wire('process'); $user = $event->wire('user'); // Only for a specific field if($inputfield->hasField != 'images') return; // Only in ProcessPageEdit if($process != 'ProcessPageEdit') return; $page = $process->getPage(); // Only for a specific template if($page->template != 'basic-page') return; $limit = null; if($user->hasRole('basic-member')) $limit = 2; if($user->hasRole('silver-member')) $limit = 5; // Only if a limit applies to this role if(!$limit) return; $inputfield->maxFiles = $limit; }); 2 Link to comment Share on other sites More sharing options...
jsantari Posted April 1, 2020 Share Posted April 1, 2020 Had to sit on this for awhile as other things have taken priority. Tried it out and it works on preventing me from adding an image over limit. However it does it after it initiates the upload showing a thumbnail and leaves the spinner spinning. Not really what I would want for UX. What would work best would be for it to disable uploading if the limit has already been reached so they don't even try but that may not be possible? Link to comment Share on other sites More sharing options...
techweek Posted May 13, 2020 Share Posted May 13, 2020 That was brilliant! thank you for this On 10/1/2017 at 11:59 PM, Robin S said: Suppose you have an images field and you want editors to upload a specific number of images to that field. Using a hook in /site/ready.php you can display a field error in Page Edit if the number of images in the field does not match the required number. Just like the standard "required" behaviour, the requirement does not prevent the field being saved if the number of images is not correct so you would still want to check the image count in your template. $wire->addHookAfter('InputfieldImage::processInput', function(HookEvent $event) { $inputfield = $event->object; // Only for this field if($inputfield->hasField != 'images') return; // Only in ProcessPageEdit if($this->process != 'ProcessPageEdit') return; $page = $this->process->getPage(); // Only for this template if($page->template == 'home') { if(count($inputfield->value) !== 4) $inputfield->error("Please upload exactly 4 images to this field"); } }); That was brilliant, thank you for this Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now