You are here

protected function TableConfigForm::fieldForm in Data 8

Helper function that generates a form snippet for defining a field.

formerly known as _data_ui_field_form().

Parameters

int $index: Index of column definition in table schema.

bool $required.:

1 call to TableConfigForm::fieldForm()
TableConfigForm::buildForm in src/Form/TableConfigForm.php
Form constructor.

File

src/Form/TableConfigForm.php, line 157

Class

TableConfigForm
Class TableConfigForm.

Namespace

Drupal\data\Form

Code

protected function fieldForm($index, $required = FALSE) {
  $defaults = array_fill_keys(array(
    'name',
    'label',
    'type',
    'size',
    'length',
    'unsigned',
    'index',
    'primary',
  ), '');
  $current = isset($this->entity->table_schema[$index]) ? $this->entity->table_schema[$index] : $defaults;
  $form = array();
  $form['#tree'] = TRUE;
  $form['name'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#required' => $required,
    '#default_value' => $current['name'],
  );
  $form['label'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $current['label'],
  );
  $form['type'] = array(
    '#type' => 'select',
    '#options' => data_get_field_types(),
    '#default_value' => $current['type'],
  );
  $form['size'] = array(
    '#type' => 'select',
    '#options' => data_get_field_sizes(),
    '#default_value' => $current['size'],
  );
  $form['length'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $current['length'],
    '#states' => array(
      'visible' => [
        [
          "#edit-table-schema-{$index}-type" => [
            'value' => 'varchar',
          ],
        ],
        [
          "#edit-table-schema-{$index}-type" => [
            'value' => 'char',
          ],
        ],
        [
          "#edit-table-schema-{$index}-type" => [
            'value' => 'text',
          ],
        ],
      ],
    ),
  );
  $form['unsigned'] = array(
    '#type' => 'checkbox',
    '#default_value' => $current['unsigned'],
  );
  $form['index'] = array(
    '#type' => 'checkbox',
    '#default_value' => $current['index'],
  );
  $form['primary'] = array(
    '#type' => 'checkbox',
    '#default_value' => $current['primary'],
  );
  return $form;
}