From 615b62b9b20ab1e6abf1b053657b17881a9b8c46 Mon Sep 17 00:00:00 2001 From: Natkeeran Date: Fri, 11 Aug 2017 15:09:58 -0400 Subject: [PATCH] Expose parent collection as link header (#66) * Expose parent collection as link header * Update islandora.module * style cleanup --- islandora.module | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/islandora.module b/islandora.module index 8d529c03..e44434e2 100644 --- a/islandora.module +++ b/islandora.module @@ -116,3 +116,47 @@ function islandora_node_delete(EntityInterface $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], + ]; + } +}