You are here

class NodejsConfigSettingsForm in Node.js integration 8

Node.js server configuration form.

Hierarchy

Expanded class hierarchy of NodejsConfigSettingsForm

1 string reference to 'NodejsConfigSettingsForm'
nodejs_config.routing.yml in nodejs_config/nodejs_config.routing.yml
nodejs_config/nodejs_config.routing.yml

File

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

Namespace

Drupal\nodejs_config\Form
View source
class NodejsConfigSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'nodejs_config_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $main_config = $this->configFactory
      ->get('nodejs.config');
    $config = $this->configFactory
      ->get('nodejs_config.settings');
    $suggestion = $config
      ->get('js_suggestion');
    $file_path = drupal_get_path('module', 'nodejs') . '/nodejs.config.js';
    $config_path_message = $this
      ->t('<ol><li>Configure your server settings in the SETTINGS form below.</li><li>Click the <b>Save configuration</b> button.</li><li>Copy the suggested configuration to <b>!file</b>. You will need to do this step manually.</li></ol>', array(
      '!file' => $file_path,
    ));
    $form['config_suggestion'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Configuration builder'),
      '#description' => $config_path_message,
    );
    $form['config_suggestion']['nodejs_config_js'] = array(
      '#type' => 'textarea',
      '#title' => $this
        ->t('Suggested configuration:'),
      '#default_value' => $suggestion,
    );
    $form['config'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Settings'),
    );
    $form['config']['nodejs_config_host'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Host'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Specify the host name or IP address that the node.js server should listen on.
                                  Leave blank to listen for any host name. Otherwise, the server will only respond
                                  to names that match the IP address given (or resolved from the given name).'),
      '#default_value' => $config
        ->get('host'),
    );
    $form['config']['nodejs_config_port'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Port'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Specify the TCP port that the node.js server should listen on.'),
      '#default_value' => $config
        ->get('port'),
    );
    $form['config']['nodejs_server_scheme'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Protocol used by Node.js server'),
      '#default_value' => $config
        ->get('scheme'),
      '#options' => array(
        'http' => t('http'),
        'https' => t('https'),
      ),
      '#description' => $this
        ->t('The protocol used to communicate with the Node.js server.'),
    );
    $form['config']['nodejs_config_key'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('SSL key file'),
      '#required' => TRUE,
      '#description' => $this
        ->t('File system path to the key used for secure communication.'),
      '#default_value' => $config
        ->get('key'),
    );
    $form['config']['nodejs_config_cert'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('SSL certificate file'),
      '#required' => TRUE,
      '#description' => $this
        ->t('File system path to the certificate used for secure communication.'),
      '#default_value' => $config
        ->get('cert'),
    );
    $form['config']['nodejs_config_publish_url'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Publish URL'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Path on which the node.js server should accept messages from the Drupal site.'),
      '#default_value' => $config
        ->get('publish_url'),
    );
    $form['config']['nodejs_service_key'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Service key'),
      '#description' => $this
        ->t('An arbitrary string used as a secret between the node.js server and the Drupal site. The key can be changed in the Nodejs integration settings.'),
      '#default_value' => $main_config
        ->get('nodejs_service_key'),
      '#disabled' => TRUE,
    );
    $form['config']['nodejs_config_write_channels'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Client write to channels'),
      '#description' => $this
        ->t('Global flag that allows all channels to be written to by client sockets without
                                  going via the backend. Defaults to false.'),
      '#default_value' => $config
        ->get('write_channels'),
    );
    $form['config']['nodejs_config_write_clients'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Client write to clients'),
      '#description' => $this
        ->t('Global flag that allows all clients to be written to by client sockets
                                  without going via the backend. Defaults to false.'),
      '#default_value' => $config
        ->get('write_clients'),
    );
    $form['config']['backend'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Backend'),
    );
    $form['config']['backend']['nodejs_config_backend_host'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Host'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Host name of the Drupal site.'),
      '#default_value' => $config
        ->get('backend_host'),
    );
    $form['config']['backend']['nodejs_config_backend_port'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Port'),
      '#required' => TRUE,
      '#description' => $this
        ->t('TCP port of the server running the Drupal site. Usually 80.'),
      '#default_value' => $config
        ->get('backend_port'),
    );
    $form['config']['backend']['nodejs_config_backend_message_path'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Auth path'),
      '#description' => $this
        ->t('Path on which the Drupal site listens for authentication check requests.'),
      '#default_value' => $config
        ->get('backend_message_path'),
    );
    $form['config']['backend']['nodejs_config_debug'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Debug'),
      '#description' => $this
        ->t('Show debug information from the node.js process.'),
      '#default_value' => $config
        ->get('debug'),
    );
    $form['ext'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Extensions'),
      '#description' => $this
        ->t('An array of names of node.js modules that should be loaded as extensions to the
                                  node.js server.'),
    );
    $form['ext']['nodejs_config_extensions'] = array(
      '#type' => 'textarea',
      '#title' => $this
        ->t('Extension file paths'),
      '#description' => $this
        ->t('A list of node.js extension files paths, one per line.'),
      '#default_value' => $config
        ->get('extensions'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'nodejs_config.settings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
NodejsConfigSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
NodejsConfigSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
NodejsConfigSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NodejsConfigSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.