Browse Source

Use CSL for short citations.

csl
Alexander O'Neill 6 years ago
parent
commit
380edeb118
  1. 6
      bibcite_footnotes.module
  2. 73
      src/Plugin/BibCiteProcessor/CiteprocPhpInline.php

6
bibcite_footnotes.module

@ -203,8 +203,12 @@ function bibcite_footnotes_preprocess_bibcite_footnote_link(&$variables) {
$title = trim(strip_tags(render($citation_data))); $title = trim(strip_tags(render($citation_data)));
$url = Url::fromUserInput('#' . $fn['fn_id'], ['attributes' => ['id' => $fn['ref_id'], 'class' => $class, 'title' => $title]]); $url = Url::fromUserInput('#' . $fn['fn_id'], ['attributes' => ['id' => $fn['ref_id'], 'class' => $class, 'title' => $title]]);
// We need to delete the cached markup to render it in a different mode.
unset($citation_data['#markup']);
unset($citation_data['#children']);
$link = Link::fromTextAndUrl($fn['value'], $url)->toRenderable(); $citation_data['#data']['mode'] = 'citation';
$link = Link::fromTextAndUrl(render($citation_data), $url)->toRenderable();
unset($variables['fn']['fn']); unset($variables['fn']['fn']);
$variables['fn']['fn'] = $link; $variables['fn']['fn'] = $link;
// $variables['fn']['fn'][] = $link; // $variables['fn']['fn'][] = $link;

73
src/Plugin/BibCiteProcessor/CiteprocPhpInline.php

@ -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…
Cancel
Save