forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 119
/
MediaLinkHeaderSubscriber.php
91 lines (73 loc) · 2.51 KB
/
MediaLinkHeaderSubscriber.php
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
<?php
namespace Drupal\islandora\EventSubscriber;
use Drupal\Core\Url;
use Drupal\media\MediaInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
/**
* Subscribes to MediaLinkHeader Event.
*
* @package Drupal\islandora\EventSubscriber
*/
class MediaLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public function onResponse(FilterResponseEvent $event) {
$response = $event->getResponse();
$media = $this->getObject($response, 'media');
if ($media === FALSE) {
return;
}
$links = array_merge(
$this->generateEntityReferenceLinks($media),
$this->generateRestLinks($media),
$this->generateMediaLinks($media)
);
// Add the link headers to the response.
if (empty($links)) {
return;
}
$response->headers->set('Link', $links, FALSE);
}
/**
* Generates link headers for the described file and source update routes.
*
* @param \Drupal\media\MediaInterface $media
* Media to generate link headers.
*
* @return string[]
* Array of link headers
*/
protected function generateMediaLinks(MediaInterface $media) {
$media_type = $this->entityTypeManager->getStorage('media_type')->load($media->bundle());
$type_configuration = $media_type->get('source_configuration');
$links = [];
$update_route_name = 'islandora.media_source_update';
$update_route_params = ['media' => $media->id()];
if ($this->accessManager->checkNamedRoute($update_route_name, $update_route_params, $this->account)) {
$edit_media_url = Url::fromRoute($update_route_name, $update_route_params)
->setAbsolute()
->toString();
$links[] = "<$edit_media_url>; rel=\"edit-media\"";
}
if (!isset($type_configuration['source_field'])) {
return $links;
}
$source_field = $type_configuration['source_field'];
if (empty($source_field) ||
!$media->hasField($source_field) ||
!method_exists($media->get($source_field), 'referencedEntities')
) {
return $links;
}
// Collect file links for the media.
foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) {
if ($referencedEntity->access('view')) {
$file_url = $this->utils->getDownloadUrl($referencedEntity);
$links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\"";
}
}
return $links;
}
}