Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

Put the keys in an array, for example:

$this->configuration['header']['margin-top-submit'] = $form_state->getValue(['header','margin-top']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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