You are here

function views_calc_field_form_item in Views Calc 7

Same name and namespace in other branches
  1. 5 views_calc.module \views_calc_field_form_item()
  2. 6.3 views_calc.module \views_calc_field_form_item()
  3. 6 views_calc.module \views_calc_field_form_item()

A form element for an individual calculated field.

1 call to views_calc_field_form_item()
views_calc_fields_form in ./views_calc.module
FAPI fields_form.

File

./views_calc.module, line 243
This module will allow you to add calculated fields to views tables and compute (SUM, COUNT, AVG, etc) columns of numeric data in a views table.

Code

function views_calc_field_form_item($i, $field, $substitutions) {
  if (empty($field)) {
    $field = new stdClass();
    $field->cid = 0;
    $field->label = '';
    $field->tablelist = '';
    $field->fieldlist = '';
    $field->calc = '';
    $field->format = '';
    $field->custom = '';
    $field->base = '';
  }
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'views') . '/includes/admin.inc';
  $options = array();
  $base_tables = views_fetch_base_tables();
  foreach ($base_tables as $base => $data) {
    $options[$base] = t($data['title']);
  }
  $form['group'][$i] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('Field: ') . !empty($field->label) ? $field->label : t('New'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['group'][$i]['cid'] = array(
    '#type' => 'hidden',
    '#value' => intval($field->cid),
  );
  $form['group'][$i]['tablelist'] = array(
    '#type' => 'hidden',
    '#value' => $field->tablelist,
  );
  $form['group'][$i]['base'] = array(
    '#type' => 'select',
    '#title' => t('Base table'),
    '#options' => $options,
    '#default_value' => !empty($field->base) && array_key_exists($field->base, $options) ? $field->base : 'node',
    '#description' => t('The base table for this field.'),
  );
  $form['group'][$i]['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#field_prefix' => 'ViewsCalc: ',
    '#default_value' => str_replace('ViewsCalc: ', '', $field->label),
    '#description' => t('The views field name for this field (i.e. Views Calc: My Calculation).'),
  );
  $form['group'][$i]['calc'] = array(
    '#type' => 'textarea',
    '#title' => t('Calculation'),
    '#default_value' => strtr($field->calc, $substitutions),
    '#description' => t("<p>The query operation to be performed, using numbers, field substitutions, and " . implode(' ', _views_calc_operators()) . ". Leave spaces between parentheses and field names, i.e. 'CONCAT( %field1, ' ', %field2 )'. <strong>" . t('Note that all fields must be from the base table selected above! You cannot combine fields from different base tables.') . "</strong></p>"),
  );
  $form['group'][$i]['format'] = array(
    '#type' => 'select',
    '#title' => t('Format'),
    '#default_value' => $field->format,
    '#options' => drupal_map_assoc(array_keys(_views_calc_format_options())),
    '#description' => t('The format of the result of this calculation.'),
  );
  $form['group'][$i]['custom'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom function'),
    '#default_value' => $field->custom,
    '#description' => t('The function to call for a custom format.'),
  );
  return $form;
}