You are here

public function EntityReferenceHierarchyAutocomplete::errorElement in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceHierarchyAutocomplete.php \Drupal\entity_hierarchy\Plugin\Field\FieldWidget\EntityReferenceHierarchyAutocomplete::errorElement()

Assigns a field-level validation error to the right widget sub-element.

Depending on the widget's internal structure, a field-level validation error needs to be flagged on the right sub-element.

Parameters

array $element: An array containing the form element for the widget, as generated by formElement().

\Symfony\Component\Validator\ConstraintViolationInterface $violation: A constraint violation reported during the validation phase.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array|bool The element on which the error should be flagged, or FALSE to completely ignore the violation (use with care!).

Overrides EntityReferenceAutocompleteWidget::errorElement

File

src/Plugin/Field/FieldWidget/EntityReferenceHierarchyAutocomplete.php, line 28

Class

EntityReferenceHierarchyAutocomplete
Widget that uses autocomplete.

Namespace

Drupal\entity_hierarchy\Plugin\Field\FieldWidget

Code

public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {
  if (isset($element[0]['target_id'])) {
    return $element[0]['target_id'];
  }
  if (isset($element['target_id'])) {
    return $element['target_id'];
  }
  return FALSE;
}