You are here

function units_unit_form in Units of Measurement 7

Same name and namespace in other branches
  1. 7.2 units_ui.pages.inc \units_unit_form()

Generate editing form for entity type 'units_unit'.

File

./units_ui.pages.inc, line 107
Menu page callbacks for Units UI module.

Code

function units_unit_form($form, &$form_state, $entity, $op = 'edit') {
  if ($op == 'clone') {
    $entity->label .= ' (cloned)';
  }
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#required' => TRUE,
    '#description' => t('Provide human readable label of this measurement unit.'),
    '#default_value' => isset($entity->label) ? $entity->label : NULL,
  );
  $form['machine_name'] = array(
    '#type' => 'machine_name',
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'units_unit_machine_name_load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this measurement unit. It must only contain lowercase letters, numbers, and underscores.'),
    '#default_value' => isset($entity->machine_name) ? $entity->machine_name : NULL,
  );
  $form['symbol'] = array(
    '#type' => 'textfield',
    '#title' => t('Symbol'),
    '#description' => t('Provide human readable symbol of this measurement unit.'),
    '#default_value' => isset($entity->symbol) ? $entity->symbol : NULL,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('Provide description of this measurement unit.'),
    '#default_value' => isset($entity->description) ? $entity->description : NULL,
  );
  $form['factor'] = array(
    '#type' => 'textfield',
    '#title' => t('Factor'),
    '#description' => t('Provide numeric factor (coefficient) multiplying by which value from this measurement unit will be converted into the SI unit (or any other standard unit) of this measure.'),
    '#default_value' => isset($entity->factor) ? $entity->factor : 0,
    '#element_validate' => array(
      'element_validate_number',
    ),
    '#required' => TRUE,
  );
  field_attach_form('units_unit', $entity, $form, $form_state);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save unit'),
  );
  if ($op != 'add' && $op != 'clone') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete unit'),
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'units_unit_form_delete_submit',
      ),
    );
  }
  return $form;
}