The Field Type API are more strict in Drupal 8 so that Web Services and Forms follow the same validation path. This is not always the behavior that we want.
Looking at your question again, I think that using an allowed values callback on the field would work as well.
I provided some alternatives based on the restrictions that core places on list fields below #1 as well in case the following doesn't work as intended. These could be useful for other people doing similar things.
#1: Try to use allowed values callback for the field instead
Set an allowed_values_callback for the list field.
function gamodule_allowed_values_callback(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
// @todo make this a bit more conditional based on the parameters above.
$result = db_query("SELECT ga_id,name FROM ga_projects");
$row = $result->fetchAll();
$row = json_decode(json_encode($row), True);
foreach ($row as $key => $value) {
$matches[$key] = $value['name'];
}
return $matches;
}
Note that the documentation for callback_allowed_values_callback mentions using opt groups, but this functionality has a bug and is broken in 8.
#2: Migrate list field to text field
Make a Select list Field Widget for text fields. Migrate your data over.
Risks
- A lot of work to re-architect.
- May confuse a development team inheriting the site.
#3: Override the AllowedValues constraint plugin
Use hook_validation_constraint_alter
to provide your own Constraint plugin (see Symfony 2 Constraint documentation).
Risks
- Site is incompatible with a couple of contrib modules doing the same thing (select_or_other, cck_select_other).
#4: Make sure all possible allowed values are in the list field
Don't use a custom table to store allowed values. Set the allowed values in list field configuration. Write a field widget plugin.
Write your own Field Widget that restricts options based on some other parameter like your query.
#5: Override Core handling of list field plugins with your own implementation
Use hook_field_info_alter
to not use Core code at all. Write your own List Field Type plugin with a better Options or Allowed Values implementation.
Risks
- May confuse a development team inheriting the site.
- All list fields now behave this way.