Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct EDTF data type in JSON-LD #998 #18

Merged
merged 9 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ greatly appreciated.
## Content Types

Below is a list of the (at least partially) implemented content types with
their fields. *Emphasized fields* were originally planned but proved infeasible
given the structure of Drupal's RDF module and the Islandora JSON-LD module.
their fields. The fields with "EDTF L1" accept and display dates corresponding
to the Library of Congress [2012 Extended Date/Time Format Specification](http://www.loc.gov/standards/datetime/pre-submission.html)
_Level 1_. [EDTF was incorporated in ISO 8601-2019 with some modifications](http://www.loc.gov/standards/datetime/edtf.html)
which will be supported in a future update.

- Corporate Body
- Preferred Name (Title)
- Alternate Name
- Founding Date (EDTF v.1)
- Dissolution Date (EDTF v.1)
- Founding Date (EDTF L1)
- Dissolution Date (EDTF L1)
- Parent Organization
- Authorities
- Description
Expand All @@ -49,14 +51,14 @@ given the structure of Drupal's RDF module and the Islandora JSON-LD module.
- Display Label (Title)
- Authorities
- Relation
- Date Begin (EDTF v.1)
- Date End (EDTF v.1)
- Date Begin (EDTF L1)
- Date End (EDTF L1)
- Person
- Title/Display Name
- Alternate Name
- Preferred Name
- Birth Date (EDTF v.1)
- Death Date (EDTF v.1)
- Birth Date (EDTF L1)
- Death Date (EDTF L1)
- Relation
- Authorities
- Description
Expand Down
118 changes: 109 additions & 9 deletions controlled_access_terms.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use Drupal\jsonld\Normalizer\NormalizerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\controlled_access_terms\EDTFConverter;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

/**
* Implements hook_rdf_namespaces().
Expand All @@ -15,6 +18,7 @@ function controlled_access_terms_rdf_namespaces() {
return [
'wgs84_pos' => 'http://www.w3.org/2003/01/geo/wgs84_pos#',
'org' => 'https://www.w3.org/TR/vocab-org/#org:',
'xs' => 'http://www.w3.org/2001/XMLSchema#',
];
}

Expand All @@ -24,17 +28,113 @@ function controlled_access_terms_rdf_namespaces() {
function controlled_access_terms_jsonld_alter_normalized_array(EntityInterface $entity, array &$normalized, array $context) {
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) {
foreach ($entity->getFieldDefinitions() as $field => $field_definition) {
if (($field_definition->getType() == 'typed_relation') && (!empty($entity->get($field)->getValue()))) {
foreach ($entity->get($field)->getValue() as $value) {
$predicate = NormalizerBase::escapePrefix($value['rel_type'], $context['namespaces']);
$referenced_entity = \Drupal::entityTypeManager()->getStorage($field_definition->getSetting('target_type'))->load($value['target_id']);
// We are assuming the first graph is the one corresponding
// to the node/taxonomy_term we are modifying.
$normalized['@graph'][0][$predicate][] = [
'@id' => $referenced_entity->toUrl('canonical', ['absolute' => TRUE])->setRouteParameter('_format', 'jsonld')->toString(),
];
if (!empty($entity->get($field)->getValue())) {
if ($field_definition->getType() == 'typed_relation') {
foreach ($entity->get($field)->getValue() as $value) {
$predicate = NormalizerBase::escapePrefix($value['rel_type'], $context['namespaces']);
$referenced_entity = \Drupal::entityTypeManager()->getStorage($field_definition->getSetting('target_type'))->load($value['target_id']);
// We are assuming the first graph is the one corresponding
// to the node/taxonomy_term we are modifying.
$normalized['@graph'][0][$predicate][] = [
'@id' => $referenced_entity->toUrl('canonical', ['absolute' => TRUE])->setRouteParameter('_format', 'jsonld')->toString(),
];
}
}
elseif ($field_definition->getType() == 'edtf') {
// Get the predicate to look in.
foreach ($context['current_entity_rdf_mapping']->get('fieldMappings')[$field]['properties'] as $predicate) {
// Find the predicate value that needs updating.
$predicate_normalized = NormalizerBase::escapePrefix($predicate, $context['namespaces']);
foreach ($normalized['@graph'][0][$predicate_normalized] as $index => $value) {
// Clean the date of ranges, uncertainty, and approximations.
// Have to remap the array from '@value' to 'value'.
$normalized_date = EDTFConverter::dateIso8601Value(['value' => $value['@value']]);

// Determine which type to use.
$date_type = NormalizerBase::escapePrefix('xs:date', $context['namespaces']);
switch (count(explode('-', $normalized_date))) {
case 1:
$date_type = NormalizerBase::escapePrefix('xs:gYear', $context['namespaces']);
break;

case 2:
$date_type = NormalizerBase::escapePrefix('xs:gYearMonth', $context['namespaces']);
break;
}
$normalized['@graph'][0][$predicate_normalized][] = [
'@value' => $normalized_date,
'@type' => $date_type,
];
}
}
}
}
}
}
}

/**
* Change fields using the EDTF Widget to the new EDTF Field Type.
*/
function controlled_access_terms_update_8002() {
// Ensure the new EDTF plugins can be found.
\Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();

// Find all the fields using the text_edtf widget via form configs.
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('core.entity_form_display.') as $entity_form_display_config_name) {
$entity_form_display = $config_factory->getEditable($entity_form_display_config_name);
$fields = $entity_form_display->get('content');
foreach ($fields as $field_name => $field_settings) {
if (isset($field_settings['type']) && $field_settings['type'] === 'text_edtf') {

// Update this form setting.
$entity_form_display->set("content.$field_name.type", 'edtf_default');

// Update the field setting.
if (!$fields = \Drupal::entityManager()->getStorage('field_config')->loadByProperties(['field_name' => $field_name])) {
continue;
}
else {
foreach ($fields as $field) {
$new_field = $field->toArray();
$new_field['field_type'] = 'edtf';
$new_field = FieldConfig::create($new_field);
$new_field->original = $new_field;
$new_field->enforceIsNew(FALSE);
$new_field->save();
}
}

// Update the field storage setting.
if (!$field_storage_configs = \Drupal::entityManager()->getStorage('field_storage_config')->loadByProperties(['field_name' => $field_name])) {
continue;
}
else {
foreach ($field_storage_configs as $field_storage) {
$new_field_storage = $field_storage->toArray();
$new_field_storage['type'] = 'edtf';
$new_field_storage = FieldStorageConfig::create($new_field_storage);
$new_field_storage->original = $new_field_storage;
$new_field_storage->enforceIsNew(FALSE);
$new_field_storage->save();
}
}
}
}
$entity_form_display->save(TRUE);
}

// Find display configs.
foreach ($config_factory->listAll('core.entity_view_display.') as $entity_view_display_config_name) {
$entity_view_display = $config_factory->getEditable($entity_view_display_config_name);
$fields = $entity_view_display->get('content');
foreach ($fields as $field_name => $field_settings) {
if (isset($field_settings['type']) && ($field_settings['type'] === 'text_edtf_human' || $field_settings['type'] === 'text_edtf_iso8601')) {
// Update this view setting.
$entity_view_display->set("content.$field_name.type", 'edtf_default');
}
}
$entity_view_display->save(TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ content:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_cat_date_end:
weight: 5
settings:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_corp_alt_name:
weight: 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ content:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_cat_date_end:
weight: 9
settings:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_relationships:
weight: 123
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ content:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_cat_date_end:
weight: 27
settings:
strict_dates: false
intervals: false
third_party_settings: { }
type: text_edtf
type: edtf_default
region: content
field_person_alternate_names:
weight: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_cat_date_end:
weight: 3
Expand All @@ -59,7 +59,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_corp_alt_name:
weight: 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_cat_date_end:
weight: 9
Expand All @@ -56,7 +56,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_relationships:
weight: 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_cat_date_end:
weight: 7
Expand All @@ -59,7 +59,7 @@ content:
day_format: dd
season_hemisphere: north
third_party_settings: { }
type: text_edtf_human
type: edtf_default
region: content
field_person_alternate_names:
weight: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: true
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: true
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string
field_type: edtf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
id: taxonomy_term.field_cat_date_begin
field_name: field_cat_date_begin
entity_type: taxonomy_term
type: string
type: edtf
settings:
max_length: 60
is_ascii: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
id: taxonomy_term.field_cat_date_end
field_name: field_cat_date_end
entity_type: taxonomy_term
type: string
type: edtf
settings:
max_length: 60
is_ascii: false
Expand Down

This file was deleted.

This file was deleted.

Loading