Support

Account

Home Forums Add-ons Repeater Field general use of variables in ACF

Solving

general use of variables in ACF

  • hi,

    i created my own field types, that often base on existing acf field types like “repeater”. i did this to have an easier and faster setup for my field configuration.

    the “problem” (its not a real issue) is, that acf often uses variables in an uncommon way.

    e.g.:

    if( $field['collapsed'] ) {

    should be written:

    if( isset($field['collapsed']) ) {

    the most php versions throw an error, if the field value not exists. but it is not really important that it exists.

    would it be possible to “fix” this unclean use of variable use in future versions of acf? this would be really great.

    .thanks
    .dennis

  • Hi @dennis

    Could you please tell me the detailed steps to reproduce the error message?

    Thanks!

  • hi,

    as i said, it is not a real issue. i developed my own field type and in it i use the real acf pro field “repeater”. it is just, cause i can do all configurations directly in my config files and do not need to click through the acf setup pages. it just save A LOT of time for me.

    ofcourse i try to set up as few variables as i can to reduce my config files.

    newer versions of php throw errors, if a variable or an array value is not existing. its a common way to ask for the existing of a variable by using “isset()”. the way it is done often in the acf code is an old way, that php in newer version do not like.

    just turn on all error_reporting (EALL) and write something like:

    if( $variable) {
     ....
    }

    you should see an error output.

  • at the end, it is just a nice to have for me, but i think this coding for an “IF..” is old stuff, that could be more clean.

  • Hi @dennis

    Could you please tell me the PHP version you have? Also, could you please share your code the steps to reproduce the error message (such as adding the custom field or adding a new row)?

    The problem is that I couldn’t reproduce the message on my end using the repeater field. I believe the plugin author doesn’t use the isset() function because $field['collapsed'] should always be set when it’s called. It’s possible that your code doesn’t set this variable so PHP returned the error.

    I hope this makes sense. Thanks!

  • hi,

    as i said, it is not a real issue. i can not provide any code, cause it is just too special and complex to show it. its clear that inside acf everything is fine, cause all variables or array values (should) exist.

    it is just a bit “dirty” code, that an if($variable) statement is just called without isset(). since php 5.x (i’m not sure since which version), php throws an error, if you do not write if(isset($variable))…

    as i said, using acf repeater field as proposed by the author will NEVER throw this error, cause every possible variable or array value will exist.

    ok, just a snippet:
    i have my own class, just nearly the same like acf_field_repeaters:

    class acf_field_my_own_repeaters extends acf_field

    and within this class i have the same functions like in the original acf repeater class, but not just as a copy-paste. i directly “include” the original acf repeater class function, but “send” my additionally field settings to the original class-function, e.g. the “sub_fields”:

    function load_field( $field ) {
    	$field = $this->my_sub_fields($field);
    	
    	return acf_field_repeater::load_field( $field );
    }

    my_sub_fields() is a new class function, that just gets all the configuration from a configuration file, that i create. this allows me to set up everything fast and easy in a file instead of using the backend acf configuration. the original way of using acf is nice for small input masks, but i have VERY complex input masks and i would need MUCH MORE time to set up my input masks. this works much faster with my configuration file.

    my – very own – problem is, that i need to set up EVERY variable, that could be used by acf. if you would use isset(), this would be not necessary.

    as i said – it is not a real issue and a very own problem of mine, but i think, that the acf code would be cleaner if a statement like if($variable) would be written like if(isset($variable)).
    it will make NO DIFFERENCE for the functionality, but it is the correct way to do an if statement in PHP.

  • Hi @dennis

    Thank you very much for your explanation 🙂

    In this case, could you please open a new ticket here: https://support.advancedcustomfields.com/new-ticket, so this topic can be passed directly to the plugin author?

    Thanks!

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

The topic ‘general use of variables in ACF’ is closed to new replies.