I installed this module an hour ago and I am still hopelessly lost. The readme file leads me to believe that somewhere along the way there is a checkbox labled "Display this Field." I cannot find it. Has it been removed?

If anybody out there wants to help me sting together two cck fields I would be grateful. I have two cck fields, one "distance" (decimal) and one "next point" (node reference).

For the display, I want to have something along the lines of "3.6km. to Montana." I can hack it together in the theme, but I hate hacking things together in the theme.

Comments

Eric_A’s picture

Component: Documentation » Code
Priority: Normal » Critical

EDIT: Seems that the evaluation of the display_format code is now dependent on the selected formatter. And there is another beautiful option as well! See below!

http://drupalcode.org/viewvc/drupal/contributions/modules/computed_field...

The checkbox is gone in beta 3. Unfortunately, the display_format field is still there as is the mentioned reference in the readme... This will lead to big usability issues, so this might be considered critical.

The reasoning is that one should use cck display formatters. If the available formatters do not do what you want you have these options:

1) code your own formatters if you need flexibility.
2) code your needs in a template file.
3) take care of the formatting contexts and values when computing the loaded value.

EDIT: Option 2 and 3 are no longer real options when you consider this:

Alternately, this code can be supplied by your own custom function named named @display_func().
/**
 * Theme function for 'plain' text field formatter.
 */
function theme_computed_field_formatter_plain($element) {
  $field = content_fields($element['#field_name']);
  // For "some" backwards compatibility
  $node_field_item['value'] = $element['#item']['value'];

  // Allow the value to be formated from code not stored in DB
  $display_func = 'computed_field_'. $field['field_name'] .'_display';
  if (function_exists($display_func)) {
    $display = $display_func($field, $element);
  }
  else {
    eval($field['display_format']);
  }
  return check_plain($display);
}
Eric_A’s picture

Issue settings back to what gomerpyle set.

@gomerpyle. Is my edited comment above enough for you to consider this fixed?

peterparker’s picture

Component: Code » Documentation
Priority: Critical » Normal

I have not had a chance to take a look at this solution just yet. The theme function part makes sense, and now that I know that the checkbox no longer exists thing have started to make more sense.

You can mark it fixed, even though I still cannot figure out how to string together two strings. That has nothing to do with the module, and more to do with my noggin.

Khai Heller’s picture

Title: "Display this Field" missing? plus a modest help request » Alternately, this code can be supplied by your own custom function named named....

Hi,

This is a great module!!!

I need some instructions about where to define my own custom function for this module?

Have created two functions that the module recognizes by showing me the following lines:
This field is computed using computed_field_field_computed_node_compute().
This field is computed using computed_field_field_computed_node_display().

How do I set the computed value from an external function?
Have tested the following 3 options without any success:

function computed_field_field_computed_node_compute(){
$node_field[0]['value'] = 'computed_field_field_computed_node_compute';
return 'computed_field_field_computed_node_compute';
return array( 0 => array('value' => 'computed_field_field_computed_node_compute'));
}

firfin’s picture

The reasoning is that one should use cck display formatters. If the available formatters do not do what you want you have these options:

How exactly do I do that? Formatter is the same as widget type (in the basic cck field settings) right?
The only widget type option I can select is "computed". Not selectbox (which I need, nor any other.) I have many widgets available when creating other cck-fields.

Also Alternately, this code can be supplied by your own custom function named named @display_func(). In my computed field settings page it is ... named computed_field_FIELDNAME_display()

So what exactly should the custom function be named? Also where should I define this function? In the "Display Format:" or the "Computed Code:" textarea? Or maybe in my template.php? Or custom module?

Finally what is the theme_computed_field_formatter_plain code meant to illustrate? It doesn't follow the @display_func nor the computed_field_fieldname_display() naming.

avibrazil@gmail.com’s picture

I also need a clue here.

Were to define this custom function? Which file?
What it should return?
How to bind the function with the field? Is it automatic or I have to explicitly call it in the field admin interface ?

Eric_A’s picture

The custom display function must be available when the theme layer is called. I put it in a module but template.php should be fine as well.

The name of the field should be in the function name. Look at the hints below the computed field textareas.

Your custom display function should return a (html) string value to be used for displaying your field.

The theme functions called by the CCK display formatters have this code:

  // Allow the value to be formated from code not stored in DB
  $display_func = 'computed_field_'. $field['field_name'] .'_display';
  if (function_exists($display_func)) {
    $display = $display_func($field, $element);
  }
  else {
    eval($field['display_format']);
  }

So your custom code looks like this:

function computed_field_MY_FIELD_display($field, $element) {
  // compute the display value.
  return $display_value;
}

The above is based on the situation at the time of my posts above. I have not tracked recent developments.

OliverColeman’s picture

It looks like the API has changed since the last post to:

computed_field_FIELD_NAME_compute(&$node, &$field, &$node_field)

hey_germano’s picture

How would you submit multiple fields? I've got these functions in a custom module:

function computed_field_field_nf_total_n_compute(&$node, &$field, &$node_field) {
  include_once('math.inc');
  $node_field[0]['value'] = $nitrogen;
}
function computed_field_field_nf_total_c_compute(&$node, &$field, &$node_field) {
  include_once('math.inc');
  $node_field[0]['value'] = $carbon;
}
function computed_field_field_nf_total_p_compute(&$node, &$field, &$node_field) {
  include_once('math.inc');
  $node_field[0]['value'] = $phosphorus;
}

However, when I re-save the node, only the value of the first calculated field (on the CCK "manage fields" page) gets saved to the database. I tested this by trying them in a different order - they all work, but only if they're first in line.

Any ideas on how to get them all to save? Could I get all of these into one function that's smarter?

EDIT: Turns out my issue isn't the functions above - moving them into the field settings in CCK yielded the same problem. I think my problem has more to do with this issue, which is about using computed fields with CCK multi-groups.

mmjvb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)