I have a custom field type and I'd like to create an autocomplete widget for it. My custom field has two data definitions - target_id and target_bundle, both strings.

I've created a custom widget which hides those two fields and adds a text field with #autocomplete_route_name attribute set to controller that is returning json of possible values and that part works fine.

The problem arises when I'm submitting the value. In method massageFormValues of my custom autocomplete widget I'm trying to fill the values of hidden fields (target_id and target_bundle) but those values are left empty no matter what I try. Basically, I'm returning "fixed" $values method argument but those values are not stored later on. So on submit I'm getting form error: An illegal choice has been detected. Please contact the site administrator. because target_id and target_bundle are empty.

Here is what I'm trying to do for better understanding.

       * {@inheritdoc}
      public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
        // Do not try to save an empty default value for the field.
        foreach (array_keys($values) as $key) {
          if (!empty($values[$key]['autocomplete']) && $matches = $this->extractBundleIdFromAutocompleteInput($values[$key]['autocomplete'])) {
            $values[$key]['target_id'] = $matches[1];       // <- not saved
            $values[$key]['target_bundle'] = $matches[0];   // <- not saved
          if (empty($values[$key]['target_id'])) {
        return $values;

Any ideas how to tackle this issue?


It turns out that issue was hidden in first part of my question for which I though it works but it wasn't. Also some context was missing.

I was extending existing widget and hiding the fields it had. It turns out that target_bundle field was previously a select field and I had to unset #options parameter from it. Then everything started to work.

I guess this question has no real value for stack overflow so I think it can be deleted as well.

| improve this answer | |

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