BibtexReferenceNormalizer.php in Bibliography & Citation 8
File
modules/bibcite_bibtex/src/Normalizer/BibtexReferenceNormalizer.php
View source
<?php
namespace Drupal\bibcite_bibtex\Normalizer;
use Drupal\bibcite_entity\Entity\Contributor;
use Drupal\bibcite_entity\Entity\ReferenceInterface;
use Drupal\bibcite_entity\Normalizer\ReferenceNormalizerBase;
use Drupal\Core\Field\FieldItemListInterface;
class BibtexReferenceNormalizer extends ReferenceNormalizerBase {
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]);
}
}
$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;
}
public function normalize($reference, $format = NULL, array $context = []) {
$attributes = [];
$attributes[$this->typeKey] = $this
->convertEntityType($reference
->bundle(), $format);
if ($keywords = $this
->extractKeywords($reference
->get('keywords'))) {
$attributes[$this->keywordKey] = $keywords;
}
$contributors = $this
->extractContributors($reference
->get('author'));
if (isset($contributors['author'])) {
$attributes[$this->contributorKey] = $contributors['author'];
}
if (isset($contributors['editor'])) {
$attributes['editor'] = $contributors['editor'];
}
$attributes += $this
->extractFields($reference, $format);
return $attributes;
}
protected function extractFields(ReferenceInterface $reference, $format) {
$attributes = parent::extractFields($reference, $format);
$attributes['title'] = $this
->extractScalar($reference
->get('title'));
$attributes['reference'] = $reference
->id();
return $attributes;
}
private function extractContributors(FieldItemListInterface $field_item_list) {
$contributors = [];
foreach ($field_item_list as $field) {
$role = $field
->get('role')
->getValue() === 'editor' ? 'editor' : 'author';
$contributors[$role][] = $field->entity
->getName();
}
return $contributors;
}
}