Validate helper to have support for other entity reference widgets.
$element:
FormStateInterface $form_state:
$form:
public static function targetTypeValidate($element, FormStateInterface $form_state, $form) {
$values =& $form_state
->getValues();
$element_values = NestedArray::getValue($values, $element['#parents']);
$bundle_options = array();
if ($element_values) {
$enabled = 0;
foreach ($element_values as $machine_name => $bundle_info) {
if (isset($bundle_info['enabled']) && $bundle_info['enabled']) {
$bundle_options[$machine_name] = $machine_name;
$enabled++;
}
}
// All disabled = all enabled.
if ($enabled === 0) {
$bundle_options = NULL;
}
}
// New value parents.
$parents = array_merge(array_slice($element['#parents'], 0, -1), array(
'target_bundles',
));
NestedArray::setValue($values, $parents, $bundle_options);
}