/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { $row['label'] = $entity->label(); $row['id'] = $entity->id(); // Render encryption method row. if ($encryption_method = $entity->getEncryptionMethod()) { $row['encryption_method'] = $encryption_method->getLabel(); } else { $row['encryption_method'] = $this->t('Error loading encryption method'); } // Render encryption key row. if ($key = $entity->getEncryptionKey()) { $row['key'] = $key->label(); } else { $row['key'] = $this->t('Error loading key'); } // Render status report row. if ($this->config->get('check_profile_status')) { $errors = $entity->validate(); if (!empty($errors)) { $row['status']['data'] = array('#theme' => 'item_list', '#items' => $errors, '#attributes' => array("class" => array("color-error"))); } else { $row['status'] = $this->t('OK'); } } return $row + parent::buildRow($entity); }
/** * Verifies that the whole entity does not violate any validation constraints. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * If validation errors are found. */ protected function validate(EntityInterface $entity) { $violations = $entity->validate(); // Remove violations of inaccessible fields as they cannot stem from our // changes. $violations->filterByFieldAccess(); if (count($violations) > 0) { $message = "Unprocessable Entity: validation failed.\n"; foreach ($violations as $violation) { $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n"; } // Instead of returning a generic 400 response we use the more specific // 422 Unprocessable Entity code from RFC 4918. That way clients can // distinguish between general syntax errors in bad serializations (code // 400) and semantic errors in well-formed requests (code 422). throw new HttpException(422, $message); } }
/** * Verifies that a AllowedValues violation exists for the given field. * * @param \Drupal\core\Entity\EntityInterface $entity * The entity object to validate. * @param string $field_name * The name of the field to verify. */ protected function assertAllowedValuesViolation(EntityInterface $entity, $field_name) { $violations = $entity->validate(); $this->assertEqual(count($violations), 1, "Allowed values violation for {$field_name} found."); $this->assertEqual($violations[0]->getPropertyPath(), "{$field_name}.0.value"); $this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.')); }
/** * Validate and save entity. Fail if violations are found. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to save. * * @return void */ protected function entityValidateAndSave(EntityInterface $entity) { $violations = $entity->validate(); if ($violations->count()) { $this->fail($violations); } else { $entity->save(); } }
/** * Verifies that a length violation exists for the given field. * * @param \Drupal\core\Entity\EntityInterface $entity * The entity object to validate. * @param string $field_name * The field that violates the maximum length. * @param int $length * Number of characters that was exceeded. */ protected function assertLengthViolation(EntityInterface $entity, $field_name, $length) { $violations = $entity->validate(); $this->assertEqual(count($violations), 1, "Violation found when {$field_name} is too long."); $this->assertEqual($violations[0]->getPropertyPath(), "{$field_name}.0.value"); $field_label = $entity->get($field_name)->getFieldDefinition()->getLabel(); $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length))); }
/** * {@inheritdoc} */ protected function entityValidate(EntityInterface $entity) { $violations = $entity->validate(); if (!count($violations)) { return; } $args = ['@entity' => Unicode::strtolower($this->entityTypeLabel()), '%label' => $entity->label(), '%error' => $violations[0]->getMessage(), '@url' => $this->url('entity.feeds_feed_type.mapping', ['feeds_feed_type' => $this->feedType->id()])]; throw new ValidationException(SafeMarkup::format('The @entity %label failed to validate with the error: %error Please check your <a href="@url">mappings</a>.', $args)); }