You are here

public function WSCallForm::form in Web Service Data 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/WSCallForm.php \Drupal\wsdata\Form\WSCallForm::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/WSCallForm.php, line 79

Class

WSCallForm
Class WSCallForm.

Namespace

Drupal\wsdata\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $wscall_entity = $this->entity;
  if (isset($wscall_entity->needSave) and $wscall_entity->needSave) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('You have unsaved changes.  Click save to save this entity.'));
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $wscall_entity
      ->label(),
    '#description' => $this
      ->t("Label for the Web Service Call."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $wscall_entity
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\wsdata\\Entity\\WSCall::load',
    ],
    '#disabled' => !$wscall_entity
      ->isNew(),
  ];
  $servers = $this->entityTypeManager
    ->getStorage('wsserver')
    ->loadMultiple();
  $options = [];
  foreach ($servers as $server) {
    $options[$server
      ->id()] = $server
      ->label() . ' (' . $server
      ->getEndpoint() . ')';
  }
  $form['wsserver'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Web Service Server'),
    '#description' => $this
      ->t('Data source.'),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $wscall_entity->wsserver,
    '#ajax' => [
      'callback' => '::wsserverForm',
      'wrapper' => 'wsserver-wrapper',
    ],
  ];
  $triggering = $form_state
    ->getTriggeringElement();
  if ($triggering['#id'] == 'wscall_new_method') {
    $values = $form_state
      ->getValues();
    $wscall_entity
      ->setMethod($values['add_method'], $values['new_method_name'], $values['new_method_path']);
  }

  /* Setting the form state in the options so that
     we can see values in the get options form. */
  $form_options = $options = $wscall_entity
    ->getOptions();
  $form_options['form_state'] = $form_state;
  $form['options'] = [
    '#id' => 'wsserver-wrapper',
    '#type' => 'container',
    'wsserveroptions' => $wscall_entity
      ->getOptionsForm($form_state
      ->getValue('wsserver'), $form_options),
  ];
  foreach ($options as $name => $option) {
    if (isset($form['options']['wsserveroptions'][$name])) {
      if (is_array($option)) {

        // Traverse down the options till we can build out the form structure.
        for ($i = 0; $i < count($option); $i++) {

          // I think this can be improved.
          foreach ($option[$i] as $options_key => $options_value) {
            $form['options']['wsserveroptions'][$name][$i][$options_key]['#default_value'] = $options_value;
          }
        }
      }
      else {
        $form['options']['wsserveroptions'][$name]['#default_value'] = $option;
      }
    }
  }
  $decoder_definitions = $this->decoderManager
    ->getDefinitions();
  $options = [
    '' => $this
      ->t('None'),
  ];
  foreach ($decoder_definitions as $key => $decoder) {
    $options[$key] = $decoder['label']
      ->render();
  }
  $form['wsdecoder'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Decoder'),
    '#description' => $this
      ->t('Decoder to decode the result.'),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $wscall_entity->wsdecoder,
  ];
  $encoder_definitions = $this->encoderManager
    ->getDefinitions();
  $options = [
    '' => $this
      ->t('None'),
  ];
  foreach ($encoder_definitions as $key => $encoder) {
    $options[$key] = $encoder['label']
      ->render();
  }
  $form['wsencoder'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Encoder'),
    '#description' => $this
      ->t('Encoder to encode the data sent to the web service.'),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $wscall_entity->wsencoder,
  ];
  if (!$this->moduleHandler
    ->moduleExists('wsdata_extras')) {
    $form['wsdecoder']['#description'] .= '  ' . $this
      ->t('Looking for more decoder plugins?  Try enabling the <em>wsdata_extras</em> module.');
    $form['wsencoder']['#description'] .= '  ' . $this
      ->t('Looking for more encoder plugins?  Try enabling the <em>wsdata_extras</em> module.');
  }
  return $form;
}