You are here

protected function EntityProcessorBase::entityValidate in Feeds 8.3

1 call to EntityProcessorBase::entityValidate()
EntityProcessorBase::process in src/Feeds/Processor/EntityProcessorBase.php
Processes the results from a parser.

File

src/Feeds/Processor/EntityProcessorBase.php, line 533

Class

EntityProcessorBase
Defines a base entity processor.

Namespace

Drupal\feeds\Feeds\Processor

Code

protected function entityValidate(EntityInterface $entity) {

  // Check if an entity with the same ID already exists if the given entity is
  // new.
  if ($entity
    ->isNew() && $this
    ->entityExists($entity)) {
    throw new ValidationException($this
      ->t('An entity with ID %id already exists.', [
      '%id' => $entity
        ->id(),
    ]));
  }
  $violations = $entity
    ->validate();
  if (!count($violations)) {
    return;
  }
  $errors = [];
  foreach ($violations as $violation) {
    $error = $violation
      ->getMessage();

    // Try to add more context to the message.
    // @todo if an exception occurred because of a different bundle, add more
    // context to the message.
    $invalid_value = $violation
      ->getInvalidValue();
    if ($invalid_value instanceof FieldItemListInterface) {

      // The invalid value is a field. Get more information about this field.
      $error = new FormattableMarkup('@name (@property_name): @error', [
        '@name' => $invalid_value
          ->getFieldDefinition()
          ->getLabel(),
        '@property_name' => $violation
          ->getPropertyPath(),
        '@error' => $error,
      ]);
    }
    else {
      $error = new FormattableMarkup('@property_name: @error', [
        '@property_name' => $violation
          ->getPropertyPath(),
        '@error' => $error,
      ]);
    }
    $errors[] = $error;
  }
  $element = [
    '#theme' => 'item_list',
    '#items' => $errors,
  ];

  // Compose error message. If available, use the entity label to indicate
  // which item failed. Fallback to the GUID value (if available) or else
  // no indication.
  $label = $entity
    ->label();
  $guid = $entity
    ->get('feeds_item')->guid;
  $messages = [];
  $args = [
    '@entity' => mb_strtolower($this
      ->entityTypeLabel()),
    '%label' => $label,
    '%guid' => $guid,
    '@errors' => \Drupal::service('renderer')
      ->renderRoot($element),
    ':url' => $this
      ->url('entity.feeds_feed_type.mapping', [
      'feeds_feed_type' => $this->feedType
        ->id(),
    ]),
  ];
  if ($label || $label === '0' || $label === 0) {
    $messages[] = $this
      ->t('The @entity %label failed to validate with the following errors: @errors', $args);
  }
  elseif ($guid || $guid === '0' || $guid === 0) {
    $messages[] = $this
      ->t('The @entity with GUID %guid failed to validate with the following errors: @errors', $args);
  }
  else {
    $messages[] = $this
      ->t('An entity of type "@entity" failed to validate with the following errors: @errors', $args);
  }
  $messages[] = $this
    ->t('Please check your <a href=":url">mappings</a>.', $args);

  // Concatenate strings as markup to mark them as safe.
  $message_element = [
    '#markup' => implode("\n", $messages),
  ];
  $message = \Drupal::service('renderer')
    ->renderRoot($message_element);
  throw new ValidationException($message);
}