0

I think I have a problem with keys and values of a list field in the rendering phase (on Drupal 7).

Let's me go in detail.

Model part:

  • We defined a CCK "double field" for a "profile2", with multiple values allowed.
  • This field is composed by a select and a text-field, but I'm interested only to the "select" part in this moment (its behavior seems to be the same of a simple list field).
  • Then we defined values in the list as K1|V1, K2|V2, etc.

Render part:

  • We defined a theme template for this field; there we verifies values by comparing the key: $val = $item['first'] (to take it) and if ($val=="K1") {...}... and everithing works fine, because tamplate receives keys of the list (inside first key of the #object array of the renderable array).
  • In a node page I have to show the same information and I would like to use the same template file (instead of replicate the code). So I tried to use render( ) Drupal function, and it needs for a renderable array... but the only function I found to get it was field_view_field( )... and it doesn't work as I expect.

The problem is that this function doesn't return field keys (e.g. K1), but values (e.g. V1)... so templates is not able to work properly.

I know I can write differently my template (e.g. if ($val=="K1" || $val=="V1"), but I think this is can't be the right solution, because code should alway depend from keys (and not values, that may change over time).

So I'm wondering what is the Drupal way to achieve a renderable array in which there is the keys and not the values of a list element.

Thank you

1

Try to change format for that field in 'Manage display' in back-office. 'key' option should be there. This option available for select list, but for "double field" try to set "unformatted list", style "simple", and text format "-none-".

  • Thanks for your suggestion: it was useful to find my solution. In the case of list I think you are correct... but having a "double field" I had to set "unformatted list", style "simple", and text format "-none-" to achieve what I need. Please, update answer adding this information, so I can accept it. – Pierpaolo Cira Mar 24 '16 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.