Drupal modules for browsing and managing Fedora-based digital repositories.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

115 lines
3.3 KiB

<?php
/**
* @file
* Contains islandora_fits.module.
*/
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\media\MediaInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
/**
* Implements hook_help().
*/
function islandora_fits_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the islandora_fits module.
case 'help.page.islandora_fits':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Enables Technical Metadata derivative generation') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_install().
*/
function islandora_fits_install() {
$term_name = 'FITS File';
$test_terms = taxonomy_term_load_multiple_by_name($term_name);
if (!$test_terms) {
$term = Term::create(array(
'parent' => [],
'name' => $term_name,
'vid' => 'islandora_media_use',
'description' => 'Technical Metadata associated with an original media file',
'field_external_uri' => ['uri' => 'https://projects.iq.harvard.edu/fits'],
))->save();
}
// Add xml extension if it doesn't already exist;
$field = FieldConfig::load("media.file.field_media_file");
$fieldSettings = $field->getSettings();
$extensions = $fieldSettings['file_extensions'];
if (!strpos($extensions, 'xml')) {
$fieldSettings['file_extensions'] .= ' xml';
$field->set('settings', $fieldSettings);
}
}
/**
* Implements hook_theme().
*/
function islandora_fits_theme($existing, $type, $theme, $path) {
return [
'fits' => [
'variables' => [
'title' => 'FITS data',
'link' => NULL,
'output' => [],
],
],
];
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function islandora_fits_media_presave(MediaInterface $media) {
$transformer = \Drupal::getContainer()->get('islandora_fits.transformxml');
if ($media->bundle() != 'fits_technical_metadata') {
return;
}
$media->set('field_complete', TRUE);
$file_id = $media->get('field_media_file')->getValue()[0]['target_id'];
$file = File::load($file_id);
$data = file_get_contents($file->getFileUri());
// Adds any new fields to FITS media type.
$has_new = $transformer->check_new($data);
if ($has_new) {
$media->set('field_complete', FALSE);
} else {
$transformer->populate_media($data, $media);
}
}
/**
* Implements hook_media_update().
*/
function islandora_fits_media_update(MediaInterface $media) {
if ($media->bundle() != 'fits_technical_metadata') {
return;
}
if ($media->get('field_complete')->value == FALSE) {
$transformer = \Drupal::getContainer()->get('islandora_fits.transformxml');
$file_id = $media->get('field_media_file')->getValue()[0]['target_id'];
$file = File::load($file_id);
$data = file_get_contents($file->getFileUri());
$transformer->add_media_fields($data);
$new_media = Media::load($media->id());
$new_media->set('field_complete', TRUE);
$new_media->save();
}
}