I have a content type which contains field_state. The nodes of this content type store the two letter abbreviation for the state in each node using field_state. OH is Ohio, CA is California and so forth. I need to render this field with the full spelling of the state name. I have a PHP Array that contains all of the 50 mappings that I need like -- $state['CA'] = 'California'; This data structure is wrapped in a PHP function state_full_name($abbreviation).

I created a View which creates a block of everything I need -- except the full spelling of the state name. I know how to apply a PHP functions to a specific Views field in both D6 and D7. The solution in D8 has been elusive for me. I believe I need to use a Views plugin in some fashion. Can you help? Thanks!

1 Answer 1


A configurable or a base field? Either way, that's a perfect use case for using a list (string) field type, then you tell Drupal what the allowed values and its keys and labels are. Views will use that when displaying he field and everything will just work.

If you define your field in Code, instead of hardcoding the list in there, you can also specify a callback that will call your PHP function, in case you expect the list to change or if it is dynamic (allowed_values_function in the field storage settings). Although you can also turn that around and treat that field configuration as the canonical source for your information and if you need it in PHP, read it out from there.

If that is for some reason not an option, then you can implement a Views field Plugin and expose it to views using hook_views_data() hook. That's very likely a lot more complicated than using the right field Type for your use case, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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