Compare commits

...

7 Commits
8.x-1.x ... ief

  1. 10
      bibcite_footnotes.libraries.yml
  2. 57
      bibcite_footnotes.module
  3. 18
      js/plugins/reference_footnotes/dialogs/footnotes.js
  4. 28
      js/replace_citations.js
  5. 23
      src/Plugin/CKEditorPlugin/ReferenceFootnotes.php
  6. 10
      src/Plugin/Filter/ReferenceFootnotesFilter.php

10
bibcite_footnotes.libraries.yml

@ -3,3 +3,13 @@ reference_footnote:
css:
theme:
css/reference_footnote.css: {}
dependencies:
- core/jquery
- core/drupalSettings
replace_citations:
version: VERSION
js:
js/replace_citations.js: {}
dependencies:
- core/jquery
- core/drupalSettings

57
bibcite_footnotes.module

@ -11,6 +11,9 @@ use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\bibcite_footnotes\CitationTools;
use Drupal\bibcite_footnotes\Plugin\Filter\ReferenceFootnotesFilter;
use Drupal\field\Entity\FieldConfig;
use Drupal\core\Ajax\SettingsCommand;
use Symfony\Component\HttpFoundation\Request;
/**
* Implements hook_theme().
@ -207,3 +210,57 @@ function bibcite_footnotes_preprocess_bibcite_footnote_link(&$variables) {
$variables['fn']['fn'] = $link;
// $variables['fn']['fn'][] = $link;
}
function bibcite_footnotes_inline_entity_form_entity_form_alter(&$form, FormStateInterface &$form_state) {
$form['#save_entity'] = TRUE;
}
function bibcite_footnotes_form_node_form_alter(&$form, FormStateInterface &$form_state) {
$ief = $form_state->get('inline_entity_form');
if ($ief) {
$serializer = \Drupal::service('serializer');
// Inline entity storage uses hashes to separate out the field instances.
$bibcite_references = [];
foreach ($ief as $ief_instance) {
/**
* @var FieldConfig $field_config
*/
$field_config = $ief_instance['instance'];
// Check if this is a bibcite_reference field type.
if ($field_config->getSetting('handler') == 'default:bibcite_reference') {
$field_name = $field_config->get('field_name');
if (!empty($ief_instance['entities'])) {
foreach ($ief_instance['entities'] as $entity_wrapper) {
/**
* @var \Drupal\core\Entity\EntityInterface $entity
*/
$entity = $entity_wrapper['entity'];
if ($entity->getEntityTypeId() == 'bibcite_reference') {
$data = $serializer->normalize($entity, 'csl');
$build = ['#theme' => 'bibcite_citation', '#data' => $data];
$citation_text = trim(strip_tags(render($build)));
$citation_hash = md5(implode('', $data));
// Attempt to match up pre-saved entities with the eventual saved ones.
$citation_key = $entity->id->first()->getValue()['value'];
$bibcite_references[] = [$citation_text, $citation_key];
}
}
}
}
}
$triggering_element = $form_state->getTriggeringElement();
if (!$triggering_element) {
//$form['#attached']['library'][] = 'bibcite_footnotes/replace_citations';
$form['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $bibcite_references;
// $form['#attached']['drupalSettings']['']
}
else {
$form[$triggering_element['#parents'][0]]['widget']['#attached']['library'][] = 'bibcite_footnotes/replace_citations';
$form[$triggering_element['#parents'][0]]['widget']['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $bibcite_references;
}
}
}

18
js/plugins/reference_footnotes/dialogs/footnotes.js

@ -14,7 +14,7 @@
{
id: 'reference',
type: 'select',
items: editor.config.referenceFootnotes_list,
items: [[" - None - ", 0]].concat(drupalSettings.bibcite_footnotes.references),
label: Drupal.t('Reference Footnote item:'),
setup: function (element) {
if (isEdit)
@ -65,6 +65,22 @@
this.fakeObj = CKEDITOR.plugins.reference_footnotes.getSelectedFootnote( editor );
this.realObj = editor.restoreRealElement( this.fakeObj );
}
var select = this.parts.contents.$.getElementsByTagName('select');
var selectBox = select.item(0);
// Remove all but the default 'None' item from teh list.
var i;
for (i = selectBox.options.length - 1 ; i >= 1 ; i--) {
selectBox.remove(i)
}
// Re-add buttons from the current state of Settings.
drupalSettings.bibcite_footnotes.references.forEach(function(reference) {
var newReference = document.createElement('option');
newReference.text = reference[0];
newReference.setAttribute("value", reference[1]);
selectBox.add(newReference);
});
this.setupContent( this.realObj );
},
onOk : function() {

28
js/replace_citations.js

@ -0,0 +1,28 @@
/**
* @file
* Provides JavaScript additions to entity embed dialog.
*
* This file provides popup windows for previewing embedded entities from the
* embed dialog.
*/
(function ($, Drupal) {
"use strict";
Drupal.behaviors.bibciteFootnotesReplaceCitations = {
attach: function attach(context, settings) {
/*
if (CKEDITOR.instances) {
for (var instance in CKEDITOR.instances) {
var editor = CKEDITOR.instances[instance];
var config = editor.config;
var name = editor.name;
editor.destroy();
CKEDITOR.replace(name, config);
}
}
*/
}
}
})(jQuery, Drupal);

23
src/Plugin/CKEditorPlugin/ReferenceFootnotes.php

@ -72,27 +72,6 @@ class ReferenceFootnotes extends CKEditorPluginBase {
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
$uid = \Drupal::currentUser()->id();
$query = \Drupal::service('entity.query');
$ref_ids = $query
->get('bibcite_reference')
->condition('uid', $uid)
->execute();
$reference_storage = \Drupal::entityTypeManager()->getStorage('bibcite_reference')->loadMultiple($ref_ids);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('bibcite_reference');
$options = [];
foreach($reference_storage as $ref_id => $ref_item) {
$build = $view_builder->view($ref_item, 'citation');
$render = render($build);
$output = trim(strip_tags($render));
//$options[] = [$output, $ref_id];
$options[] = [$output, $ref_id];
}
return ['referenceFootnotes_list' => $options];
return [];
}
}

10
src/Plugin/Filter/ReferenceFootnotesFilter.php

@ -184,6 +184,8 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
$randstr = $this->randstr();
$value = $this->extractAttribute($matches, 'value');
$page = $this->extractAttribute($matches, 'page');
$reference = $this->extractAttribute($matches, 'reference');
if ($value) {
// A value label was found. If it is numeric, record it in $n so further
@ -195,7 +197,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
$n = $value;
}
}
elseif ($opt_collapse and $value_existing = $this->findFootnote($matches[2], $store_matches)) {
elseif ($opt_collapse and $value_existing = $this->findFootnote($matches[2], $reference, $store_matches)) {
// An identical footnote already exists. Set value to the previously
// existing value.
$value = $value_existing;
@ -211,8 +213,6 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
// attribute.
$value_id = preg_replace('|[^\w\-]|', '', $value);
$page = $this->extractAttribute($matches, 'page');
$reference = $this->extractAttribute($matches, 'reference');
// Create a sanitized version of $text that is suitable for using as HTML
// attribute value. (In particular, as the title attribute to the footnote
@ -279,10 +279,10 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
/**
* @inheritdoc
*/
private function findFootnote($text, &$store_matches) {
private function findFootnote($text, $reference, &$store_matches) {
if (!empty($store_matches)) {
foreach ($store_matches as &$fn) {
if ($fn['text'] == $text) {
if ($fn['text'] == $text && $fn['reference'] == $reference) {
return $fn['value'];
}
}

Loading…
Cancel
Save