Jump to content

Require a specific number of images in a field


Robin S
 Share

Recommended Posts

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");
    }
});

 

  • Like 10
Link to comment
Share on other sites

  • 2 years later...

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

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;
});

 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

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

  • 1 month later...

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

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...