You are here

function bibcite_import_batch_callback in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_import/bibcite_import.batch.inc \bibcite_import_batch_callback()

Batch operation callback. Denormalize entries and try to save entity.

Parameters

array $entries: Array of parsed entries.

\Drupal\bibcite\Plugin\BibciteFormatInterface $format: Instance of format plugin.

array $context: The batch context array, passed by reference.

1 string reference to 'bibcite_import_batch_callback'
ImportForm::submitForm in modules/bibcite_import/src/Form/ImportForm.php
Form submission handler.

File

modules/bibcite_import/bibcite_import.batch.inc, line 21
Batch functions.

Code

function bibcite_import_batch_callback(array $entries, BibciteFormatInterface $format, array &$context) {
  if (empty($context['results'])) {
    $context['success'] = FALSE;
    $context['results']['success'] = [];
    $context['results']['errors'] = [];
    $context['results']['inaccessible_entities'] = 0;
  }

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $config = \Drupal::config('bibcite_import.settings');
  $denormalize_context = [
    'contributor_deduplication' => $config
      ->get('settings.contributor_deduplication'),
    'keyword_deduplication' => $config
      ->get('settings.keyword_deduplication'),
  ];
  foreach ($entries as $entry) {
    $entity = '';

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    try {
      $entity = $serializer
        ->denormalize($entry, Reference::class, $format
        ->getPluginId(), $denormalize_context);
    } catch (UnexpectedValueException $e) {
      $message = [
        t('Entry has not been parsed.'),
        $e
          ->getMessage(),
      ];
      \Drupal::logger('bibcite_import')
        ->error(implode("\n", $message));
      $context['results']['errors'][] = implode("\n", $message);
    }
    if (!empty($entity)) {
      if (!$entity
        ->access('create')) {
        $context['results']['inaccessible_entities']++;
        continue;
      }
      try {
        if ($entity
          ->save()) {
          $context['results']['success'][] = $entity
            ->id() . ' : ' . $entity
            ->label();
          $context['success'] = TRUE;
        }
      } catch (Exception $e) {
        $message = [
          t('Entity can not be saved.'),
          t('Label: @label', [
            '@label' => $entity
              ->label(),
          ]),
          '<pre>',
          $e
            ->getMessage(),
          '</pre>',
        ];
        \Drupal::logger('bibcite_import')
          ->error(implode("\n", $message));
        $context['results']['errors'][] = $entity
          ->label();
      }
      $context['message'] = $entity
        ->label();
    }
  }
}