About Restrict Repeater Matrix

Allows restrictions and limits to be placed on Repeater Matrix fields.

Category Admin Helpers
Admin helper modules are those that provide helpful tools, UI enhancements or optimizations to the admin panel (excluding Fieldtype or Inputfield modules).
Release StateBeta
Close to stable, but users are advised to be cautious and test thoroughly.*
Authorrobin-s
Module Version0.0.2
Class NameRestrictRepeaterMatrix
Compatibility3.0
Date AddedJuly 29, 2017
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/RestrictRepeaterMatrix/
How to install or uninstall modules

README

Restrict Repeater Matrix

A module for ProcessWire CMS/CMF. Allows restrictions and limits to be placed on Repeater Matrix fields. Requires FieldtypeRepeaterMatrix >= v0.0.4

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

  • 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

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:

2017-06-26_213521

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:

2017-06-26_220809

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

License

Released under Mozilla Public License v2. See file LICENSE for details.

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.