5

I created a form for a block plugin. I'm attempting to save the values to the form after submitting. However I'm having trouble saving the values if they are within an array.

For instance

public function blockForm($form, FormStateInterface $form_state){
    $form['plugins_string_text'] = array(
      '#type' => 'text_format',
      '#title' => $this->t('Block contents'),
      '#format' => 'full_html',
      '#description' => $this->t('This text will appear in the block.'),
      '#default_value' => $this->configuration['plugins_string_text-submit'],
    );

    $form['header']['margin-top'] = array(
        '#type' => 'textfield',
        '#title' => t('Margin Top (pixels)'),
        '#default_value' => $this->configuration['header']['margin-top-submit'],
        );
    $form['header']['margin-bottom'] = array(
        '#type' => 'textfield',
        '#title' => t('Margin Bottom (pixels)'),
        '#default_value' => $this->configuration['header']['margin-bottom-submit'],
        );
    $form['header']['margin-left'] = array(
        '#type' => 'textfield',
        '#title' => t('Margin Left (pixels)'),
        '#default_value' => $this->configuration['header']['margin-left-submit'],
        );
    $form['header']['margin-right'] = array(
        '#type' => 'textfield',
        '#title' => t('Margin Right (pixels)'),
        '#default_value' => $this->configuration['header']['margin-right-submit'],
        );
}

    public function blockSubmit($form, FormStateInterface $form_state) {
        $this->configuration['plugins_string_text-submit']  = $form_state->getValue('plugins_string_text');
        $this->configuration['header']['margin-top-submit'] = $form_state->getValue('header','margin-top');
        $this->configuration['header']['margin-bottom-submit'] = $form_state->getValue('header','margin-bottom');
        $this->configuration['header']['margin-right-submit'] = $form_state->getValue('header','margin-right');
        $this->configuration['header']['margin-left-submit'] = $form_state->getValue('header','margin-left');
}

This is saved to the form properly

$this->configuration['plugins_string_text-submit']  = $form_state->getValue('plugins_string_text');

However each of the lines below display the entirety of the 'header' array instead of the individual margin values.

$this->configuration['header']['margin-top-submit'] = $form_state->getValue('header','margin-top');
$this->configuration['header']['margin-bottom-submit'] = $form_state->getValue('header','margin-bottom');
$this->configuration['header']['margin-right-submit'] = $form_state->getValue('header','margin-right');
$this->configuration['header']['margin-left-submit'] = $form_state->getValue('header','margin-left');

What is the proper way to use getValue to reach an element within an array. Or is there another function I should be using?

17

Put the keys in an array, for example:

$this->configuration['header']['margin-top-submit'] = $form_state->getValue(['header','margin-top']);
  • 2
    I finally got this to work, but only after adding ... '#tree' => TRUE ... to a parent element (fieldset in my case, but a fieldgroup will work and maybe others). – Delford Chaffin Sep 8 '17 at 20:20
  • @DelfordChaffin Did you use the '#tree' => TRUE at a container or a fieldset ?? – Hawk Feb 5 '18 at 4:15
  • 1
    Thanks @DelfordChaffin. '#tree' => TRUE in my fieldset was the secret sauce to getting the array. – Andrew Wasson Aug 2 at 2:53
5

This worked for me in drupal 8 after many hours of testing. I hope it helps.

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $is_live = $form_state->getValue('settings')['is_live '];
}
  • After "many hours of testing", I finally got this to work after adding $form['account']['#tree'] = TRUE; – Sébastien Gicquel Aug 22 at 15:55

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.