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.

163 lines
5.0 KiB

<?php
/**
* @file
* Contains islandora.module.
*
* This file is part of the Islandora Project.
*
* (c) Islandora Foundation
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*/
use Drupal\Core\Database\IntegrityConstraintViolationException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function islandora_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the islandora module.
case 'help.page.islandora':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Islandora integrates Drupal with a Fedora repository.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_modules_installed().
*/
function islandora_modules_installed($modules) {
// Ensure the auth and serialization formats we need are available.
if (in_array('rest', $modules)) {
$rest_resource_config_storage = \Drupal::service('entity_type.manager')->getStorage('rest_resource_config');
$rest_resource_config = $rest_resource_config_storage->load('entity.node');
if ($rest_resource_config) {
$configuration = $rest_resource_config->get('configuration');
if (!in_array('jsonld', $configuration['formats'])) {
$configuration['formats'][] = 'jsonld';
}
if (!in_array('jwt_auth', $configuration['authentication'])) {
$configuration['authentication'][] = 'jwt_auth';
}
$rest_resource_config->set('configuration', $configuration);
$rest_resource_config->save(TRUE);
}
}
}
/**
* Implements hook_rdf_namespaces().
*/
function islandora_rdf_namespaces() {
// Yes, it's amazing, rdf is not registered by default!
return [
'ldp' => 'http://www.w3.org/ns/ldp#',
'dc11' => 'http://purl.org/dc/elements/1.1/',
'nfo' => 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/v1.1/',
'ebucore' => 'http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#',
'fedora' => 'http://fedora.info/definitions/v4/repository#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'ore' => 'http://www.openarchives.org/ore/terms/',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'islandora' => 'http://islandora.ca/CLAW/',
'pcdm' => 'http://pcdm.org/models#',
'use' => 'http://pcdm.org/use#',
'iana' => 'http://www.iana.org/assignments/relation/',
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function islandora_node_insert(EntityInterface $entity) {
// Creates a record in the db to track the number of changes to the entity.
$versionCounter = \Drupal::service('islandora.versioncounter');
try {
$versionCounter->create($entity->uuid());
}
catch (IntegrityConstraintViolationException $e) {
\Drupal::logger('islandora')->error(
'Attempted to create duplicate entry for @uuid in version counter ' .
'table. This should never happen.',
['@uuid' => $entity->uuid()]
);
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function islandora_node_update(EntityInterface $entity) {
// Increments the number of changes to the entity.
$versionCounter = \Drupal::service('islandora.versioncounter');
$versionCounter->increment($entity->uuid());
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function islandora_node_delete(EntityInterface $entity) {
// Deletes the record in the db to track the number of changes to the entity.
$versionCounter = \Drupal::service('islandora.versioncounter');
$versionCounter->delete($entity->uuid());
}
/**
* Implements hook_node_view_alter().
*/
function islandora_node_view_alter(&$build, EntityInterface $entity) {
// Return if memberof field does not exist.
if ($entity->hasField('field_memberof') == FALSE) {
return;
}
// Return if memberof field has no values.
$collection_members = $entity->get('field_memberof')->getValue();
if (count($collection_members) == 0) {
return;
}
// Loop through each member and add to the collection_links.
$collection_links = [];
foreach ($collection_members as $member_info) {
$collection_id = $member_info['target_id'];
$collection_entity = $entity->load($collection_id);
// If collection entity does not exist, skip.
if ($collection_entity == NULL) {
continue;
}
// If entity bundle type is not Collection, skip.
$collection_entity_bundle = $collection_entity->bundle();
if ($collection_entity_bundle != "islandora_collection") {
continue;
}
$collection_entity_url = $collection_entity->url('canonical', ['absolute' => TRUE]);
array_push($collection_links, "<" . $collection_entity_url . ">; rel='collection'");
}
if (count($collection_links) > 0) {
$collection_links_str = implode(", ", $collection_links);
$build['#attached']['http_header'] = [
['Link', $collection_links_str],
];
}
}