You are here

public function HttpConfigRequestForm::form in HTTP Client Manager 8

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

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/HttpConfigRequestForm.php, line 75

Class

HttpConfigRequestForm
Class HttpConfigRequestForm.

Namespace

Drupal\http_client_manager\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $serviceApi = $this->request
    ->get('serviceApi');
  $commandName = $this->request
    ->get('commandName');
  $http_config_request = $this->entity;
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $http_config_request
      ->label(),
    '#description' => $this
      ->t("Label for the Http Config Request."),
    '#required' => TRUE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $http_config_request
      ->id(),
    '#machine_name' => array(
      'exists' => '\\Drupal\\http_client_manager\\Entity\\HttpConfigRequest::load',
    ),
    '#disabled' => !$http_config_request
      ->isNew(),
  );
  $form['service_api'] = [
    '#type' => 'value',
    '#value' => $serviceApi,
  ];
  $form['command_name'] = [
    '#type' => 'value',
    '#value' => $commandName,
  ];
  $form['parameters'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('parameters'),
    '#tree' => TRUE,
  ];
  $client = $this->httpClientFactory
    ->get($serviceApi);
  $parameters = $http_config_request
    ->get('parameters');
  foreach ($client
    ->getCommand($commandName)
    ->getParams() as $param) {
    $name = $param
      ->getName();
    $form['parameters'][$name] = [
      '#command_param' => $param,
      '#title' => $this
        ->t($name),
      '#type' => 'textarea',
      '#rows' => 1,
      '#required' => $param
        ->getRequired(),
      '#default_value' => $parameters[$name] ? $parameters[$name] : $param
        ->getDefault(),
      '#description' => $param
        ->getDescription() ? $this
        ->t($param
        ->getDescription()) : '',
    ];
    switch ($param
      ->getType()) {
      case 'integer':
        $form['parameters'][$name]['#type'] = 'number';
        $form['parameters'][$name]['#value_callback'] = [
          $this,
          'integerValue',
        ];
        break;
      case 'array':
        $form['parameters'][$name]['#type'] = 'textarea';
        $form['parameters'][$name]['#rows'] = 3;
        $placeholder = $this
          ->t('Enter a list of values (one value per row).');
        $form['parameters'][$name]['#attributes']['placeholder'] = $placeholder;
        $form['parameters'][$name]['#value_callback'] = [
          $this,
          'arrayValue',
        ];
        break;
    }
  }

  // Show the token help.
  $form['token_help'] = array(
    '#theme' => 'token_tree_link',
  );
  return $form;
}