The list of code snippets is great, especially for a rank non-coder like myself. But I did not spot the simple case of two text fields that want to be concatenated to form a new single text field.
Straightforward example is:
A: $entity_field_lastname['value']
B: $entity_field_firstname['value']

And we want to have a new field:
A & B: $entity_field_fullname['value']

With a "space" between the two field values (of course!)

Could someone please set me straight on this?

Comments

deggertsen’s picture

I'm trying to get help with this as well. Did you ever figure it out?

deggertsen’s picture

Status: Active » Fixed

I was able to do what I was trying to do with this code.

In the computed code I did this. In my use case I have a node with the title as a last name and another field with the first name. I wanted a hidden field that I could use in views and faceted search that would automatically combine the two. It took me awhile to figure out how to get the title (or last name) to display correctly, but this did it. Notice the ' ' for the space in between the two values.

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_speaker_first_name'))) . ' ' . $entity->title;

Then, in order to display HTML special characters such as &, I put this function into a custom module.

function computed_field_field_full_name_display($field, $entity_field_item, $entity_lang, $langcode) {
  return htmlspecialchars_decode($entity_field_item['value']);
}

Hope that helps someone.

boabjohn’s picture

Status: Fixed » Active

Thanks for the input @deggersten but this is not our usecase or solution envelope (ie, no use of custom module should be required for such a trivial exercise).
Could anyone please spare a moment to drop a line of PHP on this issue?
Thanks indeed.

deggertsen’s picture

The only reason the custom module was needed is because I didn't want special characters coming out as $amp; and the like. If you don't care about that no custom module is needed.

rudyard55’s picture

So...

What's the answer?

To not require a custom module, what gets put in the "Display Code (PHP)" box?

boabjohn’s picture

Good questyion! I've been doing a pythin course over at codecademy...maybe I should dip into the PHP offering and see if there's a quick answer. It has to be totally trivial (for those who know what they are doing!) but fo rthe rest of us it's a showstopper. That's why I wanted to get it into the list of code snippets. That was 9 months ago! still no code. sigh.

rudyard55’s picture

Here's what I've figured out...

In the code section use

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_lastname'))) . ' ' . array_pop(array_pop(field_get_items($entity_type, $entity, 'field_firstname')));

and in the display section use:

$display_output = $entity_field_item['value'];

My scenario required a little different code because my second field came from a term reference field. But I believe the above will work in your specific scenario.

MatthijsG’s picture

This worked for me. When you're looking for the correct fieldname, use the name used in the row 'system name' on the page admin/structure/types/manage/YOURCONTENTTYPE/fields.

Now find out how to use the date to combine with the text ..

DrupalRanger’s picture

I know this post is old but the above solution throws a non critical error. The below solution I found to work.

Computed Code
$field_a = field_get_items($entity_type, $entity, 'field_first_name');
$field_b = field_get_items($entity_type, $entity, 'field_last_name');
$entity_field[0]['value'] = $field_a[0]['value'] . " " . $field_b[0]['value'];

Display Code
$display_output = $entity_field_item['value'];