Jump to content

Restrict Repeater Matrix


Robin S
 Share

Recommended Posts

Restrict Repeater Matrix

Allows restrictions and limits to be placed on Repeater Matrix fields. Requires ProcessWire >= v3.0.0 and FieldtypeRepeaterMatrix >= v0.0.5.

For any matrix type in a Repeater Matrix field you have the option to:

  • Disable settings for items (cannot change matrix type)
  • Prevent drag-sorting of items
  • Prevent cloning of items
  • Prevent toggling of the published state of items
  • Prevent trashing of items
  • Limit the number of items that may be added to the inputfield. When the limit is reached the "Add new" button for the matrix type will be removed and the matrix type will not be available for selection in the "Type" dropdown of other matrix items.
  • Hide the clone button when the limit for a matrix type has been reached. Note that in PW >= 3.0.187 this also means that the copy/paste feature will become unavailable for the matrix type.

Please note that restrictions and limits are applied with CSS/JS so should not be considered tamper-proof.

Usage

Install the Restrict Repeater Matrix module.

For each matrix type created in the Repeater Matrix field settings, a "Restrictions" fieldset is added at the bottom of the matrix type settings:

Settings

For newly added matrix types, the settings must be saved first in order for the Restrictions fieldset to appear. Set restrictions for each matrix type as needed. A limit of zero means that no items of that matrix type may be added to the inputfield.

Setting restrictions via a hook

Besides setting restrictions in the field settings, you can also apply or modify restrictions by hooking RestrictRepeaterMatrix::checkRestrictions. This allows for more focused restrictions, for example, applying restrictions depending on the template of the page being edited or depending on the role of the user.

The checkRestrictions() method receives the following arguments:

  • $field This Repeater Matrix field
  • $inputfield This Repeater Matrix inputfield
  • $matrix_types An array of matrix types for this field. Each key is the matrix type name and the value is the matrix type integer.
  • $page The page that is open in ProcessPageEdit

The method returns a multi-dimensional array of matrix types and restrictions for each of those types. An example of a returned array:

type-restrictions

Example hooks

Prevent the matrix type "images_block" from being added to "my_matrix_field" in a page with the "basic-page" template:

$wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) {
    $field = $event->arguments('field');
    $page = $event->arguments('page');
    $type_restrictions = $event->return;
    if($field->name === 'my_matrix_field' && $page->template->name === 'basic-page') {
        $type_restrictions['images_block']['limit'] = 0;
    }
    $event->return = $type_restrictions;
});

Prevent non-superusers from trashing any Repeater Matrix items in "my_matrix_field":

$wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) {
    $field = $event->arguments('field');
    $type_restrictions = $event->return;
    if($field->name === 'my_matrix_field' && !$this->user->isSuperuser()) {
        foreach($type_restrictions as $key => $value) {
            $type_restrictions[$key]['notrash'] = true;
        }
    }
    $event->return = $type_restrictions;
});

 

  • Like 16
Link to comment
Share on other sites

  • 2 weeks later...

@cjx2240, sorry to hear that. Can you give some more information please...

  • Is the "who_we_are" field a Repeater Matrix field?
  • Do you have the latest version of Repeater Matrix installed - v0.0.4?
  • Do you have debug mode enabled in /site/config.php? If not please enable and let me know if you see any error messages when you try and edit the field.
  • If you don't see any error messages still, please install Tracy Debugger, tick the "Backend" option for "Show Debug Bar", and check for errors when you try and edit the field, as in the screenshot below:

2017-07-07_094559.png.21f111189d28c49b36be31d0a76e9a7c.png

Thanks.

  • Like 2
Link to comment
Share on other sites

9 minutes ago, cjx2240 said:

Updating RepeaterMatrix from 0.0.3 to 0.0.4 resolved the issue, thanks.

Thanks for letting me know. I have updated the module to set a minimum version requirement for FieldtypeRepeaterMatrix.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...
8 hours ago, Juergen said:

There is always the message that FieldtypRepeaterMatrix is not the correct version, but I have downloaded it today from the modules directory - so it must be ok.

This module requires Repeater Matrix which is part of the Profields bundle. The Matrix module you have shown in your screenshot is something completely different.

Link to comment
Share on other sites

  • 1 year later...

v0.1.4 released. This version adds support for some new features added in Repeater Matrix v0.0.5 - you can disable the settings for matrix types (so items cannot change their matrix type), and when the limit for a type is reached it becomes unavailable for selection in the type dropdown of other matrix items.

The module now requires Repeater Matrix >= v0.0.5. If you are using Repeater Matrix v0.0.4 or older then there is no need to upgrade Restrict Repeater Matrix as the new features in v0.1.4 are only applicable to Repeater Matrix v0.0.5.

  • Like 2
Link to comment
Share on other sites

  • 2 years later...

Hi @Robin S, Thanks a lot for this module! Exactly what I was looking for 😉 

Version 0.0.8. of RepeaterMatrix introduced the 'insert before' and 'insert after' buttons. 
If I limit the use of matrix types to 1 item per type, it's still possible to add a new item via 'insert before' and 'insert after' (see screenshot below). Is it possible to prevent this?

matrix.png.a676cd0ded0bc4dd4d44fb8c2b6aea10.png

Link to comment
Share on other sites

13 hours ago, Didjee said:

Version 0.0.8. of RepeaterMatrix introduced the 'insert before' and 'insert after' buttons. 
If I limit the use of matrix types to 1 item per type, it's still possible to add a new item via 'insert before' and 'insert after' (see screenshot below). Is it possible to prevent this?

It's not just the new insert before / insert after features, it's also all the new options for adding items that would need attention.

2022-01-29_124714.png.3e2f161e0af808aeac521c05c61ccf15.png

It looks like a lot of work to support and probably it still won't work reliably for all the options, e.g. custom. Sorry, but it's unlikely I'm going to get around to spending the time on this in the foreseeable future.

It's probably not practical for a module like Restrict Repeater Matrix to exists when the base module (Repeater Matrix) can be changed at any point. Repeater Matrix is a commercial module so Ryan should be responsive to the needs of its users - I suggest submitting requests to Ryan for the features you want to see. I'll add a note at the top of the readme about the incompatibility with recent updates to Repeater Matrix.

  • Like 1
Link to comment
Share on other sites

On 1/29/2022 at 12:55 PM, Robin S said:

Sorry, but it's unlikely I'm going to get around to spending the time on this in the foreseeable future.

Well, I couldn't help myself. 🙂
Plus it wasn't as bad as I expected.

@Didjee, the insert buttons and other features from Repeater Matrix v7/8 should work in the newly released Restrict Repeater Matrix v0.2.0.

  • Like 3
  • Thanks 3
Link to comment
Share on other sites

Quote

the insert buttons and other features from Repeater Matrix v7/8 should work in the newly released Restrict Repeater Matrix v0.2.0.

@Robin S wow, this is really great! Thanks for spending your weekend hours on this update 😉 

  • Like 1
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

×
×
  • Create New...