0

I have a custom views filter extending Drupal\user\Plugin\views\filter\Name that I am trying to add an additional form element. The existing filter is an autocomplete field that I need to add a checkbox element. I've tried updating ::valueForm.

// Working code that creates the autocomplete field.

$existingElement = $form['value'];

$form['value'] = [
  '#tree' => TRUE,
  'existing_element' => $existingElement,
  'new_element' => [
    '#type' => 'checkbox',
    '#title' => t('Checkbox'),
    '#default_value' => FALSE,
  ],
];

It throws this error.

Error: Cannot create references to/from string offsets in Drupal\Component\Utility\NestedArray::setValue() (line 155 of core/lib/Drupal/Component/Utility/NestedArray.php).

It seems to be related to the form state, but I have troubles in figuring out exactly what is missing.

1 Answer 1

0

You probably need to override the valueValidate method to tell it about the new structure:

protected function valueValidate($form, FormStateInterface $form_state) {
  $uids = [];
  if ($values = $form_state->getValue(['options', 'value', 'existing_element'])) {
    foreach ($values as $value) {
      $uids[] = $value['target_id'];
    }
    sort($uids);
  }

  $checkbox_value = $form_state->getValue(['options', 'value', 'new_element']);
  // Do something with $checkbox_value here as you're going to lose it

  $form_state->setValue(['options', 'value'], $uids);
}

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.