generated from paul121/farm_contrib_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarm_loocc.module
95 lines (79 loc) · 2.71 KB
/
farm_loocc.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
<?php
/**
* @file
* Primary module hooks for farmOS LOOC-C module.
*/
use Drupal\asset\Entity\AssetInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function farm_loocc_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Module help page.
case 'help.page.farm_loocc':
$output = [
'#markup' => t('Provides a farmOS integration with the <a href=":link">LOOC-C calculator.</a>', [':link' => 'https://looc-c.farm/) calculator.']) . ' ' . t('For more information see <a href=":link">What is LOOC-C</a>', [':link' => 'https://looc-c.farm/about']),
];
return $output;
// Cobenefits page.
case 'farm_loocc.cobenefits':
$output = [
'#markup' => t('Select a method to view associated farm co-benefits and disbenefits.') . ' ' . t('For more information about these methods see the <a href=":link">Emissions Reduction Fund (ERF)</a>.', [':link' => 'http://www.cleanenergyregulator.gov.au/ERF']),
];
return $output;
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function farm_loocc_preprocess_views_view(&$variables) {
// Bail if there is no view.
if (empty($variables['view'])) {
return;
}
// Only modify some views.
$target_view_ids = ['farm_loocc_estimates'];
if (!in_array($variables['view']->id(), $target_view_ids)) {
return;
}
// Only modify if there is an exposed variable.
if (!empty($variables['exposed'])) {
// Save the existing exposed array.
$exposed = $variables['exposed'];
// Build new exposed variable as a details element.
$variables['exposed'] = [
'#type' => 'details',
'#title' => t('Filter'),
'#open' => FALSE,
'#attached' => [
'library' => ['farm_loocc/views_collapsible_filters'],
],
];
// Add back the original exposed array.
$variables['exposed']['form'] = $exposed;
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function farm_loocc_asset_update(AssetInterface $asset) {
// Bail if there is no original asset.
if (empty($asset->original)) {
return;
}
// Delete estimates if the asset is now archived.
if ($asset->get('status')->value == 'archived' && $asset->original->get('status')->value != 'archived') {
/** @var \Drupal\farm_loocc\LooccEstimateInterface $loocc_estimate */
$loocc_estimate = \Drupal::service('farm_loocc.estimate');
$loocc_estimate->deleteEstimate($asset);
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function farm_loocc_asset_delete(AssetInterface $asset) {
/** @var \Drupal\farm_loocc\LooccEstimateInterface $loocc_estimate */
$loocc_estimate = \Drupal::service('farm_loocc.estimate');
$loocc_estimate->deleteEstimate($asset);
}