Jump to content

Related pages: How to enforce uniqueness and minimum/maximum count?


wet
 Share

Recommended Posts

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

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.

  • Like 3
Link to comment
Share on other sites

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");
        }
    }
});
  • Like 8
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...