From 380edeb118d43074ad81859b533f34770e3a19fb Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 2 Apr 2019 17:01:02 -0300 Subject: [PATCH] Use CSL for short citations. --- bibcite_footnotes.module | 8 +- .../BibCiteProcessor/CiteprocPhpInline.php | 73 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/Plugin/BibCiteProcessor/CiteprocPhpInline.php diff --git a/bibcite_footnotes.module b/bibcite_footnotes.module index 07d76b0..b9585c5 100644 --- a/bibcite_footnotes.module +++ b/bibcite_footnotes.module @@ -202,9 +202,13 @@ function bibcite_footnotes_preprocess_bibcite_footnote_link(&$variables) { $title = trim(strip_tags(render($citation_data))); $url = Url::fromUserInput('#' . $fn['fn_id'], ['attributes' => ['id' => $fn['ref_id'], 'class' => $class, 'title' => $title]]); - - $link = Link::fromTextAndUrl($fn['value'], $url)->toRenderable(); +// We need to delete the cached markup to render it in a different mode. + unset($citation_data['#markup']); + unset($citation_data['#children']); + + $citation_data['#data']['mode'] = 'citation'; + $link = Link::fromTextAndUrl(render($citation_data), $url)->toRenderable(); unset($variables['fn']['fn']); $variables['fn']['fn'] = $link; // $variables['fn']['fn'][] = $link; diff --git a/src/Plugin/BibCiteProcessor/CiteprocPhpInline.php b/src/Plugin/BibCiteProcessor/CiteprocPhpInline.php new file mode 100644 index 0000000..87d320f --- /dev/null +++ b/src/Plugin/BibCiteProcessor/CiteprocPhpInline.php @@ -0,0 +1,73 @@ +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); + } + +}