-
Notifications
You must be signed in to change notification settings - Fork 6
/
islandora_citations.module
100 lines (87 loc) · 3.77 KB
/
islandora_citations.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* @file
* General hook implementations.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function islandora_citations_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_islandora_citations_form_field_config_edit_form_alter($form, $form_state, $form_id);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function islandora_citations_form_base_field_override_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_islandora_citations_form_field_config_edit_form_alter($form, $form_state, $form_id);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function islandora_citations_form_base_field_override_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_islandora_citations_form_field_config_edit_form_alter($form, $form_state, $form_id);
}
/**
* Common form alteration callback to add third-party settings.
*/
function _islandora_citations_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$schema = json_decode(file_get_contents(__DIR__ . '/data/csl-data.json'), 1);
if (!$schema) {
return;
}
$cslPropertyArray = array_keys($schema['items']['properties']);
$cslFieldOptions = array_combine($cslPropertyArray, $cslPropertyArray);
/** @var \Drupal\field\FieldConfigInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
$form['third_party_settings']['islandora_citations'] = [
'#title' => 'Citation Settings',
'#type' => 'fieldset',
];
// For typed relation, there is no separate mapping.
// It can just be enabled and the mapping will be based on rel type.
if ($entity->getType() == 'typed_relation') {
$form['third_party_settings']['islandora_citations']['use_entity_checkbox'] = [
'#title' => t('Map CSL from relation type'),
'#description' => t('Enable mapping of this typed relation field from the selected relation type. Rel types, author and creator both get mapped to author.'),
'#type' => 'checkbox',
'#required' => FALSE,
'#default_value' => $entity->getThirdPartySetting('islandora_citations', 'use_entity_checkbox'),
];
}
// For ERR (paragraphs), we only allow mapping from entity.
// Fields mapped inside the paragraph are only considered, else ignored.
// For ER fields, we allow both mapping selected from entity
// or mapping just the title of the ER entity.
if ($entity->getType() == 'entity_reference_revisions' || $entity->getType() == 'entity_reference') {
$form['third_party_settings']['islandora_citations']['use_entity_checkbox'] = [
'#title' => t('Map from referenced entity'),
'#description' => \t('If this field is enabled, the csl mapping will be taken from the referenced entity. Make sure you map fields in the referenced entity before enabling this.'),
'#type' => 'checkbox',
'#required' => FALSE,
'#default_value' => $entity->getThirdPartySetting('islandora_citations', 'use_entity_checkbox'),
];
}
// We do not allow direct mapping for typed relation or ERR.
if (!($entity->getType() === 'entity_reference_revisions' || $entity->getType() === 'typed_relation')) {
$form['third_party_settings']['islandora_citations']['csl_field'] = [
'#type' => 'select',
'#title' => \t('CSL Field'),
'#description' => \t('Select which CSL value this field should be mapped to.'),
'#empty_option' => \t('- Select -'),
'#multiple' => TRUE,
'#options' => $cslFieldOptions,
'#default_value' => $entity->getThirdPartySetting('islandora_citations', 'csl_field'),
];
}
}
/**
* Implements hook_theme().
*/
function islandora_citations_theme($existing, $type, $theme, $path) {
return [
'display_citations' => [
'render element' => 'form',
],
];
}