Browse Source

WIP Inline Entity Form support.

ief
Alexander O'Neill 6 years ago
parent
commit
475f4acd27
  1. 3
      bibcite_footnotes.libraries.yml
  2. 57
      bibcite_footnotes.module

3
bibcite_footnotes.libraries.yml

@ -3,3 +3,6 @@ reference_footnote:
css:
theme:
css/reference_footnote.css: {}
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;
/**
* 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));
//}
}
}
}

Loading…
Cancel
Save