Alexander O'Neill
6 years ago
2 changed files with 79 additions and 2 deletions
@ -0,0 +1,73 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\bibcite_footnotes\Plugin\BibCiteProcessor; |
||||
|
||||
use AcademicPuma\CiteProc\CiteProc; |
||||
use Drupal\Core\Config\ConfigFactoryInterface; |
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||
use Drupal\bibcite\Plugin\BibCiteProcessorBase; |
||||
use Drupal\bibcite\Plugin\BibCiteProcessorInterface; |
||||
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
|
||||
/** |
||||
* Defines a style provider based on citeproc-php library. |
||||
* |
||||
* Extended to support inline citation links based on the CSL. |
||||
* |
||||
* @BibCiteProcessor( |
||||
* id = "citeproc-php", |
||||
* label = @Translation("Citeproc PHP"), |
||||
* ) |
||||
*/ |
||||
class CiteprocPhpInline extends BibCiteProcessorBase implements BibCiteProcessorInterface, ContainerFactoryPluginInterface { |
||||
|
||||
/** |
||||
* Config factory service. |
||||
* |
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface |
||||
*/ |
||||
protected $configFactory; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||
return new static( |
||||
$configuration, |
||||
$plugin_id, |
||||
$plugin_definition, |
||||
$container->get('config.factory') |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) { |
||||
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||
|
||||
$this->configFactory = $config_factory; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getDescription() { |
||||
return $this->t('Render citation by citeproc-php library'); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function render($data, $csl, $lang) { |
||||
$cite_proc = new CiteProc($csl, $lang); |
||||
$mode = !empty($data['mode']) ? $data['mode'] : NULL; |
||||
|
||||
if (!$data instanceof \stdClass) { |
||||
$data = json_decode(json_encode($data)); |
||||
} |
||||
|
||||
return $cite_proc->render($data, $mode); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue