|
|
|
@ -5,12 +5,14 @@
|
|
|
|
|
* Contains bibcite_footnotes.module. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
use Drupal\Core\Field\EntityReferenceFieldItemListInterface; |
|
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Implements hook_theme(). |
|
|
|
@ -207,3 +209,107 @@ function bibcite_footnotes_preprocess_bibcite_footnote_link(&$variables) {
|
|
|
|
|
$variables['fn']['fn'] = $link; |
|
|
|
|
// $variables['fn']['fn'][] = $link; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Implementation of hook_inline_entity_form_entity_form_alter(). |
|
|
|
|
* |
|
|
|
|
* Force all entities to be saved once they are created. |
|
|
|
|
* |
|
|
|
|
* @TODO: Add cleanup batch task for orphaned References. |
|
|
|
|
* |
|
|
|
|
* @param $form |
|
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
|
|
|
*/ |
|
|
|
|
function bibcite_footnotes_inline_entity_form_entity_form_alter(&$form, FormStateInterface &$form_state) { |
|
|
|
|
$form['#save_entity'] = TRUE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Implementation of hook_form_node_form_alter(). |
|
|
|
|
* |
|
|
|
|
* This function populates the Reference list drop-down in the CKEditor drop-down. |
|
|
|
|
* It gathers all referenced entities of type bibcite_reference and puts them |
|
|
|
|
* into a JavaScript setting under drupalSettings.bibcite_footnotes.references. |
|
|
|
|
* |
|
|
|
|
* @param $form |
|
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
|
|
|
*/ |
|
|
|
|
function bibcite_footnotes_form_node_form_alter(&$form, FormStateInterface &$form_state) { |
|
|
|
|
$ief = $form_state->get('inline_entity_form'); |
|
|
|
|
if ($ief) { |
|
|
|
|
// 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') { |
|
|
|
|
list($citation_text, $citation_key) = bibcite_footnotes_get_ckeditor_select_item($entity); |
|
|
|
|
$bibcite_references[] = [$citation_text, $citation_key]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$triggering_element = $form_state->getTriggeringElement(); |
|
|
|
|
if (!$triggering_element) { |
|
|
|
|
$form['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $bibcite_references; |
|
|
|
|
} |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
// Not using Inline Entity Form, get the references if they exist |
|
|
|
|
$entity = $form_state->getFormObject()->getEntity(); |
|
|
|
|
$fields = $entity->getFields(); |
|
|
|
|
$reference_field = FALSE; |
|
|
|
|
foreach ($fields as $field) { |
|
|
|
|
$type = $field->getFieldDefinition(); |
|
|
|
|
if ($type->getType() == 'entity_reference') { |
|
|
|
|
if ($type->getSetting('handler') == 'default:bibcite_reference') { |
|
|
|
|
$referenced_entities = $field->referencedEntities(); |
|
|
|
|
$bibcite_references = []; |
|
|
|
|
foreach ($referenced_entities as $referenced_entity) { |
|
|
|
|
list($citation_text, $citation_key) = bibcite_footnotes_get_ckeditor_select_item($referenced_entity); |
|
|
|
|
$bibcite_references[] = [$citation_text, $citation_key]; |
|
|
|
|
} |
|
|
|
|
$form['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $bibcite_references; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Construct an item to go into the CKEditor Reference drop-down list. |
|
|
|
|
* |
|
|
|
|
* @param $entity |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
|
* The form of the array is ["Citation string", [reference entity id]. |
|
|
|
|
*/ |
|
|
|
|
function bibcite_footnotes_get_ckeditor_select_item($entity): array { |
|
|
|
|
$serializer = \Drupal::service('serializer'); |
|
|
|
|
|
|
|
|
|
$data = $serializer->normalize($entity, 'csl'); |
|
|
|
|
$build = ['#theme' => 'bibcite_citation', '#data' => $data]; |
|
|
|
|
$citation_text = trim(strip_tags(render($build))); |
|
|
|
|
// Attempt to match up pre-saved entities with the eventual saved ones. |
|
|
|
|
$citation_key = $entity->id->first()->getValue()['value']; |
|
|
|
|
return [$citation_text, $citation_key]; |
|
|
|
|
} |
|
|
|
|