Robin S Posted November 24, 2016 Share Posted November 24, 2016 LimitRepeater Allows restrictions and limits to be placed on Repeater fields. For any Repeater field you can limit the number of items that may be added and also prevent the use of drag-sorting, toggling of published state, and the trashing of items. There is also an option to hide the clone button when the limit is reached. Usage Install the LimitRepeater module. Since v0.2.0 the module settings are configured at Setup > Fields > [your Repeater field]. The settings are contained within the "Restrictions" fieldset on the "Details" tab. Please note that the restrictions limits are applied in Page Edit with CSS/JS so should not be considered tamper-proof. Setting restrictions via a hook Besides setting restrictions in the field settings, you can also apply or modify restrictions by hooking LimitRepeater::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 field $inputfield This Repeater inputfield $page The page that is open in ProcessPageEdit The method returns an array of restrictions for the Repeater field. An example of a returned array: Example hook Prevent non-superusers from trashing any items in "my_repeater_field": $wire->addHookAfter('LimitRepeater::checkRestrictions', function(HookEvent $event) { $field = $event->arguments('field'); $restrictions = $event->return; if($field->name === 'my_repeater_field' && !$this->user->isSuperuser()) { $restrictions['notrash'] = true; } $event->return = $restrictions; }); Upgrading from < v0.2.0 There are two major changes to be aware of when upgrading from earlier versions of the module. The settings are no longer defined on the module config page, but rather in the field settings of each Repeater field: Setup > Fields > [your Repeater field]. If you visit the module config page you'll find shortcuts to the settings for each Repeater field. In earlier versions you could apply restrictions to a particular role. This is still possible but is now handled by hooking LimitRepeater::checkRestrictions as this is a more flexible and powerful approach. If you were applying restrictions to a particular role or roles you'll need to add hook code to achieve the same effect after you upgrade the module. See the hook information above in this readme. https://github.com/Toutouwai/LimitRepeaterhttp://modules.processwire.com/modules/limit-repeater/ 12 Link to comment Share on other sites More sharing options...
adrian Posted November 24, 2016 Share Posted November 24, 2016 @Robin S you're a man of no limits these days! Btw, what is that text from 4 Link to comment Share on other sites More sharing options...
Robin S Posted November 24, 2016 Author Share Posted November 24, 2016 13 minutes ago, adrian said: @Robin S you're a man of no limits these days! I'm really enjoying module development in PW. The flexibility that comes from hooks is brilliant. A couple more modules due shortly. 15 minutes ago, adrian said: Btw, what is that text from An all-time classic. 1 Link to comment Share on other sites More sharing options...
szabesz Posted November 24, 2016 Share Posted November 24, 2016 5 hours ago, Robin S said: A couple more modules due shortly. Can't wait! 1 Link to comment Share on other sites More sharing options...
LimeWub Posted November 24, 2016 Share Posted November 24, 2016 Thanks for this! 1 Link to comment Share on other sites More sharing options...
Juergen Posted November 25, 2016 Share Posted November 25, 2016 Very useful! I use it in combination with opening hours. For this reason I have created a repeater with 7 repeats (one for each day). In this case it prevents the customer from deleting or draging a weekday from the repeater list. 7 Link to comment Share on other sites More sharing options...
szabesz Posted November 25, 2016 Share Posted November 25, 2016 Thanks @Juergen! But how did you do the columns? No matter what Column Width settings I use, I have 100% width fields in my repeater... Am I missing something? Link to comment Share on other sites More sharing options...
Juergen Posted November 25, 2016 Share Posted November 25, 2016 Hello @szabesz here are my settings for the repeater field: best regards 3 Link to comment Share on other sites More sharing options...
szabesz Posted November 25, 2016 Share Posted November 25, 2016 Thanks! What is the firts one with 100%? I simply have two 50%, or 30%-30% or whatever, but the result is two 100% wide "columns". Strange... But thanx anyway. I will try to figure it out somehow. Link to comment Share on other sites More sharing options...
Juergen Posted November 25, 2016 Share Posted November 25, 2016 Its only here for creating the label and will not be displayed as a field in the template (see field settings below). I only use it to enter the day of the week (fe Monday). As you can see in my previous post every repeater has the name of the day in the repeater label. Field settings of the hidden field: 1 Link to comment Share on other sites More sharing options...
szabesz Posted November 25, 2016 Share Posted November 25, 2016 I see, thank you once more. Well, I dunno, it is not working "here" Link to comment Share on other sites More sharing options...
szabesz Posted November 25, 2016 Share Posted November 25, 2016 (edited) Okay, finally I figured out that I was fiddling with the wrong settings, so that is why it "did not work". Thanks to @Juergen who was kind enough to take a look at it! Edit: Black Friday, eh? Edited November 25, 2016 by szabesz 2 Link to comment Share on other sites More sharing options...
Robin S Posted February 4, 2017 Author Share Posted February 4, 2017 v0.0.3 released. You can now select multiple roles to be affected by the limit/restrictions, including the superuser role and "all roles". 4 Link to comment Share on other sites More sharing options...
Robin S Posted March 8, 2017 Author Share Posted March 8, 2017 v0.0.4 released. Adds config option to remove clone button. 4 Link to comment Share on other sites More sharing options...
netcarver Posted July 20, 2018 Share Posted July 20, 2018 @Robin S Thanks for this very useful module! I've not yet dug into its internals, but before I do, I'd like to know if it would it be possible to control the "trashability" of a repeater entry based on a value within it? (I have a hidden field in one of my repeaters and I'd like to prevent deletion of items if that field is non-blank.) Is the module ajax calling to check for conditions or is everything pushed into client-side JS? Link to comment Share on other sites More sharing options...
Robin S Posted July 22, 2018 Author Share Posted July 22, 2018 Hi @netcarver, Right now the module can't do that. It's on my to-do list to update this module so that it has a hookable method that would allow the sort of thing you're wanting to do, but not sure when I'll get around to that. For now you could use a hook to hide/disable the trash icon, similar to what I described in this topic: In /site/ready.php: $wire->addHookAfter('InputfieldFieldset::render', function(HookEvent $event) { /* @var $fieldset InputfieldFieldset */ $fieldset = $event->object; $attr = $fieldset->wrapAttr(); // Fieldsets in a Repeater inputfield have a data-page attribute if(isset($attr['data-page'])) { // Get the Repeater item $p = $this->pages((int) $attr['data-page']); if($p->hidden_field != '') $fieldset->addClass('no-trash'); } }); In some custom admin CSS: /* Hide and disable the trash icon while keeping its space in the layout */ .no-trash .InputfieldRepeaterTrash { visibility:hidden; pointer-events:none; } You could use display:none instead if you're not worried about the icon alignment. 3 Link to comment Share on other sites More sharing options...
netcarver Posted July 23, 2018 Share Posted July 23, 2018 @Robin S thank you, that works fine. 1 Link to comment Share on other sites More sharing options...
PWaddict Posted January 8, 2019 Share Posted January 8, 2019 @Robin S If I create a process module with a repeater field, will I be able to limit it with your module? Link to comment Share on other sites More sharing options...
Robin S Posted January 8, 2019 Author Share Posted January 8, 2019 1 hour ago, PWaddict said: If I create a process module with a repeater field, will I be able to limit it with your module? As far as I know, it's not possible to use a repeater field in a Process module. A repeater is necessarily a combination of fieldtype and inputfield whereas a Process module can only contain inputfields. 1 Link to comment Share on other sites More sharing options...
PWaddict Posted February 25, 2019 Share Posted February 25, 2019 @Robin S If a repeater doesn't have any items, the "Add New" link is still visible. Can you please add an option that will allow us to hide it? Link to comment Share on other sites More sharing options...
Robin S Posted March 3, 2019 Author Share Posted March 3, 2019 @PWaddict, if I understand right you want to set a limit of zero items. I added support for this in v0.1.6. 1 1 Link to comment Share on other sites More sharing options...
Robin S Posted February 10, 2022 Author Share Posted February 10, 2022 v0.2.0 of this module has been released, and because it is a fairly major rewriting of the module there are a couple of things to be aware of before upgrading: 1. The settings are no longer defined on the module config page, but rather in the field settings of each Repeater field: Setup > Fields > [your Repeater field]. If you visit the module config page you'll find shortcuts to the settings for each Repeater field. 2. In earlier versions you could apply restrictions to a particular role. This is still possible but is now handled by hooking LimitRepeater::checkRestrictions as this is a more flexible and powerful approach. If you were applying restrictions to a particular role or roles you'll need to add hook code to achieve the same effect after you upgrade the module. See the hook information in the updated readme. 3 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