10

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 that are in an array.

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 properly saved.

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

However, each of the following lines displays the entirety of the 'header' array instead of the individual 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');

How do I call $form_state->getValue() to get the value of an element in an array?

2 Answers 2

24

Put the keys in an array, for example:

$this->configuration['header']['margin-top-submit'] = $form_state->getValue(['header','margin-top']);
3
  • 5
    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). Sep 8, 2017 at 20:20
  • @DelfordChaffin Did you use the '#tree' => TRUE at a container or a fieldset ?? Feb 5, 2018 at 4:15
  • 1
    Thanks @DelfordChaffin. '#tree' => TRUE in my fieldset was the secret sauce to getting the array. Aug 2, 2019 at 2:53
6

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 '];
}
1
  • 1
    After "many hours of testing", I finally got this to work after adding $form['account']['#tree'] = TRUE; Aug 22, 2019 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.