You are here

public function YamlFormSubmissionForm::form in YAML Form 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/YamlFormSubmissionForm.php, line 186

Class

YamlFormSubmissionForm
Provides a form to collect and edit submissions.

Namespace

Drupal\yamlform

Code

public function form(array $form, FormStateInterface $form_state) {

  // Check for a custom form, track it, and return it.
  if ($custom_form = $this
    ->getCustomForm($form, $form_state)) {
    $custom_form['#custom_form'] = TRUE;
    return $custom_form;
  }

  /* @var $yamlform_submission \Drupal\yamlform\YamlFormSubmissionInterface */
  $yamlform_submission = $this
    ->getEntity();
  $yamlform = $this
    ->getYamlForm();
  $form = parent::form($form, $form_state);

  /* Information */

  // Prepend form submission data using the default view without the data.
  if (!$yamlform_submission
    ->isNew() && !$yamlform_submission
    ->isDraft()) {
    $form['navigation'] = [
      '#theme' => 'yamlform_submission_navigation',
      '#yamlform_submission' => $yamlform_submission,
      '#weight' => -20,
    ];
    $form['information'] = [
      '#theme' => 'yamlform_submission_information',
      '#yamlform_submission' => $yamlform_submission,
      '#source_entity' => $this->sourceEntity,
      '#weight' => -19,
    ];
  }

  /* Data */

  // Get and prepopulate (via query string) submission data.
  $data = $yamlform_submission
    ->getData();
  $this
    ->prepopulateData($data);

  /* Elements */

  // Get form elements.
  $elements = $yamlform_submission
    ->getYamlForm()
    ->getElementsInitialized();

  // Populate form elements with form submission data.
  $this
    ->populateElements($elements, $data);

  // Prepare form elements.
  $this
    ->prepareElements($elements, $form, $form_state);

  // Add wizard progress tracker to the form.
  $current_page = $this
    ->getCurrentPage($form, $form_state);
  if ($current_page && $this
    ->getYamlFormSetting('wizard_progress_bar') || $this
    ->getYamlFormSetting('wizard_progress_pages') || $this
    ->getYamlFormSetting('wizard_progress_percentage')) {
    $form['progress'] = [
      '#theme' => 'yamlform_progress',
      '#yamlform' => $this
        ->getYamlForm(),
      '#current_page' => $current_page,
    ];
  }

  // Append elements to the form.
  $form['elements'] = $elements;

  // Pages: Set current wizard or preview page.
  $this
    ->displayCurrentPage($form, $form_state);

  /* Form  */

  // Move all $elements properties to the $form.
  $this
    ->setFormPropertiesFromElements($form, $elements);

  // Default: Add CSS and JS.
  // @see https://www.drupal.org/node/2274843#inline
  $form['#attached']['library'][] = 'yamlform/yamlform.form';

  // Assets: Add custom CSS and JS.
  // @see yamlform_css_alter()
  // @see yamlform_js_alter()
  $assets = [
    'css' => $yamlform
      ->getCss(),
    'javascript' => $yamlform
      ->getJavaScript(),
  ];
  foreach ($assets as $type => $value) {
    if ($value) {
      $form['#attached']['library'][] = "yamlform/yamlform.assets.{$type}";
      $form['#attached']['drupalSettings']['yamlform']['assets'][$type][$yamlform
        ->id()] = md5($value);
    }
  }

  // Attach disable back button.
  if ($this
    ->getYamlFormSetting('form_disable_back')) {
    $form['#attached']['library'][] = 'yamlform/yamlform.form.disable_back';
  }

  // Unsaved: Add unsaved message.
  if ($this
    ->getYamlFormSetting('form_unsaved')) {
    $form['#attributes']['class'][] = 'js-yamlform-unsaved';
    $form['#attached']['library'][] = 'yamlform/yamlform.form.unsaved';
    $current_page = $this
      ->getCurrentPage($form, $form_state);
    if ($current_page && $current_page != $this
      ->getFirstPage($form, $form_state)) {
      $form['#attributes']['data-yamlform-unsaved'] = TRUE;
    }
  }

  // Novalidate: Add novalidate attribute to form if client side validation disabled.
  if ($this
    ->getYamlFormSetting('form_novalidate')) {
    $form['#attributes']['novalidate'] = 'novalidate';
  }

  // Details toggle: Display collapse/expand all details link.
  if ($this
    ->getYamlFormSetting('form_details_toggle')) {
    $form['#attributes']['class'][] = 'yamlform-details-toggle';
    $form['#attached']['library'][] = 'yamlform/yamlform.element.details.toggle';
  }

  // Autofocus: Add autofocus class to form.
  if ($this->entity
    ->isNew() && $this
    ->getYamlFormSetting('form_autofocus')) {
    $form['#attributes']['class'][] = 'js-yamlform-autofocus';
  }

  // Details save: Attach details element save open/close library.
  // This ensures that the library will be loaded even if the form is
  // used as a block or a node.
  if ($this
    ->config('yamlform.settings')
    ->get('ui.details_save')) {
    $form['#attached']['library'][] = 'yamlform/yamlform.element.details.save';
  }

  // Pages: Disable form auto submit on enter for wizard form pages only.
  if ($this
    ->getPages($form, $form_state)) {
    $form['#attributes']['class'][] = 'js-yamlform-disable-autosubmit';
  }

  // Add #after_build callbacks.
  $form['#after_build'][] = '::afterBuild';
  return $form;
}