Repository navigation
Expand file tree
/
Copy pathfarm_fieldkit.install
More file actions
59 lines (50 loc) · 1.5 KB
/
Copy pathfarm_fieldkit.install
File metadata and controls
59 lines (50 loc) · 1.5 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
<?php
/**
* @file
* Install, update and uninstall function for the farm_fieldkit module.
*/
declare(strict_types=1);
use Drupal\consumers\Entity\Consumer;
use Drupal\simple_oauth\Oauth2ScopeInterface;
/**
* Implements hook_install().
*/
function farm_fieldkit_install() {
// Check for default role scopes.
/** @var \Drupal\simple_oauth\Oauth2ScopeProviderInterface $scope_provider */
$scope_provider = \Drupal::service('simple_oauth.oauth2_scope.provider');
$scopes = $scope_provider->loadMultiple(['farm_manager', 'farm_worker']);
$scope_ids = array_map(function (Oauth2ScopeInterface $scope) {
return $scope->id();
}, $scopes);
// Create a consumer for the farmOS Field Kit client.
$fk_consumer = Consumer::create([
'label' => 'Field Kit',
'client_id' => 'fieldkit',
'access_token_expiration' => 3600,
'grant_types' => [
'refresh_token',
'password',
],
'scopes' => array_values($scope_ids),
'redirect' => 'https://farmOS.app',
'allowed_origins' => 'https://farmos.app',
'secret' => NULL,
'confidential' => FALSE,
'third_party' => FALSE,
]);
$fk_consumer->save();
}
/**
* Implements hook_uninstall().
*/
function farm_fieldkit_uninstall() {
// Load the fieldkit consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'fieldkit']);
// If found, delete the consumer.
if (!empty($consumers)) {
$client_consumer = reset($consumers);
$client_consumer->delete();
}
}