Submit configuration form in block
Add the following method to the HelloBlock
class. In this example, it's located in the src/Plugin/Block/HelloBlock.php
file, but as you start thinking in a more OOP-oriented way, where it's physically located in the file structure is less important than the namespace. If you're a savvy OO programmer, you'll keep both closely aligned. But just in case, the namespace—very similar to the module's folder name and machine name from our earlier discussion—will be important later when you want to interact with your module’s code programmatically.
/** * {@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 the form elements, you should pass an array to the getValue()
function instead of just the field name. Here, myfieldset
is the fieldset that wraps the hello_block_name
field.
$this->configuration['hello_block_name'] = $form_state->getValue(['myfieldset', 'hello_block_name']);
Adding this code means the form will be processed and the form input will be saved to the configuration for this specific block instance, independently of other block instances. However, the block still doesn’t use the updated configuration result. That’s on the next page of the book.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.