-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdgi_3d_viewer.module
More file actions
77 lines (67 loc) · 2.37 KB
/
Copy pathdgi_3d_viewer.module
File metadata and controls
77 lines (67 loc) · 2.37 KB
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
<?php
/**
* @file
* Hook implementations.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\media\MediaInterface;
use Drupal\paragraphs\ParagraphInterface;
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function dgi_3d_viewer_media_presave(MediaInterface $media) {
if ($media->bundle() !== '3d_object') {
return;
}
$camera = $media->get('field_camera')->referencedEntities()[0] ?? NULL;
if (!$camera) {
$media->set('field_customcamera', NULL);
return;
}
$camera_settings = dgi_3d_viewer_flatten_camera_values($camera);
$media->set('field_customcamera', serialize($camera_settings));
}
/**
* Flatten camera values.
*/
function dgi_3d_viewer_flatten_camera_values(ParagraphInterface $camera) {
$type = $camera->bundle();
$camera_settings = [
'settings' => [
'near' => $camera->get('field_near')->getValue(),
'far' => $camera->get('field_far')->getValue(),
'position' => [
'x' => $camera->get('field_position_x')->getValue(),
'y' => $camera->get('field_position_y')->getValue(),
'z' => $camera->get('field_position_z')->getValue(),
],
'rotation' => [
'x' => $camera->get('field_rotation_x')->getValue(),
'y' => $camera->get('field_rotation_y')->getValue(),
'z' => $camera->get('field_rotation_z')->getValue(),
],
],
];
switch ($type) {
case 'orthographic_camera_settings':
$camera_settings['type'] = 'OrthographicCamera';
$camera_settings['settings']['top'] = $camera->get('field_top')->getValue();
$camera_settings['settings']['bottom'] = $camera->get('field_bottom')->getValue();
$camera_settings['settings']['left'] = $camera->get('field_left')->getValue();
$camera_settings['settings']['right'] = $camera->get('field_right')->getValue();
break;
case 'perspective_camera_settings':
$camera_settings['type'] = 'PerspectiveCamera';
$camera_settings['settings']['aspect'] = $camera->get('field_aspect')->getValue();
$camera_settings['settings']['fov'] = $camera->get('field_fov')->getValue();
break;
}
return $camera_settings;
}
/**
* Implements hook_js_settings_alter().
*/
function dgi_3d_viewer_js_settings_alter(array &$settings, AttachedAssetsInterface $assets) {
// Check if prod split is enabled.
$settings['isProd'] = \Drupal::config('config_split.config_split.prod')->get('status');
}