Get the display value

Daegaladh

Member
Hi, we can get the internal value of a custom field with {$acf_field} or $TMPL['acf_field'], but how to get the corresponding display value?
 

Mark

Administrator
Staff member
Not sure i understand what your after

Add phrase through the language manager? And then paste that tag in your template
 

Daegaladh

Member
no, I've created custom field named acf_country, a drop-down list for selecting country, and I got for example "es" as internal value and "Spain" for visible text.

with {$acf_country} I get the "es" value, but how can I get the "Spain" value?
 

Mark

Administrator
Staff member
I believe you would need to reassign this in your plugin.

1 method is to use a switch

PHP:
switch ($TMPL['acf_country']) {
    case 'es':
        $TMPL['acf_country_display'] = 'Spain';
        break;
    case 'de':
        $TMPL['acf_country_display'] = 'Germany';
        break;
    case 'ca':
        $TMPL['acf_country_display'] = 'Canada';
        break;
}
then you can call {$acf_country_display} as needed.


you could also query the database to rebuild this dynamically if you dont want to retype these relationships out. But it will cost a query
PHP:
SELECT field_choice_text FROM {$CONF['sql_prefix']}_join_fields WHERE field_choice_value = "{$TMPL['acf_country']}"
 

Basti

Administrator
Staff member
Yea this part could need an update. Uppon joining the internal value is used, as with a normal select box as well.
The admin side internal value is restricted to no special chars or spaces in an attempts to auto secure the select box on the frontend. Personally would like to keep it at that way, but indeed makes it hard to use the display text later for the member.

Easy fix would be to just use 1 box for the admin, and uppon joining maybe validate in a similar fashion as with the categories

We think of something to make it it easier, preferable without removing the second box, as that might really fu** some sites using it already.
Also involves quite some texting as its used by checkboxes and radio buttons as well. So iam not sure anything happens for that for 1.0
Properbly stick with what Mark posted for the time being
 

Daegaladh

Member
I believe you would need to reassign this in your plugin.

1 method is to use a switch

PHP:
switch ($TMPL['acf_country']) {
    case 'es':
        $TMPL['acf_country_display'] = 'Spain';
        break;
    case 'de':
        $TMPL['acf_country_display'] = 'Germany';
        break;
    case 'ca':
        $TMPL['acf_country_display'] = 'Canada';
        break;
}
then you can call {$acf_country_display} as needed.

Yeah I did it setting up an array, but is pretty much the same.
Anyways, it would be useful if you make something like $acfd_ for using the display parameter in future versions (acf for internal value and acfd for display value).
 

Basti

Administrator
Staff member
Yea that would be ideal. The hole function is really complicated to keep so much automated and in touch with other file and easy to use. Basicly no issue to create a vl_sites column for both them when checkbox/radio/select
But keeping it backward compatible might get complicated. But we will manage somehow
 

Mark

Administrator
Staff member
i think given the complexity of this and the few users who would need this that using a query or array or switch is just fine.
 

Basti

Administrator
Staff member
Just to let everyone know, this has been implemented into 1.0 as of today, so you can use both the internal and display text values in the templates
 
Top