Support

Account

Home Forums Add-ons Repeater Field Disable Repeater 'Add Row' Button

Solved

Disable Repeater 'Add Row' Button

    • vanian

    • August 23, 2017 at 5:56 am

    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;
    }
    • vanian

    • August 23, 2017 at 6:05 am

    For reference, this thread was very similar but unfortunately no solution was presented:

    Disable Update button until all fields have loaded

    • vanian

    • August 23, 2017 at 6:35 am

    Well, I thought this would work:

    acf/init

    …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.

    • vanian

    • August 29, 2017 at 8:40 am

    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;
    }
    
    • mulli

    • January 6, 2021 at 9:27 pm

    @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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.