You are here

public function CircleV2::frontEndEnvironmentForm in Build Hooks 3.x

Same name and namespace in other branches
  1. 8.2 modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleV2.php \Drupal\build_hooks_circleci\Plugin\FrontendEnvironment\CircleV2::frontEndEnvironmentForm()

Overrides FrontendEnvironmentBase::frontEndEnvironmentForm

File

modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleV2.php, line 92

Class

CircleV2
Defines a circle v2 environment.

Namespace

Drupal\build_hooks_circleci\Plugin\FrontendEnvironment

Code

public function frontEndEnvironmentForm($form, FormStateInterface $form_state) {
  $build = parent::frontEndEnvironmentForm($form, $form_state);
  $build['token'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token'),
    '#default_value' => $this->configuration['token'],
    '#required' => TRUE,
  ];
  $build['project'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Project'),
    '#default_value' => $this->configuration['project'],
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter your project in the form organisation/repository'),
  ];
  $build['type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Reference type'),
    '#default_value' => $this->configuration['type'],
    '#options' => [
      'branch' => $this
        ->t('Branch'),
      'tag' => $this
        ->t('Tag'),
    ],
  ];
  $build['reference'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Branch / Tag'),
    '#default_value' => $this->configuration['reference'],
    '#required' => TRUE,
  ];
  $build['parameters'] = [
    '#caption' => $this
      ->t('Parameters'),
    '#prefix' => '<div id="parameters-add-more">',
    '#suffix' => '</div>',
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Value'),
      $this
        ->t('Remove'),
    ],
  ];
  $parameters = $form_state
    ->getCompleteFormState()
    ->getValue([
    'settings',
    'parameters',
  ], $this->configuration['parameters'] ?: [
    [
      'name' => '',
      'type' => 'string',
      'value' => '',
    ],
  ]);
  foreach ($parameters as $ix => $parameter) {
    $build['parameters'][$ix] = [
      'name' => [
        '#type' => 'textfield',
        '#title_display' => 'invisible',
        '#title' => $this
          ->t('Name<span class="visually-hidden"> of parameter @ix</span>', [
          '@ix' => $ix + 1,
        ]),
        '#default_value' => $parameter['name'],
      ],
      'type' => [
        '#type' => 'select',
        '#title_display' => 'invisible',
        '#title' => $this
          ->t('Type<span class="visually-hidden"> of parameter @ix</span>', [
          '@ix' => $ix + 1,
        ]),
        '#options' => [
          'string' => $this
            ->t('String'),
          'boolean' => $this
            ->t('Boolean'),
          'integer' => $this
            ->t('Integer'),
        ],
        '#default_value' => $parameter['type'],
      ],
      'value' => [
        '#type' => 'textfield',
        '#title_display' => 'invisible',
        '#title' => $this
          ->t('Value<span class="visually-hidden"> of parameter @ix</span>', [
          '@ix' => $ix + 1,
        ]),
        '#default_value' => $parameter['value'],
      ],
      'remove' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Remove %label', [
          '%label' => $parameter['name'] ?: $this
            ->t('item @ix', [
            '@ix' => $ix + 1,
          ]),
        ]),
        '#submit' => [
          [
            static::class,
            'removeParameterSubmit',
          ],
        ],
        '#ajax' => [
          'callback' => [
            static::class,
            'updateForm',
          ],
          'wrapper' => 'parameters-add-more',
          'effect' => 'fade',
          'method' => 'replaceWith',
        ],
      ],
    ];
  }
  $build['add_another'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another parameter'),
    '#submit' => [
      [
        static::class,
        'addMoreSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        static::class,
        'updateForm',
      ],
      'wrapper' => 'parameters-add-more',
      'effect' => 'fade',
      'method' => 'replaceWith',
    ],
  ];
  $build['help'] = [
    '#markup' => $this
      ->t('<p>Parameters to send to CircleCI. To send a value of False for a boolean, enter 0.</p>'),
  ];
  return $build;
}