acf-builder icon indicating copy to clipboard operation
acf-builder copied to clipboard

File field returns ID

Open aitormendez opened this issue 5 years ago • 11 comments

Even with 'return_format' => 'array' or 'return_format' => 'url' the value returned is ID.

    ->addFile('pdf', [
        'label' => 'PDF',
        'return_format' => 'array',
    ])
$pdf = get_field('pdf');
var_dump($pdf)

/srv/www/superbiajuridico.es/current/web/app/uploads/cache/dc6ae7fac7abc76e90569c3927ffe06e183e0d69.php:22:string '1495' (length=4)

aitormendez avatar Mar 01 '20 08:03 aitormendez

What is the output from the entire FieldsGroupBuilder build function, what does the resulting configuration array look like?

According to the ACF docs, array is the default return value for this field type so I'm not sure why it is happening, or that it is an ACF Builder specific problem.

stevep avatar Mar 02 '20 14:03 stevep

This is a var_dump of the builder. It says 'return_format' => string 'array' for the element 1 of array (pdf field), but still returning ID.

If I make it the same field with de GUI interface from ACF, it return the array. With ACF-builder, just return ID.

This is the builder in context: https://github.com/aitormendez/superbiajuridico/blob/master/web/app/themes/sj/app/fields/sentencias.php

/srv/www/superbiajuridico.es/current/web/app/themes/sj/app/fields/sentencias.php:56:
object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
  protected 'config' => 
    array (size=2)
      'key' => string 'sentencias' (length=10)
      'title' => string 'Sentencias' (length=10)
  protected 'fieldManager' => 
    object(StoutLogic\AcfBuilder\FieldManager)[11353]
      private 'fields' => 
        array (size=6)
          0 => 
            object(StoutLogic\AcfBuilder\FieldBuilder)[11355]
              private 'type' => string 'text' (length=4)
              private 'config' => 
                array (size=4)
                  'name' => string 'nombre' (length=6)
                  'label' => string 'Nombre' (length=6)
                  'key' => string 'field_nombre' (length=12)
                  'instructions' => string 'Si la sentencia tiene un nombre descriptivo, hay que ponerlo aquí.' (length=67)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
          1 => 
            object(StoutLogic\AcfBuilder\FieldBuilder)[11356]
              private 'type' => string 'file' (length=4)
              private 'config' => 
                array (size=7)
                  'name' => string 'pdf' (length=3)
                  'label' => string 'PDF' (length=3)
                  'key' => string 'field_pdf' (length=9)
                  'instructions' => string 'Subir aquí el archivo PDF de esta sentencia.' (length=45)
                  'required' => int 1
                  'return_format' => string 'array' (length=5)
                  'library' => string 'all' (length=3)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
          2 => 
            object(StoutLogic\AcfBuilder\FieldBuilder)[11357]
              private 'type' => string 'url' (length=3)
              private 'config' => 
                array (size=5)
                  'name' => string 'url_externa' (length=11)
                  'label' => string 'URL externa' (length=11)
                  'key' => string 'field_url_externa' (length=17)
                  'instructions' => string 'Si la sentencia puede ser encontrada en Cendoj, poner aquí su dirección.' (length=74)
                  'wpml_cf_preferences' => string '0' (length=1)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
          3 => 
            object(StoutLogic\AcfBuilder\FieldBuilder)[11358]
              private 'type' => string 'number' (length=6)
              private 'config' => 
                array (size=5)
                  'name' => string 'sentencia_no' (length=12)
                  'label' => string 'Sentencia nº' (length=13)
                  'key' => string 'field_sentencia_no' (length=18)
                  'instructions' => string '' (length=0)
                  'wpml_cf_preferences' => string '0' (length=1)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
          4 => 
            object(StoutLogic\AcfBuilder\FieldBuilder)[11359]
              private 'type' => string 'date_picker' (length=11)
              private 'config' => 
                array (size=9)
                  'name' => string 'fecha' (length=5)
                  'label' => string 'Fecha' (length=5)
                  'key' => string 'field_fecha' (length=11)
                  'instructions' => string 'La fecha de la sentencia' (length=24)
                  'required' => int 1
                  'display_format' => string 'd/m/Y' (length=5)
                  'return_format' => string 'd/m/Y' (length=5)
                  'first_day' => int 1
                  'wpml_cf_preferences' => string '0' (length=1)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
          5 => 
            object(StoutLogic\AcfBuilder\ChoiceFieldBuilder)[11360]
              private 'choices' => 
                array (size=3)
                  'STS' => string 'Tribunal Supremo' (length=16)
                  'SAP' => string 'Audiencia Provincial' (length=20)
                  'JPI' => string 'Juzgado de Primera Instancia' (length=28)
              private 'type' (StoutLogic\AcfBuilder\FieldBuilder) => string 'radio' (length=5)
              private 'config' (StoutLogic\AcfBuilder\FieldBuilder) => 
                array (size=8)
                  'name' => string 'tribunal' (length=8)
                  'label' => string 'Tribunal' (length=8)
                  'key' => string 'field_tribunal' (length=14)
                  'instructions' => string 'Especificar aquí el tribunal que emite la sentencia.' (length=53)
                  'required' => int 1
                  'default_value' => string 'STS : Tribunal Supremo' (length=22)
                  'return_format' => string 'value' (length=5)
                  'wpml_cf_preferences' => string '0' (length=1)
              private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
                &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
  protected 'location' => 
    object(StoutLogic\AcfBuilder\LocationBuilder)[11354]
      private 'config' (StoutLogic\AcfBuilder\ConditionalBuilder) => 
        array (size=1)
          0 => 
            array (size=1)
              0 => 
                array (size=3)
                  'param' => string 'post_type' (length=9)
                  'operator' => string '==' (length=2)
                  'value' => string 'sentence' (length=8)
      private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => 
        &object(StoutLogic\AcfBuilder\FieldsBuilder)[11352]
  protected 'name' => string 'sentencias' (length=10)
  private 'parentContext' (StoutLogic\AcfBuilder\ParentDelegationBuilder) => null

aitormendez avatar Mar 02 '20 15:03 aitormendez

Can you do a print_r($builder->build()); replacing $builder with whatever the variable is named.

stevep avatar Mar 02 '20 15:03 stevep

ok, thank you for the indications.

Array
(
    [key] => group_sentencias
    [title] => Sentencias
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => nombre
                    [label] => Nombre
                    [key] => field_sentencias_nombre
                    [instructions] => Si la sentencia tiene un nombre descriptivo, hay que ponerlo aquí.
                )

            [1] => Array
                (
                    [type] => file
                    [name] => pdf
                    [label] => PDF
                    [key] => field_sentencias_pdf
                    [instructions] => Subir aquí el archivo PDF de esta sentencia.
                    [required] => 1
                    [return_format] => array
                    [library] => all
                )

            [2] => Array
                (
                    [type] => url
                    [name] => url_externa
                    [label] => URL externa
                    [key] => field_sentencias_url_externa
                    [instructions] => Si la sentencia puede ser encontrada en Cendoj, poner aquí su dirección.
                    [wpml_cf_preferences] => 0
                )

            [3] => Array
                (
                    [type] => number
                    [name] => sentencia_no
                    [label] => Sentencia nº
                    [key] => field_sentencias_sentencia_no
                    [instructions] => 
                    [wpml_cf_preferences] => 0
                )

            [4] => Array
                (
                    [type] => date_picker
                    [name] => fecha
                    [label] => Fecha
                    [key] => field_sentencias_fecha
                    [instructions] => La fecha de la sentencia
                    [required] => 1
                    [display_format] => d/m/Y
                    [return_format] => d/m/Y
                    [first_day] => 1
                    [wpml_cf_preferences] => 0
                )

            [5] => Array
                (
                    [choices] => Array
                        (
                            [STS] => Tribunal Supremo
                            [SAP] => Audiencia Provincial
                            [JPI] => Juzgado de Primera Instancia
                        )

                    [type] => radio
                    [name] => tribunal
                    [label] => Tribunal
                    [key] => field_sentencias_tribunal
                    [instructions] => Especificar aquí el tribunal que emite la sentencia.
                    [required] => 1
                    [default_value] => STS : Tribunal Supremo
                    [return_format] => value
                    [wpml_cf_preferences] => 0
                )

        )

    [location] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [param] => post_type
                            [operator] => ==
                            [value] => sentence
                        )

                )

        )

)

aitormendez avatar Mar 02 '20 16:03 aitormendez

This seems similar to other issues: #106, #81 and at this point I'm still not sure why. Can you export the JSON versions of the version you built with the ACF UI that does work?

stevep avatar Mar 02 '20 16:03 stevep

of course:

[
    {
        "key": "group_5b45a8de0c199",
        "title": "Sentencia",
        "fields": [
            {
                "key": "field_5b45a8f8384e4",
                "label": "PDF",
                "name": "pdf",
                "type": "file",
                "instructions": "Subir aquí el archivo PDF correspondiente a este contenido.",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "wpml_cf_preferences": 0,
                "return_format": "array",
                "library": "all",
                "min_size": "",
                "max_size": "",
                "mime_types": ""
            }
        ],
        "location": [
            [
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "sentence"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "top",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": true,
        "description": ""
    }
]

aitormendez avatar Mar 02 '20 16:03 aitormendez

Ok next question is when the field getting registered in code? Is it getting executed on the front end, before the ACF get_field function is called? Can you put some sort of debug code around it to test if it is getting called?

stevep avatar Mar 02 '20 16:03 stevep

I'm not sure about what do you mean exactly (I'm not much experienced developer). Anyway, I declare the fields in the App namespace:

https://github.com/aitormendez/superbiajuridico/tree/master/web/app/themes/sj/app/fields

Then, I get field from template. So, get_field is executed after field builder:

https://github.com/aitormendez/superbiajuridico/blob/master/web/app/themes/sj/resources/views/partials/content-sentence.blade.php#L8

In other hand, all other fields work fine.

Is this useful?

aitormendez avatar Mar 02 '20 16:03 aitormendez

Same issue here. Worth noting I'm using Log1x/acf-composer, but the issue is identical:

Even with 'return_format' => 'array' or 'return_format' => 'url' the value returned is ID.

print_r($builder->build()); shows the correct value, as above.

Using ACF Pro 5.12.1 (latest) if that helps.

dalepgrant avatar Mar 24 '22 03:03 dalepgrant

@dalepgrant After looking into it again, I think the cause is defining the fields in the init WordPress action. I was finally able to recreate this in a Sage theme. If I build the field inside a the init action like:

add_action('init', function() {
     $builder = new FieldsBuilder('hero');
     $builder->addFile('background_video');

     acf_add_local_field_group($builder->build());
});

get_field in my blade template will return the file id

var_dump(get_field('background_video')); // string(3) "446"

But if I take the field building outside of the init action

$builder = new FieldsBuilder('hero');
$builder->addFile('background_video');

acf_add_local_field_group($builder->build());

get_field will return an array array(23) { ["ID"]=> int(446) ["id"]=> int(446) ["title"]=> string(15).....

I think this is because get_field in the template is getting called before the init action fires, therefore the ACF field isn't defined yet.

Try moving the code outside the init action and see if that fixes the problem.

stevep avatar Mar 24 '22 14:03 stevep

So more specifically it has to do where the add_action('init', function or add_action('acf/init', function function is being defined, if you're using them.

In a Sage theme, if I move it directly into the setup.php file, it still works inside the init action correctly. (I had them in a Controller before, and defining them in an init action didn't work)

The main cause still is acf_add_local_field_group() getting called after get_field()

stevep avatar Mar 24 '22 14:03 stevep