CitationStyler.php in Bibliography & Citation 8
File
src/CitationStyler.php
View source
<?php
namespace Drupal\bibcite;
use Drupal\bibcite\Entity\CslStyleInterface;
use Drupal\bibcite\Plugin\BibCiteProcessorInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
class CitationStyler implements CitationStylerInterface {
protected $processor;
protected $pluginManager;
protected $configuration;
protected $languageManager;
protected $cslStorage;
protected $style;
protected $langCode;
public function __construct(PluginManagerInterface $plugin_manager, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->pluginManager = $plugin_manager;
$this->configuration = $config_factory
->get('bibcite.settings');
$this->languageManager = $language_manager;
$this->cslStorage = $entity_type_manager
->getStorage('bibcite_csl_style');
}
public function render($data) {
$csl = $this
->getStyle()
->getCslText();
$lang = $this
->getLanguageCode();
return $this
->getProcessor()
->render($data, $csl, $lang);
}
public function setProcessor(BibCiteProcessorInterface $processor) {
$this->processor = $processor;
return $this;
}
public function setProcessorById($processor_id) {
$this->processor = $this->pluginManager
->createInstance($processor_id);
return $this;
}
public function getProcessor() {
if (!$this->processor) {
$this
->setProcessorById($this->configuration
->get('processor'));
}
return $this->processor;
}
public function getAvailableProcessors() {
return $this->pluginManager
->getDefinitions();
}
public function getAvailableStyles() {
return $this->cslStorage
->loadMultiple();
}
public function getStyle() {
if (!$this->style) {
$this
->setStyleById($this->configuration
->get('default_style'));
}
return $this->style;
}
public function setStyle($csl_style) {
$this->style = $csl_style;
return $this;
}
public function setStyleById($style_id) {
$this->style = $this->cslStorage
->load($style_id);
if (!$this->style) {
throw new \UnexpectedValueException("CSL style '{$style_id}' does not exist.");
}
return $this;
}
public function getLanguageCode() {
if (!$this->langCode) {
$this->langCode = $this->languageManager
->getCurrentLanguage()
->getId();
}
return $this->langCode;
}
public function setLanguageCode($lang_code) {
$this->lang = $lang_code;
return $this;
}
}