-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken_custom.features.inc
108 lines (99 loc) · 2.81 KB
/
token_custom.features.inc
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
<?php
/**
* @file
* Features integration for the Custom tokens module.
*/
/**
* Implements hook_features_export_options().
*/
function token_custom_features_export_options() {
$options = array();
$data = token_custom_features_config_get_tokens();
foreach ($data as $key) {
$options[$key->machine_name] = $key->machine_name;
}
return $options;
}
/**
* Implements hook_features_export().
*/
function token_custom_features_export($data, &$export, $module_name) {
// We have module dependencies in order for this module to function properly
// so we'll add them here.
$export['dependencies']['token_custom'] = 'token_custom';
// The following is the simplest implementation of a straight object export
// with no further export processors called.
foreach ($data as $component) {
$export['features']['token_custom'][$component] = $component;
}
return array();
}
/**
* Implements hook_features_export_render().
*/
function token_custom_features_export_render($module_name, $data, $export = NULL) {
$code = array();
$i = 0;
foreach ($data as $component) {
// Here is just a variable_get, in other cases, it could be a query!
$token_obj = token_custom_features_config_get_machinename($component);
if ($token_obj) {
foreach ($token_obj as $key => $value) {
$code[$i][$key] = $value;
}
}
$i++;
}
$code = " return " . features_var_export($code, ' ') . ";";
return array('token_custom_features_default_settings' => $code);
}
/**
* Implements hook_features_revert().
*/
function token_custom_features_revert($module = NULL) {
token_custom_features_rebuild($module);
}
/**
* Implements hook_features_rebuild().
*/
function token_custom_features_rebuild($module) {
$items = module_invoke($module, 'token_custom_features_default_settings');
foreach ($items as $value) {
try {
$result = token_custom_features_config_get_machinename($value);
$new = $result?FALSE : TRUE;
backdrop_write_record('token_custom', $value, $new?array() : 'machine_name');
}
catch (Exception $e) {
watchdog("Error in updating", $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
}
/**
* Get machine name of all custom tokens.
*
* @return array
* An associative array, or FALSE if there is no next row.
*/
function token_custom_features_config_get_tokens() {
$codes = db_select('token_custom', 'tc')
->fields('tc', array())
->execute();
return $codes->fetchAll();
}
/**
* Get machine name specific data.
*
* @param string $name
* Machine name from token_custom table.
*
* @return array
* An associative array.
*/
function token_custom_features_config_get_machinename($name) {
$codes = db_select('token_custom', 'tc')
->fields('tc', array())
->condition('machine_name', $name)
->execute();
return $codes->fetchAssoc();
}