Process the Block Config Form
Add the following method to the HelloBlock class. In this example, it is located in the src/Plugin/Block/HelloBlock.php file, but as you start thinking in a more OOP fashion, where it is physically located in the file structure is less important than where it is in the namespace. If you are a very clever OO programmer, you keep the two things very tightly aligned. But just in case, it is actually the namespace, very much like the Module folder name vs. machine name for our module discussion earlier in this section, that will be important later on for when you want to programmatically interact with your module's code.
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['hello_block_name'] = $form_state->getValue('hello_block_name');
}
If you have a fieldset wrapper around your form elements then you should pass an array to the getValue() function, instead of passing the field name alone. Here myfieldset is the fieldset which is wrapping the hello_block_name field.
$this->configuration['hello_block_name'] = $form_state->getValue(['myfieldset', 'hello_block_name']);
Adding this code will mean that the form will process, and the input to the form will be saved in the configuration for that instance of the block, independent of the other instances of the block. The block is still not making use of the results of the configuration change, however. That is in the next book page.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.