-
Notifications
You must be signed in to change notification settings - Fork 3
/
embridge.module
178 lines (160 loc) · 5.55 KB
/
embridge.module
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* @file
* EMBridge module file.
*/
use \Drupal\Core\Link;
use \Drupal\embridge\Entity\EmbridgeAssetEntity;
use \Drupal\embridge\EmbridgeAssetEntityInterface;
use \Drupal\Core\Template\Attribute;
/**
* Implements hook_theme().
*/
function embridge_theme() {
return array(
'embridge_file_link' => [
'variables' => [
'asset' => NULL,
'description' => '',
'entity' => NULL,
'link_to' => '',
],
],
'embridge_image' => [
'variables' => [
'asset' => NULL,
'conversion' => '',
'application_id' => '',
'description' => '',
'entity' => NULL,
'link_to' => '',
],
],
'embridge_search_results' => [
'variables' => ['results' => []],
],
);
}
/**
* Implements hook_cron().
*/
function embridge_cron() {
/** @var Drupal\embridge\EnterMediaAssetHelper $asset_helper */
$asset_helper = \Drupal::getContainer()->get('embridge.asset_helper');
$asset_helper->deleteTemporaryAssets();
}
/**
* Prepares variables for file link templates.
*
* Default template: file-link.html.twig.
*
* @param array $variables
* An associative array containing:
* - asset: An EMBridge Asset Entity object to which the link will be created.
* - entity: The referring entity.
* - link_to: whether to link to the content, the file, or nothing.
* - description: A description to be displayed instead of the filename.
* - attributes: An associative array of attributes to be placed in the a tag.
*/
function template_preprocess_embridge_file_link(&$variables) {
$asset = $variables['asset'];
$options = array();
$asset_entity = ($asset instanceof EmbridgeAssetEntity) ? $asset : EmbridgeAssetEntity::load($asset->id);
$variables['asset'] = $asset;
$mime_type = $asset->getMimeType();
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options['attributes']['type'] = $mime_type . '; length=' . $asset->getSize();
// Use the description as the link text if available.
if (empty($variables['description'])) {
$link_text = $asset_entity->getFilename();
}
else {
$link_text = $variables['description'];
$options['attributes']['title'] = $asset_entity->getFilename();
}
$url = NULL;
if (!empty($variables['entity']) && $variables['link_to'] == 'content') {
/** @var Drupal\Core\Entity\EntityInterface $entity */
$entity = $variables['entity'];
$url = $entity->urlInfo();
}
// Add to variables for embridge_image.
$variables['url'] = $url;
// Classes to add to the file field for icons.
$classes = array(
'file',
// Add a specific class for each and every mime type.
'file--mime-' . strtr($mime_type, array('/' => '-', '.' => '-')),
// Add a more general class for groups of well known mime types.
'file--' . file_icon_class($mime_type),
);
// Set file classes to the options array.
$variables['attributes'] = new Attribute($variables['attributes']);
$variables['attributes']->addClass($classes);
if ($url) {
$link = Link::fromTextAndUrl($link_text, $url);
}
else {
$link = $link_text;
}
$variables['link'] = $link;
}
/**
* Prepares variables for file link templates.
*
* Default template: file-link.html.twig.
*
* @param array $variables
* An associative array containing:
* - asset: An EMBridge Asset Entity object to which the link will be created.
* - entity: The referring entity.
* - link_to: whether to link to the content, the file, or nothing.
* - description: A description to be displayed instead of the filename.
* - attributes: An associative array of attributes to be placed in the a tag.
*/
function template_preprocess_embridge_image(&$variables) {
template_preprocess_embridge_file_link($variables);
$variables['image'] = array(
'#theme' => 'image',
);
/** @var EmbridgeAssetEntity $asset */
$asset = $variables['asset'];
$variables['image']['#width'] = $asset->getWidth();
$variables['image']['#height'] = $asset->getHeight();
$variables['image']['#alt'] = $variables['description'];
/** @var \Drupal\embridge\EnterMediaAssetHelperInterface $asset_helper */
$asset_helper = Drupal::getContainer()->get('embridge.asset_helper');
$uri = $asset_helper->getAssetConversionUrl($asset, $variables['application_id'], $variables['conversion']);
if ($variables['link_to'] == 'file') {
$variables['url'] = $uri;
}
$variables['image']['#uri'] = $uri;
}
/**
* Checks that a file meets the criteria specified by the validators.
*
* After executing the validator callbacks specified hook_file_validate() will
* also be called to allow other modules to report errors about the file.
*
* @param EmbridgeAssetEntityInterface $asset
* A file entity.
* @param array $validators
* An optional, associative array of callback functions used to validate the
* file. The keys are function names and the values arrays of callback
* parameters which will be passed in after the file entity. The
* functions should return an array of error messages; an empty array
* indicates that the file passed validation. The functions will be called in
* the order specified.
*
* @return array
* An array containing validation error messages.
*
* @deprecated
* Use EmbridgeAssetValidator::validate(),
*/
function embridge_asset_validate(EmbridgeAssetEntityInterface $asset, $validators = array()) {
/** @var \Drupal\embridge\EmbridgeAssetValidator $validator */
$validator = \Drupal::getContainer()->get('embridge.asset_validator');
return $validator->validate($asset, $validators);
}