Home › Forums › Add-ons › Repeater Field › Disable Repeater 'Add Row' Button
I’d like to modify my Repeater buttons/links based on the current User’s role, disabling them (or adding a class) so that only certain users with the proper capabilities can add more rows. After some searching here, I have what seems to be an almost-working solution, but I don’t think the buttons exist yet when my function runs (code shown below).
Is there a flag that I can listen for to know when all of the ACF fields and elements are finished loading? Thank you.
add_filter('acf/load_field', 'check_ACF_permissions');
function check_ACF_permissions($field) {
global $post;
if ($post->post_name == 'project-tracker') {
$field['readonly'] = 1;
?><script type='text/javascript'>jQuery('.acf-actions').data('event', '');</script><?php
$userRoles = ( km_get_user_role() );
foreach($userRoles as $r => $role) {
if ($userRoles[$r] == 'project-manager') {
$field['readonly'] = 0;
?><script type="text/javascript">jQuery('.acf-button').data('event', 'add-row');</script><?php
break;
}
}
}
return $field;
}
For reference, this thread was very similar but unfortunately no solution was presented:
Disable Update button until all fields have loaded
Well, I thought this would work:
…but it fires before the acf/load_field filter runs.
You would need to add you own custom JavaScript https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/. You could localize this with information about the current user and based on that you could remove the buttons. Unfortunately, without spending the time to actually build something that will do this I can’t really help you more than than.
Thanks for your reply; I got it working like this (using acf/load_field to first check permissions and set a global var):
add_action ( 'admin_footer', 'check_ACF_permissions_button' );
function check_ACF_permissions_button($post) {
if ($GLOBALS['projectManager'] === false) {
?><script type="text/javascript">
jQuery('a[data-event="add-row"]').remove();
jQuery('a[data-event="remove-row"]').remove();
</script><?php
}
}
Hello,
I resolved this problem with:
add_filter('acf/load_field/type=repeater', 'disable_repeater');
function disable_repeater($field) {
if($field['readonly'] || $field['disabled']){
?>
<script type='text/javascript'>
acf.addAction('load', function(){
jQuery('.acf-actions, .acf-row-handle').remove();
});</script>
<?php
}
return $field;
}
@vanian , @p-j-albuquerque Thanks for sharing!
I used acf/load_field/type=repeater
for new post & form generation (ie. not for edit)
Realizing that ALL repeaters are affected.
{
foreach (RepeaterItem rptItem in Repeater1.Items)
{
if (rptItem.FindControl(“lbtnTest”) != null)
((LinkButton)rptItem.FindControl(“lbtnTest”)).Enabled = false;
}
}
The topic ‘Disable Repeater 'Add Row' Button’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.