function mathfield_form_afterbuild in Math Field 7
Afterbuild callbcak for forms with math expression fields.
1 string reference to 'mathfield_form_afterbuild'
- mathfield_field_widget_form in ./
mathfield.module - Implements hook_field_widget_form().
File
- ./
mathfield.module, line 287 - Adds a dynamic math expression field.
Code
function mathfield_form_afterbuild($form, &$form_state) {
$info = entity_get_info($form['#entity_type']);
$is_new = !empty($form['#entity']->{$info['entity keys']['id']});
if ($is_new || $form_state['rebuild'] || empty($form_state['mathfield'])) {
// Nothing to see here.
return $form;
}
// Order mathfield elements by dependency.
$elements = $form_state['mathfield'];
uasort($elements, '_mathfield_order_by_dependency');
// Evaluate math expression fields on initial form load for new entities.
foreach ($elements as $element) {
if ($element['#widget'] != 'mathfield_markup') {
// Don't override custom values.
$submitted_value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
if (is_numeric($submitted_value)) {
continue;
}
}
$expression = $element['#settings']['expression'];
$replacements = mathfield_get_token_values($element, $form_state);
$value = mathfield_evaluate($expression, $element['#settings'], $replacements);
if (is_numeric($value)) {
// Update the value of the mathfield.
$element['#value'] = $value;
// Update the value column and field prefix for readonly math
// expressions.
if ($element['#widget'] == 'mathfield_markup') {
$parents = $element['#array_parents'];
array_splice($parents, -1, 1, array(
'value',
));
$value_element = drupal_array_get_nested_value($form, $parents);
$value_element['#value'] = $value;
drupal_array_set_nested_value($form, $parents, $value_element, TRUE);
$element['#field_prefix'] = preg_replace('/<span class="mathfield-display">*?<\\/span>/i', '<span class="mathfield-display">' . check_plain($value) . '</span>', $element['#field_prefix']);
}
// Update the value in $form and $form_state.
drupal_array_set_nested_value($form, $element['#parents'], $element, TRUE);
mathfield_form_set_value($element, $value, $form_state);
}
}
return $form;
}