Dustin
Dustin

Reputation: 4459

Trying to understand the Drupal 7 computed fields module

I just installed the computed fields module and I'm a little lost as to how to use it. I have 2 fields in my content type: field_shortdesc and field_audio_description.

If field_shortdesc is blank I need to display field_audio_description and vice-versa.

What code do I need to place into the computed code and display code areas? Seems like I can't get even the simplest code to work.

For example if I put $entity_field[0]['value'] = "test"; in the computed area and then put print $entity_field[0]['value']; in the display code area nothing gets printed out. Obviously I'm doing something completely wrong.

Upvotes: 0

Views: 4057

Answers (3)

Giles B
Giles B

Reputation: 411

If you are talking about displaying the values in a view that uses fields, rather than viewing the node, there's an easier way to do this through the view settings, without adding another field.

If you only want to show one default value, even if both are present:

  • Add field 1 (the optional field) to the view but exclude from display
  • Add field 2 (the default field) to the view and edit the "No results behavior" to show the value of field 1 when field 2 is empty.

If you want to show both values if both are present, and you are using a table:

  • Add both fields to the view, but in table settings put the display of field 1 in the same column with field 2, using whatever separator is appropriate. If one field or the other is empty, views appears to be smart enough not to output the separator.

If not using a table, but want to show both values:

  • Add field 1 and exclude from display.
  • Add field 2 and edit the "Rewrite Results" settings to show both. Be sure that the "Hide rewriting if empty" box under "No results behavior" is not checked.

Upvotes: 0

nmc
nmc

Reputation: 8686

Note the help text in the Display Code section states:

This code should assign a string to the $display_output variable, which will be printed when the field is displayed.

Instead of print $entity_field[0]['value'] try $display_output = $entity_field[0]['value']

Upvotes: 1

vicky
vicky

Reputation: 1

I dont think computed fields module is not the right solutin for your requirement.

In my opinion using Ajax state system is the best choice.

http://colorado2010.camps.drupal.org/drupalcampcolorado.org/sessions/dynamic-forms-drupal-7-ajax-and-states.html

http://drupal.org/node/752056

Upvotes: 0

Related Questions