View source
<?php
namespace Drupal\bibcite_entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class ReferenceViewBuilder extends EntityViewBuilder {
protected $serializer;
protected $configFactory;
public function __construct(EntityTypeInterface $entity_type, NormalizerInterface $serializer, ConfigFactoryInterface $config_factory, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->serializer = $serializer;
$this->configFactory = $config_factory;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$serializer = $container
->get('serializer');
$config_factory = $container
->get('config.factory');
$entity_repository = $container
->get('entity.repository');
$language_manager = $container
->get('language_manager');
$theme_registry = $container
->get('theme.registry');
$entity_display_repository = $container
->get('entity_display.repository');
return new static($entity_type, $serializer, $config_factory, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
}
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
parent::buildComponents($build, $entities, $displays, $view_mode);
foreach ($entities as $id => $entity) {
$bundle = $entity
->bundle();
$display = $displays[$bundle];
if ($display
->getComponent('citation')) {
$build[$id]['citation'] = [
'#theme' => 'bibcite_citation',
'#data' => $this->serializer
->normalize($entity, 'csl'),
'#entity' => $entity,
];
}
}
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'config:bibcite_entity.reference.settings',
'config:bibcite.settings',
]);
}
}