0

For a content type with a decimal field with unlimited values (rows), how can I have another field in that same content type that shows the sum of all of the decimal values from the multi value field?

The goal is to keep a tally of the decimal values as the user enters them so they know the total before hitting save on the node.

When the node is saved, the final sum should also be saved in the database.

Modules used are Entity Api and Field Collection.

2

I was going to recommend researching the AJAX in hook_form_alter function(), but when I read that you were using Field Collections I got a little nervous.

The problem with Field Collections is they can be little finicky when you try and do more complex field "stuff" like the scenario you're proposing. I have yet to try using AJAX in combination with Field Collections and I definitely would not want to be the developer who was tasked with it.

However, if you feel like you want to tackle such a problem you are most certainly going to have create a custom module specific to your situation.

Below is the basic layout for a Node Form Alter using AJAX. Ultimately you want to add an AJAX Event function on your select list, so when the value changes it will call a function that adds the values together.

That's even before you factor in the Field Collections module, which is going to add another layer of complexity to the whole operation.

https://api.drupal.org/api/examples/ajax_example!ajax_example_node_form_alter.inc/7

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds two fields to the node form, second only appears after first is enabled.
 */
function ajax_example_form_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form['#node'];
  $form['ajax_example_1'] = array(
    '#type' => 'checkbox',
    '#title' => t('AJAX Example 1'),
    '#description' => t('Enable to show second field.'),
    '#default_value' => $node->ajax_example['example_1'],
    '#ajax' => array(
      'callback' => 'ajax_example_form_node_callback',
      'wrapper' => 'ajax-example-form-node',
      'effect' => 'fade',
    ),
  );
  $form['container'] = array(
    '#prefix' => '<div id="ajax-example-form-node">',
    '#suffix' => '</div>',
  );

  // If the state values exist and 'ajax_example_1' state value is 1 or
  // if the state values don't exist and 'example1' variable is 1 then
  // display the ajax_example_2 field.
  if (!empty($form_state['values']['ajax_example_1']) && $form_state['values']['ajax_example_1'] == 1
      || empty($form_state['values']) && $node->ajax_example['example_1']) {

    $form['container']['ajax_example_2'] = array(
      '#type' => 'textfield',
      '#title' => t('AJAX Example 2'),
      '#description' => t('AJAX Example 2'),
      '#default_value' => empty($form_state['values']['ajax_example_2']) ? $node->ajax_example['example_2'] : $form_state['values']['ajax_example_2'],
    );
  }
}

ajax['event']

Description: When this event occurs to this element, Drupal will perform an HTTP request in the background via Javascript.

Values: String. Possible values: Any valid jQuery event, including 'mousedown' (for submit, imagebutton, and button), 'blur' (for textfield and textarea), 'change' (for select). Note that #ajax['event'] does not need to be explicitly specified. Although it can be manually set, usually the default value will be sufficient.

0

There is a module to have field that are based on computation : Drupal Computed Field

See this page for a detailed explanation. You can set the computation code in a module which is great for maintenance and version control.

I have not used the module myself, but it seems used on a lot of sites so i should be quite stable.

Then for the "without page reload" thing, you could add a custom javascript to display/update the computation when the source field are changed. I don't think ajax is necessary as you don't need to save the information to the database live, right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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