Support

Account

Home Forums Bug Reports acf/prepare_field incomplete?

Solving

acf/prepare_field incomplete?

    • alicam

    • February 6, 2019 at 9:32 am

    I’m testing using this code:

    
    function my_acf_prepare_field( $field ) {
      $field['class'] .= 'form-control';
      $field['wrapper']['class'] .= 'wrapper-form-control';
      return $field;
    }
    add_filter('acf/prepare_field', 'my_acf_prepare_field');
    

    On a date picker field, it doesn’t add either of those classes to the html output.

    Does someone have the definitive word on where and when classes are added, and when they’re not?!

    Is this a bug or is there a reason why the input of the date picker doesn’t get the class applied?

    And what of all the other field types?

    • alicam

    • February 6, 2019 at 9:45 am

    I’m looking at ACF Pro > includes > fields > class-acf-field-date_picker.php:

    
    // elements
    		$div = array(
    			'class'					=> 'acf-date-picker acf-input-wrap',
    			'data-date_format'		=> acf_convert_date_to_js($field['display_format']),
    			'data-first_day'		=> $field['first_day'],
    		);
    		$hidden_input = array(
    			'id'					=> $field['id'],
    			'name'					=> $field['name'],
    			'value'					=> $hidden_value,
    		);
    		$text_input = array(
    			'class' 				=> 'input',
    			'value'					=> $display_value,
    		);
    

    Why can we not change the 3rd last line there to:

    
    			'class' 				=> $field['class'] . ' input',
    

    ?

    And do this consistently for all other field types?

    For me, this is about applying Bootstrap 4 styles to the correct field elements for front-end forms. I can’t, because I can’t get to the input element to add a class, unless I make ACF (Pro) consistent in this way…

    Is this a missing bit of code, or deliberate??

    A

  • The only thing that ACF gives you the ability to add is a wrapper class on the field container

    
    <div clas"acf-field acf-field-date-picker ... my-custom-class"...
    

    This is where the value of $field['wrapper']['class'] will go.

    There has never been any ability to add or alter classes on any of the nested elements of a field, so I’d say this is by design.

    • alicam

    • February 8, 2019 at 9:19 am

    I think this needs some more thought, John. Honestly.

    A basic ACF text field DOES get the $field[‘class’] attribute applied to its class.

    But the input of a datepicker field is ignored (as I show above).

    Can we not, at least, aim for simple consistency?

    $field[‘class’] should always be available, and should always be applied to the visible element, so that things like Bootstrap classes can be applied easily.

    To my mind this is sensible, and a fairly trivial update to the code. It’s very little work to do it, and a big win for consistency and usefulness.

    A

  • What you have done is found something that is an internal ACF thing $field[‘class’]. Yes, you are correct, if you add something to this during prepare field it will be added to a basic fields class attribute. It may work with other fields as well, I don’t know.

    It does not work on the date picker field because that value is not used. A date picker field is a jquery date picker and it’s made up of multiple fields and ACF applies different classes to each field that’s needed and the jquery date picker code takes if from there.

    I think this is something that was not intended for public use, I would not use it in this manner because it is not consistently used.

    As far as updating the code, you’d need to take that up with the developer https://www.advancedcustomfields.com/contact/. I am just another user trying to help out.

Viewing 5 posts - 1 through 5 (of 5 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.