I have ConfigFormBase.
It has a form element with '#type' => 'entity_autocomplete'
.
When I add my custom element validation by '#element_validate' => [[get_class($this), 'validateReferencedItemExistence']]
the original element validation which comes by the element type (Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete
) won't be added anymore.
After spending hours in the jungle of the FormBuilder
class code I finally found the corresponding lines:
if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = $this->elementInfo->getInfo($element['#type']))) {
// Overlay $info onto $element, retaining preexisting keys in $element.
$element += $info;
$element['#defaults_loaded'] = TRUE;
}
I find this kinda bad approach, that you cannot extend the list of the validation items. What is the reason behind it?
How could I make a workaround / solve this issue?
(Just by calling Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete
in my custom validation I would create an unnecessary depedency in my code. (Just think about if they change their way of validation with a different function))
$array['item'] = [];
you are replacing$array['item']
; if you use$array['item'][] = [];
you are adding items (supposing that$array['item']
is an array).