You are here

protected function YamlFormTestTrait::createYamlForm in YAML Form 8

Create a form with submissions.

Parameters

array|null $elements: (optional) Array of elements.

array $settings: (optional) Form settings.

Return value

\Drupal\yamlform\YamlFormInterface A form.

4 calls to YamlFormTestTrait::createYamlForm()
YamlFormPathTest::testPaths in src/Tests/YamlFormPathTest.php
Tests YAML page and title.
YamlFormSubmissionStorageTest::testSubmissionStorage in src/Tests/YamlFormSubmissionStorageTest.php
Test form submission storage.
YamlFormTestTrait::createYamlFormWithSubmissions in src/Tests/YamlFormTestTrait.php
Create a form with submissions.
YamlFormUiElementTest::testPermissions in modules/yamlform_ui/src/Tests/YamlFormUiElementTest.php
Tests permissions.

File

src/Tests/YamlFormTestTrait.php, line 235

Class

YamlFormTestTrait
Defines form test trait.

Namespace

Drupal\yamlform\Tests

Code

protected function createYamlForm($elements = NULL, array $settings = []) {
  if ($elements === NULL) {
    $elements = [
      'first_name' => [
        '#type' => 'textfield',
        '#title' => 'First name',
      ],
      'last_name' => [
        '#type' => 'textfield',
        '#title' => 'Last name',
      ],
      'sex' => [
        '#type' => 'select',
        '#title' => 'Sex',
        '#options' => 'gender',
      ],
      'dob' => [
        '#type' => 'date',
        '#title' => 'Date of birth',
        '#format' => 'l, F j, Y',
      ],
      'node' => [
        '#type' => 'entity_autocomplete',
        '#title' => 'Favorite node',
        '#target_type' => 'node',
      ],
      'colors' => [
        '#type' => 'checkboxes',
        '#title' => 'Flag colors',
        '#options' => [
          'red' => 'Red',
          'white' => 'White',
          'blue' => 'Blue',
        ],
      ],
      'likert' => [
        '#type' => 'likert',
        '#title' => 'Likert',
        '#questions' => [
          'q1' => 'Question 1',
          'q2' => 'Question 2',
          'q3' => 'Question 3',
        ],
        '#answers' => [
          '1' => 'Answer 1',
          '2' => 'Answer 2',
          '3' => 'Answer 3',
        ],
      ],
      'address' => [
        '#type' => 'yamlform_address',
        '#title' => 'Address',
      ],
    ];
  }

  // Create new form.
  $id = $this
    ->randomMachineName(8);
  $yamlform = YamlForm::create([
    'langcode' => 'en',
    'status' => TRUE,
    'id' => $id,
    'title' => $id,
    'elements' => Yaml::encode($elements),
    'settings' => $settings + YamlForm::getDefaultSettings(),
  ]);
  $yamlform
    ->save();
  return $yamlform;
}