0

I would like to add a submission handler to my form class so that the submitted values are saved. The parent class does not save them but only displays a message. I use Drupal 8! Here is my form class:

<?php

namespace Drupal\greeting\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class GreetingForm extends ConfigFormBase {

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

/**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form = parent::buildForm($form, $form_state);

    $config = $this->config('greeting.settings');

    $form['page_text'] = array(
      '#type' => 'textarea',
      '#title' => $this->t('Content of the Greeting page'),
      '#default_value' => $config->get('page_text'),
      '#description' => $this->t('Allows you to define the text of the Greeting page'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $config = $this->config('greeting.settings');

    $config->set('page_text', $form_state->getValue('page_text'));
    
    $config->save();

    return parent::submitForm($form, $form_state);
  }

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

}

$config = \Drupal::config('greeting.settings');
$page_title = $config->get('page_text');

How do I do it?

4
  • 1
    What does “save” mean here? Does your code not save the value to the active configuration?
    – cilefen
    May 8 at 22:34
  • 1
    The code you've appended at the end of your class will not show the value you've saved. If that's how you're determiningg it is not saving, you are incorrectly determining that. From all appearances, your class should save fine.
    – Jaypan
    May 9 at 2:50
  • @cilefen Yes, my code was already storing the value in the active configuration. May 9 at 18:35
  • @Jaypan Yes, I deleted the code at the end of the class. Thanks a lot! May 9 at 18:36

1 Answer 1

0

It looks like the problem reported in the question details have been resolved and the code in the question seems to be working.

However for anybody reaching this page trying to find an answer to the question in the title, here is the answer.

You can attach custom submit handlers to submit buttons in forms by setting the #submit property on the submit button.

$form['actions']['button_name'] = [
  '#type' => 'submit',
  '#value' => $this->t('Button Text'),
  '#submit' => ['::customSubmitHandler1', '::customSubmitHandler2'],
];

customSubmitHandler1 and customSubmitHandler2 would be functions defined inside your form class.

If instead you want to call a function defined in a module, you can add 'custom_module_submit_handler' which can be a function defined in a module.

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.