Score:0

How to add a submission handler to a form class on Drupal 8?

cn flag

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?

id flag
What does “save” mean here? Does your code not save the value to the active configuration?
Jaypan avatar
de flag
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.
cn flag
@cilefen Yes, my code was already storing the value in the active configuration.
cn flag
@Jaypan Yes, I deleted the code at the end of the class. Thanks a lot!
Score:0
hk flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.