Jump to content

Images fields question


Tyssen
 Share

Recommended Posts

I've been brought in to work on a site which allows members to upload images to display on their profile page. The number they can display is limited by the member group they're in.

Currently, the limit is only applied on output, not on input. The same image field is available to all members so they're all getting the same upload limit..

I'd like to change it so that each member group has its own images field and each field has the upload limit set to the limit for that group.

I know I can use field dependencies to show the correct image field depending on which member group they choose.

But how do I get the existing images that are attached to the current all-in-one images field into the new member group-specific image fields?

Link to comment
Share on other sites

1 hour ago, Tyssen said:

I'd like to change it so that each member group has its own images field and each field has the upload limit set to the limit for that group.

I suggest setting the maxFiles setting of the inputfield dynamically with a hook.

There are many different methods you could potentially hook - I don't know if you're talking about Page Edit, ProcessProfile, ProcessUser, or some form on the front-end, and I don't know if by "group" you mean "role". But here is something that you can adapt to suit:

$wire->addHookBefore("InputfieldImage::render", function(HookEvent $event) {
	/* @var InputfieldImage $inputfield */
	$inputfield = $event->object;
	$field = $inputfield->hasField;
	if(!$field || $field->name !== 'images') return;
	$user = $event->wire('user');
	if($user->hasRole('foo')) {
		$inputfield->maxFiles = 1;
		$inputfield->description = 'You may upload a maximum of 1 image.';
	} elseif($user->hasRole('bar')) {
		$inputfield->maxFiles = 2;
		$inputfield->description = 'You may upload a maximum of 2 images.';
	} else {
		$inputfield->maxFiles = 0; // No limit
	}
});

 

  • Like 2
Link to comment
Share on other sites

Ah, that's an interesting idea and probably less work than what I had in mind, lol.

Except in my case, the member group is assigned to a portfolio page via a page reference field, rather than being assigned to the user. And the user is given edit access to his/her page with the Page Edit Per User module. I can still work out which member group page a user is associated with, it just won't be as straightforward as using hasRole.

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