Support

Account

Home Forums Front-end Issues External condition for displaying field

Solved

External condition for displaying field

  • Hey,

    I was wondering whether it’s possible to show a field basend on condition (apart from the built in conditional logic).

    Something like hooking into the field and if a certain condition is not met not to display the field.

    I hooked into acf/load_field/name=my_field and in my function used

    public function my_acf_load_field($field)
        {
            if(!my condition):
              $field = null;
              return $field;
            endif;
        } 

    But this still renders an empty text input (instead of the repeater field) on my frontend form. Is there a way to not render/display the field at all?

    Thanks,
    Florian

  • If anyone is able to help, this would be highly appreciated. Thank you.

  • Is this something you want to have change, like the current conditional logic makes fields appear and disappear or something that will just hide or show the field and it will not change if other selections are made?

  • I want to show / hide the field in the frontend form based on a custom user meta.

    So no conditional relationship to any other fields. I want to have some sort of php condition whether to show the field for the specific user at all in the frontend form (where users can send in posts).

    Something like this (pseudo code)

    if(get_user_meta('myfield') == 'myvalue'):
     show_this_acf_repeater_field;
    endif;
  • I was about to post that you could set the class of wrapper to hidden and then add some custom CSS when I stumbled upon something that I was unaware of.

    Using and acf/load_field filter http://www.advancedcustomfields.com/resources/acfload_field/

    
    add_filter('acf/load_field/name=test_hidden_field', 'load_field_test_hidden_field');
    function load_field_test_hidden_field($field) {
    	$condition = false; 
    	if (!$condition) {
    		$field['wrapper']['class'] = 'hidden';
    	}
    	return $field;
    }
    
  • Thanks a lot. This works. But I’m a bit concerned that the field is still part of the form (just hidden)… for security reasons I’d rather see a possibility to use the load field filter in a way to remove the field. But this seems not to be possible.

  • No, I don’t think it’s possible, at least not using filters.

    It would be possible if you dynamically generated and registered the field group in code. Doing this you could check for your condition and if it’s not met then you don’t add the field to the group. But that’s about the only way I can think of to do what you want.

  • Thank you for your input. I tried this last night but it didn’t work

    I tried to conditionally register a repeater field that should sit within a tab field… but I threw out some errors.

    I think I stick to your class solution. Thanks a lot!

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

The topic ‘External condition for displaying field’ is closed to new replies.