You are here

public function HttpServiceApiPreviewForm::buildForm in HTTP Client Manager 8

Same name and namespace in other branches
  1. 8.2 src/Form/HttpServiceApiPreviewForm.php \Drupal\http_client_manager\Form\HttpServiceApiPreviewForm::buildForm()

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/HttpServiceApiPreviewForm.php, line 116

Class

HttpServiceApiPreviewForm
Class HttpServiceApiPreviewForm.

Namespace

Drupal\http_client_manager\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $serviceApi = $this->request
    ->get('serviceApi');
  $client = $this->httpClientFactory
    ->get($serviceApi);
  $commands = $client
    ->getCommands();
  ksort($commands);
  $form['search'] = [
    '#type' => 'select',
    '#options' => array_combine(array_keys($commands), array_keys($commands)),
    '#empty_option' => $this
      ->t('- All Commands -'),
    '#title' => $this
      ->t('Filter commands'),
    '#title_display' => 'invisible',
    '#ajax' => [
      'callback' => '::filterCommandsAjaxCallback',
      'wrapper' => 'service-commands-wrapper',
    ],
    '#required' => TRUE,
  ];
  $form['service_commands'] = [
    '#type' => 'vertical_tabs',
    '#prefix' => '<div id="service-commands-wrapper">',
    '#suffix' => '</div>',
  ];
  $header = [
    'name' => $this
      ->t('Name'),
    'type' => $this
      ->t('Type'),
    'default' => $this
      ->t('Default'),
    'description' => $this
      ->t('Description'),
    'required' => $this
      ->t('Required'),
    'location' => $this
      ->t('Location'),
  ];

  /** @var Operation $command */
  foreach ($commands as $commandName => $command) {
    $rows = [];

    /** @var Parameter $param */
    foreach ($command
      ->getParams() as $param) {
      $row = [
        'name' => $param
          ->getName(),
        'type' => $this
          ->getParameterType($param),
        'default' => $param
          ->getDefault(),
        'description' => $param
          ->getDescription(),
        'required' => $param
          ->getRequired() ? $this
          ->t('Yes') : $this
          ->t('No'),
        'location' => $param
          ->getLocation(),
      ];
      $rows[] = $row;
    }
    $form[$commandName] = [
      '#type' => 'details',
      '#title' => $this
        ->t($command
        ->getName()),
      '#description' => $this
        ->t($command
        ->getSummary()),
      '#group' => 'service_commands',
      '#access' => !$this
        ->isHiddenCommand($commandName, $form_state),
    ];
    $form[$commandName]['info'] = [
      '#type' => 'table',
      '#header' => [
        'method' => $this
          ->t('HTTP Method'),
        'uri' => $this
          ->t('URI'),
        'operations' => $this
          ->t('Operations'),
      ],
      '#rows' => [
        [
          $command
            ->getHttpMethod(),
          $command
            ->getUri(),
          [
            'data' => $this
              ->buildOperations($serviceApi, $commandName),
          ],
        ],
      ],
    ];
    $form[$commandName]['parameters'] = [
      '#type' => 'table',
      '#caption' => $this
        ->t('Parameters'),
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this
        ->t('There are no parameters for this command.'),
    ];
  }
  $form['#attached']['library'][] = 'http_client_manager/service.preview';
  return $form;
}