-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomdb_api.install
105 lines (89 loc) · 3.85 KB
/
omdb_api.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the OMDB API module.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\File\FileSystemInterface;
use Drupal\omdb_api\Entity\Exception\OmdbApiEntityException;
use Drupal\user\Entity\Role;
/**
* Implements hook_install().
*/
function omdb_api_install() {
try {
$default_scheme = \Drupal::config('system.file')->get('default_scheme') ?? \Drupal::service('file_system')->getTempDirectory();
$destination = $default_scheme . "://omdb-api/qrcodes";
// Prepare directory for QR codes storing.
\Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
catch (OmdbApiEntityException $e) {
\Drupal::logger('omdb_api')->warning(sprintf('Unable to create directories, following error logged %s ', $e->getMessage()));
}
}
/**
* Implements hook_requirements().
*/
function omdb_api_requirements($phase) {
$requirements = [];
if (($phase == 'install') || ($phase == 'runtime')) {
$default_scheme = \Drupal::config('system.file')->get('default_scheme')
?? \Drupal::service('file_system')->getTempDirectory();
$destination = $default_scheme . "://omdb-api/qrcodes";
\Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$is_writable = is_writable($destination);
$is_directory = is_dir($destination);
if (!$is_writable || !$is_directory) {
if (!$is_directory) {
$error = new TranslatableMarkup('The directory @directory does not exist.', ['@directory' => $destination]);
}
else {
$error = new TranslatableMarkup('The directory @directory is not writable.', ['@directory' => $destination]);
}
$description = new TranslatableMarkup('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']);
if (!empty($error)) {
$description = $error . ' ' . $description;
$requirements['omdb_api']['description'] = $description;
$requirements['omdb_api']['severity'] = REQUIREMENT_ERROR;
}
}
}
return $requirements;
}
/**
* Implements hook_uninstall().
*/
function omdb_api_uninstall() {
// Remove the qr codes directory and generated images.
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
try {
$file_system
->deleteRecursive(\Drupal::config('system.file')
->get('default_scheme') . '://omdb-api/qrcodes');
}
catch (OmdbApiEntityException $e) {
\Drupal::logger('omdb_api')->warning(sprintf('Unable to delete directories, following error logged %s ', $e->getMessage()));
}
}
/**
* Update hook to add new permissions to Anonymous and Authenticated roles.
*/
function omdb_api_update_9001() {
$anonymous_role = Role::load('anonymous');
if (!empty($anonymous_role) && !$anonymous_role->hasPermission(('View omdb api entities'))) {
$anonymous_role->grantPermission('View omdb api entities');
$anonymous_role->save();
}
$authenticated_role = Role::load('authenticated');
if (!empty($authenticated_role) && !$authenticated_role->hasPermission(('View omdb api entities'))) {
$authenticated_role->grantPermission('View omdb api entities');
$authenticated_role->save();
}
}
/**
* Re-Import newly added config.
*/
function __omdb_api_update_9002(&$sandbox) {
\Drupal::service('config.installer')->installDefaultConfig('module', 'omdb_api');
}