You are here

public function EditForm::buildForm in Form Defaults 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EditForm.php, line 15

Class

EditForm

Namespace

Drupal\Formdefaults\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $path_args = explode('/', current_path());
  $formid = $path_args[1];
  $fieldname = $path_args[2];

  // Load the form
  $data = formdefaults_getform($formid);
  $fields = array();
  $form['formid'] = array(
    '#type' => 'value',
    '#value' => $formid,
  );
  foreach ($data as $f => $field) {
    if (strpos($f, '#') !== 0) {
      $t = @$field['title'] ? ' - ' . @$field['title'] : '';
      $fields[$f] = Link::createFromRoute(t($f . $t), 'formdefaults.edit', [
        'formid' => $formid,
        'field' => urlencode($f),
      ]);
    }
  }
  $form['fields'] = array(
    '#type' => 'checkboxes',
    '#title' => 'Overriden Fields',
    '#options' => $fields,
  );
  $form['add'] = array(
    '#type' => 'fieldset',
    '#title' => 'Add Fields',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $types = array(
    'markup' => 'Markup',
    'fieldset' => 'Collapsed fieldset with markup ',
  );
  $form['add']['field_type'] = array(
    '#type' => 'select',
    '#title' => 'Type',
    '#options' => $types,
    '#description' => t('Choose Markup to add a place for instructions that are always seen.  Choose collapsed fieldset to add instructions inside an expandable box'),
  );

  // Weight of
  $weight_range = range(-50, 50);
  $weights = array(
    'unset' => 'unset',
  );
  foreach ($weight_range as $weight) {
    $weights[(string) $weight] = (string) $weight;
  }
  $form['add']['weight'] = array(
    '#type' => 'select',
    '#title' => 'Weight',
    '#options' => $weights,
    '#default_value' => -49,
    '#description' => 'Controls placement within the form, -49 is a good header value or 50 is usually a good footer value',
  );
  $form['add']['add_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Add',
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => 'Reset Selected',
  );
  return $form;
}