wet Posted May 23, 2016 Share Posted May 23, 2016 Is there a way to restrict the 'Page' field type to allow only a maximum number of pages and enforce their uniqueness? I am trying to build a 'featured products' slide show for the homepage of the target site. 'Products' are, of course, pages. The slide show is yet another page with a 'Page' field to which users can add 'featured pages' by adding and removing their product pages. It'd be convenient if I could specify additional restrictions: I do not want that a product occurs more than once. The slide show's 'Page' field should only contain unique pages. I do not want the user to add less than 4 or more than 6 pages to the slide show. The 'Page' field should not allow to add more than the maximum count and the template should validate upon save whether a minimum count of related pages has been added. I've tried two input input fields: PageSelectMultiple: Cannot find neither a parameter to enforce uniqueness nor to specify count restrictions. PageAutocomplete: Selected pages are unique though they keep reappearing in the autocomplete suggestions. Cannot specify count restrictions. Is there a core feature I could leverage, or would I have to resort to custom coding? Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 23, 2016 Share Posted May 23, 2016 Pagefields afaik are always unique in that they cannot hold a page multiple times. But enforcing the number of items is not possible. Personally I code such things to allow for 0-6 items in my template and just add a note to the field that it'll look bad without at least 4 items and that every access item after the first 6 will not be shown. 3 Link to comment Share on other sites More sharing options...
Robin S Posted May 23, 2016 Share Posted May 23, 2016 Adapting Soma's code from here, in /site/ready.php: $this->addHookAfter('InputfieldPage::processInput', function($event) { $field = $event->object; if($field->name == "featured_products"){ $count = count($field->value); if( $count < 4 || $count > 6 ) { $page = $this->modules->ProcessPageEdit->getPage(); $old_value = $page->get($field->name); $field->value = $old_value; $field->error("You must select more than 4 and less than 7 products"); } } }); 8 Link to comment Share on other sites More sharing options...
wet Posted May 23, 2016 Author Share Posted May 23, 2016 Thanks, very helpful. Link to comment Share on other sites More sharing options...
bernhard Posted May 23, 2016 Share Posted May 23, 2016 see also this post and my javascript solution: https://processwire.com/talk/topic/11538-pagefield-–-limit-number-of-choices/?p=107504 (and also tpr's modification) should not be too hard to adapt the code. putting everything into a modules would also be nice... it was beyond my head then and now i'm really busy, sorry 8 Link to comment Share on other sites More sharing options...
Jan Romero Posted May 23, 2016 Share Posted May 23, 2016 That UI is fricken genius. I love it. Very convenient that I can like it twice now lol 2 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