Creating custom settings/variables for site I created a custom Form from a module in Drupal 8. Here is the code:

class SocialSettingsForm extends FormBase {

    public function buildForm(array $form, FormStateInterface $form_state){
        $form['facebook'] = [
            '#type' =&gt; 'textfield',
            '#title' =&gt; $this-&gt;t('Facebook Username'),
            '#description' =&gt; $this-&gt;t('Enter your facebook username.'),
            'required' =&gt; FALSE,
        ];


        $form['actions']= [
            '#type' =&gt; 'actions',
        ];


        $form['actions']['submit'] = [
            '#type' =&gt; 'submit',
            '#value' =&gt; $this-&gt;t('Submit'),
        ];

        return $form;
    }

        public function getFormId(){
            return 'social_socialsettingsform';
        }


         public function validateForm(array &amp;$form, FormStateInterface $form_state){
            $facebook = $form_state-&gt;getValue('facebook');
            if (strlen($facebook) &lt; 5){
                // Set an error for the form element with a key of "title".
                $form_state-&gt;setErrorByName('facebook', $this-&gt;t('The username must be at least 5 characters long.'));
            }
         }


         public function submitForm(array &amp;$form, FormStateInterface $form_state) {

            $title =$form_state-&gt;getValue('facebook');
            drupal_set_message(t('You specified a title of %title', ['%title' =&gt; $title]));
         }

    }


The form works fine. But I'm not sure which direction and conventions to follow in order to make use of the user's input.

I would like to make use of the username the user enters, but I'm not sure how to store that input from the form. Ideally I would use these settings in a Twig template or a module.

UPDATE:
I have figured out how to add custom variables to my theme but I'm not sure how to get my form to access them. I was able to add

function mytheme_preprocess_page(&amp;$vars){
    $vars['facebook'] = 'default';
}


But I'm not sure how to get my form to alter this variable.
To save custom configuration you probably want to use ConfigFormBase, the code would look something like this.

class SocialSettingsForm extends ConfigFormBase {
  ...

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'social.settings', // The name of the config you want to edit
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $this-&gt;config('social.settings')
      -&gt;set('facebook', $form_state-&gt;getValue('facebook'));
  }
}


Then everywhere, you can use the config factory of the Drupal global object to get the config like this.

function some_preprocess_function(&amp;$variables) {
  $facebook = \Drupal::config('social.settings')-&gt;get('facebook', 'default_value_if_no_value_is_set');
}


Note the above approach is centered around how you would do it in a module, saving and getting configuration. You should also note that it's best practice to define the configuration a module provides in a yml file located in module_folder/config/schema/module_name.schema.yml. I'm not 100% sure what the file is actually used for, as it's possible to save configuration not defined in the config file and all other features seem to "work" just fine without it.