You are here

public function BibtexReferenceNormalizer::denormalize in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_bibtex/src/Normalizer/BibtexReferenceNormalizer.php \Drupal\bibcite_bibtex\Normalizer\BibtexReferenceNormalizer::denormalize()

Overrides ReferenceNormalizerBase::denormalize

File

modules/bibcite_bibtex/src/Normalizer/BibtexReferenceNormalizer.php, line 18

Class

BibtexReferenceNormalizer
Normalizes/denormalizes reference entity to BibTeX format.

Namespace

Drupal\bibcite_bibtex\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  $contributors = [];
  $contributor_key = $this
    ->getContributorKey();
  foreach ([
    $contributor_key,
    'editor',
  ] as $role) {
    if (!empty($data[$role])) {
      foreach ((array) $data[$role] as $author_name) {
        $contributors[] = [
          'name' => $author_name,
          'role' => $role,
        ];
      }
      unset($data[$role]);
    }
  }

  /* @var \Drupal\bibcite_entity\Entity\Reference $entity */
  $entity = parent::denormalize($data, $class, $format, $context);
  if (!empty($contributors)) {
    $author_field = $entity
      ->get('author');
    foreach ($contributors as $contributor) {
      $author = $this->serializer
        ->denormalize([
        'name' => [
          [
            'value' => $contributor['name'],
          ],
        ],
      ], Contributor::class, $format, $context);
      $author_field
        ->appendItem([
        'entity' => $author,
        'role' => $contributor['role'],
      ]);
    }
  }
  return $entity;
}