You are here

public function CivicrmEntity::validate in CiviCRM Entity 8.3

Validates the currently set values.

Return value

\Drupal\Core\Entity\EntityConstraintViolationListInterface A list of constraint violations. If the list is empty, validation succeeded.

Overrides ContentEntityBase::validate

File

src/Entity/CivicrmEntity.php, line 157

Class

CivicrmEntity
Entity class for CiviCRM entities.

Namespace

Drupal\civicrm_entity\Entity

Code

public function validate() {
  $violations = parent::validate();
  $params = $this
    ->civicrmApiNormalize();
  $civicrm_api = \Drupal::getContainer()
    ->get('civicrm_entity.api');
  $civicrm_entity_type = $this
    ->getEntityType()
    ->get('civicrm_entity');
  $civicrm_violations = $civicrm_api
    ->validate($civicrm_entity_type, $params);
  if (!empty($civicrm_violations)) {
    foreach (reset($civicrm_violations) as $civicrm_field => $civicrm_violation) {
      $definition = $this
        ->getFieldDefinition($civicrm_field);
      $violation = new ConstraintViolation(str_replace($civicrm_field, $definition
        ->getLabel(), $civicrm_violation['message']), str_replace($civicrm_field, $definition
        ->getLabel(), $civicrm_violation['message']), [], '', $civicrm_field, $params[$civicrm_field]);
      $violations
        ->add($violation);
    }
  }
  return $violations;
}