From 475f4acd27263962c9993ca1df2d6eb8db73556d Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 22 Nov 2018 03:23:54 -0400 Subject: [PATCH] WIP Inline Entity Form support. --- bibcite_footnotes.libraries.yml | 3 ++ bibcite_footnotes.module | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/bibcite_footnotes.libraries.yml b/bibcite_footnotes.libraries.yml index 66a8c36..cb3a423 100644 --- a/bibcite_footnotes.libraries.yml +++ b/bibcite_footnotes.libraries.yml @@ -3,3 +3,6 @@ reference_footnote: css: theme: css/reference_footnote.css: {} + dependencies: + - core/jquery + - core/drupalSettings \ No newline at end of file diff --git a/bibcite_footnotes.module b/bibcite_footnotes.module index 180dc63..dd0339b 100644 --- a/bibcite_footnotes.module +++ b/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; + /** * 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_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. + $bibcite_references[$field_name][] = ['hash' => $citation_hash, 'citation' => $citation_text]; + } + } + } + } + } + + $triggering_element = $form_state->getTriggeringElement(); + if (!$triggering_element) { + $form[]['#attached']['library'][] = 'bibcite_footnotes/reference_footnote'; + $form['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $bibcite_references; + } + else { + $data = $bibcite_references; + $form[$triggering_element['#parents'][0]]['widget']['#attached']['drupalSettings']['bibcite_footnotes']['references'] = $data; + //$response = $form_state->getResponse(); + //if (!empty($response)) { + // $response->addCommand(new SettingsCommand ($data, TRUE)); + //} + + } + } +} +