// Error if there are no entities available for a required field.
$form_state->setError($element,t('There are no @entity_type_plural matching "%value".',$params));
}
}
elseif(count($entities)>5){
$params['@id']=key($entities);
// Error if there are more than 5 matching entities.
$form_state->setError($element,t('Many @entity_type_plural are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".',$params));
}
elseif(count($entities)>1){
// More helpful error if there are only a few matching entities.
$multiples=[];
foreach($entitiesas$id=>$name){
$multiples[]=$name.' ('.$id.')';
}
$params['@id']=$id;
$form_state->setError($element,t('Multiple @entity_type_plural match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".',['%multiple'=>strip_tags(implode('", "',$multiples))]+$params));