You are here

public function EntityReferenceBrowserWidget::flagErrors in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::flagErrors()

Reports field-level validation errors against actual form elements.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values.

\Symfony\Component\Validator\ConstraintViolationListInterface $violations: A list of constraint violations to flag.

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 form state.

Overrides WidgetBase::flagErrors

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php, line 331

Class

EntityReferenceBrowserWidget
Plugin implementation of the 'entity_reference' widget for entity browser.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
  if ($violations
    ->count() > 0) {

    /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
    foreach ($violations as $offset => $violation) {

      // The value of the required field is checked through the "not null"
      // constraint, whose message is not very useful. We override it here for
      // better UX.
      if ($violation
        ->getConstraint() instanceof NotNullConstraint) {
        $violations
          ->set($offset, new ConstraintViolation($this
          ->t('@name field is required.', [
          '@name' => $items
            ->getFieldDefinition()
            ->getLabel(),
        ]), '', [], $violation
          ->getRoot(), $violation
          ->getPropertyPath(), $violation
          ->getInvalidValue(), $violation
          ->getPlural(), $violation
          ->getCode(), $violation
          ->getConstraint(), $violation
          ->getCause()));
      }
    }
  }
  parent::flagErrors($items, $violations, $form, $form_state);
}