You are here

function dimension_field_instance_settings_form in Dimension 7

Implements hook_field_instance_settings_form().

File

./dimension.module, line 116

Code

function dimension_field_instance_settings_form($field, $instance) {
  $settings = drupal_array_merge_deep(_dimension_default_field_instance_settings(), $instance['settings']);
  list($has_length, $has_more) = _dimension_get_modes($field['settings']['mode']);
  $field_names = array(
    'height',
    'width',
    'length',
  );
  $columns = array(
    'label',
    'prefix',
    'suffix',
    'datatype',
    'decimals',
    'factor',
    'descr',
  );
  $form['#tree'] = TRUE;
  $form['fields'] = array(
    '#type' => 'container',
    '#prefix' => '<table><tr>
        <th>' . t('Dimension') . '</th>
        <th>' . t('Label') . '</th>
        <th>' . t('Prefix') . '</th>
        <th>' . t('Suffix') . '</th>
        <th>' . t('Data type') . '</th>
        <th>' . t('Decimals') . '</th>
        <th>' . t('Factor') . '</th>
        <th>' . t('Description') . '</th>
        </tr>',
    '#suffix' => '</table>',
  );
  $n = 0;
  foreach ($field_names as $field_name) {
    $n++;
    $form['fields'][$field_name] = array(
      '#type' => 'container',
      '#prefix' => '<tr><td>' . t('Dimension %n', array(
        '%n' => $n,
      )) . '</td>',
      '#suffix' => '</tr>',
      '#access' => $field_name == 'length' ? $has_length : $has_more,
    );
    foreach ($columns as $column) {
      if ($column == 'datatype') {
        $form['fields'][$field_name][$column] = array(
          '#type' => 'select',
          '#default_value' => $settings['fields'][$field_name][$column],
          '#options' => array(
            DIMENSION_DATATYPE_INTEGER => t('Integer'),
            DIMENSION_DATATYPE_DECIMAL => t('Decimal'),
          ),
          '#prefix' => '<td>',
          '#suffix' => '</td>',
        );
      }
      else {
        $form['fields'][$field_name][$column] = array(
          '#type' => 'textfield',
          '#default_value' => $settings['fields'][$field_name][$column],
          '#size' => 6,
          '#prefix' => '<td>',
          '#suffix' => '</td>',
        );
      }
    }
  }
  if ($has_more) {
    $form['dimension_unit'] = array(
      '#type' => 'textfield',
      '#title' => t('Unit for the !name', array(
        '!name' => $has_length ? t('volume') : t('area'),
      )),
      '#default_value' => empty($settings['dimension_unit']) ? '' : $settings['dimension_unit'],
      '#size' => 6,
    );
  }
  return $form;
}