0

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' => 'textfield',
            '#title' => $this->t('Facebook Username'),
            '#description' => $this->t('Enter your facebook username.'),
            'required' => FALSE,
        ];


        $form['actions']= [
            '#type' => 'actions',
        ];


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

        return $form;
    }

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


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


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

            $title =$form_state->getValue('facebook');
            drupal_set_message(t('You specified a title of %title', ['%title' => $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(&$vars){
    $vars['facebook'] = 'default';
}

But I'm not sure how to get my form to alter this variable.

1

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 &$form, FormStateInterface $form_state) {
    $this->config('social.settings')
      ->set('facebook', $form_state->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(&$variables) {
  $facebook = \Drupal::config('social.settings')->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.

  • I added your code but \Drupal::config('social.settings')->get('facebook', 'defaultvalue'); is still empty – Matt Jun 8 '16 at 19:51
  • I was able to get it to work by adding ->save() to $this->config('social.settings')->set('facebook', $form_state->getValue('facebook'))->save(); – Matt Jun 8 '16 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.