I've created a computed field and it all works fine, but there is one thing that disconcerted me for a while: the field does not appear at all when you are in edit mode! So although I can see the result of the field when I have actually created the node, I can't see it while I am in the process of creating although I marked it to 'display' - which I would quite like to do. Is there any way around this?

Comments

Justin Freeman’s picture

Assigned: Unassigned » Justin Freeman
Status: Active » Closed (fixed)

Yes it is possible. See this tutorial HowTo: Theme a CCK input form

jibort’s picture

Component: User interface » Code

I found a workarround that worked for me.

1- Create a custom module o use and existing custom module
2- Implement "module_form_alter"
3- Write a code similar to

$value = $form['field_name_of_calculated_field'][0]['value']['#default_value'];
$form['testing'] = array('#value' => $value);

The last line of code creates a new element in the form of the type 'markup' (default type). I hope this help.

jordi

sansui’s picture

Rather old thread, but I am looking for a similar solution. I have an age computed field that I need a label/markup for in my edit form to allow for cck private field options.

I am able to display the value on my form with my age value and privacy option, but it is falling outside of the CCK fieldgroup/fieldset that it should be in - 'group_profile_personal'.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
	$value = $form['field_profile_age'][0]['value']['#default_value'];
	$form['node_form']['group_profile_personal']['field_profile_age'] = array(
	'#prefix' => '<div id="edit-field-profile-age-value-wrapper">',
        '#title' => t('Age'),	
	'#value' => $value,
	'#suffix' => '</div>',
	);
}
clintthayer’s picture

For some reason I'm still having issues with displaying a CCK computed field on the edit form. Don't need to edit the field, just display. If folks have example code to point me in the right direction - that would be helpful.

Anonymous’s picture

I found a way to solve this problem //to display a field in edit mode :

<?php
/**
* Implementation of hook_form_alter
*/
function my_module_form_alter(&$form, $form_state, $form_id) {
    $form['#after_build'][] = 'my_module_cck_alter';
}

/**
* #after_build function to modify CCK fields
*/
function my_module_cck_alter($form, &$form_state) {
  $cck_fields = array('field_one', 'field_two')
  $field_to_modify = my_module_cck_walker($form, $cck_fields);
  $field_to_modify[0]['value']['#type'] = 'fieldset';
  $field_to_modify[0]['value']['#title'] = t('Insert pretty title here');
  return $form;
}

/**
* Recursively walk down the $form array looking for the CCK elements to modify
* @param $array array
*     the $form array or piece of the form array when called recursivly (must be passed by reference)
* @param $cck_fields array
*    array of cck fields to mark as read only
* @return
*    All modification are made by reference.
*/
function _my_module_cck_walker(&$array, &$cck_fields) {
  $field = null;
  
  foreach (element_children($elements) as $key) {
    if (isset($elements[$key]) && $elements[$key]) {
      if ($key == $field_name) {
        $field = &$elements[$key];
      }
      elseif (strpos($key, 'group_') === 0) {
        $field = &_my_module_cck_walker($elements[$key], $field_name);
      }
    }
    if ($field != null) {
      break;
    }
  }
  return $field;
}
?>
clintthayer’s picture

Thanks franck0015. I went a different direction by adding an additional CCK field and dumping the result of the computed field in. I'll keep this solution in mind if I need something more.

c

El Bandito’s picture

@clintthayer

Any chance you could share your implementation method ?

Thanks

El B

agileadam’s picture

Here's another method of showing the value of the computed field in the form. This illustrates how to add a markup field to the form that will show the user the current value or some placeholder text if they're creating a new node.

Note that this field will be displayed as HTML, so it's just to show the user the current value. It will not be editable.

This would go in a hook_form_alter().

case 'courseware_node_form':
  // Show the computed field "Courseware Total" in the node add/edit form
  $calculated_cost = '';
  if ($form['#node']->field_cware_calc_total[0]['value']) { 
    $calculated_cost = uc_currency_format($form['#node']->field_cware_calc_total[0]['value']);
  } 

  $help_text = t('This field is automatically re-calculated every time this form is submitted.') . '<br />';
  $help_text .= t('The value will be: <strong>customer cost + (customer cost * (tax rate / 100)) + shipping.</strong>');
  $calc_total_html = '<label for="calculated-total-display">' . t('Courseware Total (auto-calculated):') . '</label>';
  $calc_total_html .= '<div id="calculated-total-display-0-value" class="computed">' . $calculated_cost  . '</div>';
  $calc_total_html .= '<div class="description">' . $help_text . '</div>';
  $form['calc_total_html'] = array( 
    '#type' => 'markup',
    '#prefix' => '<div class="form-item" id="edit-field-calculated-total-display-0-wrapper">',
    '#value' => $calc_total_html,
    '#suffix' => '</div>',
    '#weight' => $form['field_cware_calc_total']['#weight'],
  );
  break;
deviato’s picture

Here's my method for showing a computed field in a cck multigroup:

<?php
function _myform_after_build($form, &$form_state) {
   $ni=count($form['#node']->field_name);  //use a normal field in the multigroup, it's used to count the istances
   for($i=0;$i<$ni;$i++) {
      //If the computed field is in the multigroup you can reuse it, with unchanged weight, using this:
      $form[group_fieldname][$i][field_computed][value]['#type']='textfield';
      $form[group_fieldname][$i][field_computed][value]['#title']=$form[group_fieldname][$i][field_computed]['#title'];
      $form[group_fieldname][$i][field_computed][value]['#attributes']['readonly'] = 'readonly';
      $form[group_fieldname][$i][field_computed][value]['#value']=$form[group_fieldname][$i][field_computed][0][value]['#value'];  //or #default_value
      //or in alternative
      $form[group_fieldname][$i][field_computed][value]['#value']=$form[field_computed][$i][value]['#default_value'];

      //If the computed field is outside the multigroup, you can use a new field, similar to above replies, adding it to multigroup part:
      $form[group_fieldname][$i][field_newfield][value]['#type']='textfield';
      $form[group_fieldname][$i][field_newfield][value]['#value']=$form[field_computed][0][value]['#default_value'];
      $form[group_fieldname][$i][field_newfield][value]['#title']=$form[field_computed]['#title'];
   }
}
?>
akolahi’s picture

Regarding #6 & #7, you can use rules to assign the value of the computed field to a dummy text field. Furthermore you can make the field non-editable using conditional fields. This in effect will show the value of the computed field on the node/edit form.

Note that this will only work after the initial save and wont update on the fly (as should probably be the behavior).

Also, the form alter method is probably a better solution in that you don't need an extra field - however this solution is super easy to implement without a custom module.

akolahi’s picture

This module will also do the trick. Just select 'read only' and it will show the view version of the field: https://drupal.org/project/field_extrawidgets