You are here

public function NodejsConfigSettingsForm::submitForm in Node.js integration 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

nodejs_config/src/Form/NodejsConfigSettingsForm.php, line 165

Class

NodejsConfigSettingsForm
Node.js server configuration form.

Namespace

Drupal\nodejs_config\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $main_config = $this->configFactory
    ->get('nodejs.config');
  $ext = $form_state
    ->getValue('nodejs_config_extensions');
  if ($ext == !NULL) {
    $ext = explode("\n", str_replace("\r", '', $ext));
  }
  $array = array(
    'scheme' => $form_state
      ->getValue('nodejs_server_scheme'),
    'host' => $form_state
      ->getValue('nodejs_config_host'),
    'port' => (int) $form_state
      ->getValue('nodejs_config_port'),
    'key' => $form_state
      ->getValue('nodejs_config_key'),
    'cert' => $form_state
      ->getValue('nodejs_config_cert'),
    'publishUrl' => $form_state
      ->getValue('nodejs_config_publish_url'),
    'serviceKey' => $main_config
      ->get('nodejs_service_key'),
    'backend' => array(
      'port' => (int) $form_state
        ->getValue('nodejs_config_backend_port'),
      'host' => $form_state
        ->getValue('nodejs_config_backend_host'),
      'messagePath' => $form_state
        ->getValue('nodejs_config_backend_message_path'),
    ),
    'clientsCanWriteToChannels' => (bool) $form_state
      ->getValue('nodejs_config_write_channels'),
    'clientsCanWriteToClients' => (bool) $form_state
      ->getValue('nodejs_config_write_clients'),
    'extensions' => $ext,
    'debug' => (bool) $form_state
      ->getValue('nodejs_config_debug'),
    'transports' => array(
      'websocket',
      'flashsocket',
      'htmlfile',
      'xhr-polling',
      'jsonp-polling',
    ),
    'jsMinification' => true,
    'jsEtag' => true,
    'logLevel' => 1,
  );
  $output = 'backendSettings = ' . Json::encode($array);
  $output = str_replace(array(
    '= {',
    ',',
    '}}',
    ':{',
    '\\/',
  ), array(
    "= {\n  ",
    ",\n  ",
    "\n  }\n}",
    ":{\n  ",
    '/',
  ), $output);
  $output = "/**\n* This configuration file was built using the 'Node.js server configuration builder'.\n* For a more fully commented example see the file nodejs.config.js.example in the root of this module\n*/\n" . $output . ';';
  $this->configFactory
    ->getEditable('nodejs_config.settings')
    ->set('js_suggestion', $output)
    ->set('host', $form_state
    ->getValue('nodejs_config_host'))
    ->set('port', (int) $form_state
    ->getValue('nodejs_config_port'))
    ->set('scheme', $form_state
    ->getValue('nodejs_server_scheme'))
    ->set('key', $form_state
    ->getValue('nodejs_config_key'))
    ->set('cert', $form_state
    ->getValue('nodejs_config_cert'))
    ->set('publish_url', $form_state
    ->getValue('nodejs_config_publish_url'))
    ->set('backend_port', (int) $form_state
    ->getValue('nodejs_config_backend_port'))
    ->set('backend_host', $form_state
    ->getValue('nodejs_config_backend_host'))
    ->set('backend_message_path', $form_state
    ->getValue('nodejs_config_backend_message_path'))
    ->set('write_channels', (bool) $form_state
    ->getValue('nodejs_config_write_channels'))
    ->set('write_clients', (bool) $form_state
    ->getValue('nodejs_config_write_clients'))
    ->set('debug', (bool) $form_state
    ->getValue('nodejs_config_debug'))
    ->set('extensions', $form_state
    ->getValue('nodejs_config_extensions'))
    ->save();
  parent::submitForm($form, $form_state);
}