forked from c31ck/baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.module
executable file
·290 lines (272 loc) · 8.38 KB
/
baseline.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* @file
* Provides a baseline for site building.
*
* @todo remove coupling with i18n.
* @todo provide hook/plugin architecture.
* @todo add alias support.
*/
require_once 'baseline.build.inc';
/**
* Builds the baseline.
*
* This invokes hooks that get all the baseline info and then builds a baseline
* based on that info.
*/
function baseline_build() {
baseline_setup();
baseline_build_aliases(baseline_get_info('aliases'));
baseline_build_blocks(baseline_get_info('blocks'));
baseline_build_date_formats(baseline_get_info('date_formats'));
baseline_build_date_types(baseline_get_info('date_types'));
baseline_build_variables(baseline_get_info('variables'));
baseline_build_nodes(baseline_get_info('nodes'));
baseline_build_menus(baseline_get_info('menus'));
baseline_build_menu_links(baseline_get_info('menu_links'));
baseline_build_user_roles(baseline_get_info('user_roles'));
baseline_build_user_permissions(baseline_get_info('user_permissions'));
baseline_finish();
}
/**
* Creates or updates a path alias.
*
* @param string $source
* Path source.
* @param string $alias
* Path alias.
* @param string $langcode
* Language code.
*/
function baseline_api_create_path_alias($source, $alias, $langcode) {
// Check if an alias is already provided for this source and langcode
// combination.
$existing_alias = path_load(array('source' => $source, 'language' => $langcode));
$alias = array('source' => $source, 'alias' => $alias, 'language' => $langcode);
if ($existing_alias !== FALSE) {
$alias['pid'] = $existing_alias['pid'];
}
path_save($alias);
}
/**
* Creates a block.
*
* @param array $block_info
* Block properties.
*/
function baseline_api_create_block($block_info) {
$fields = array(
'module' => $block_info['module'],
'delta' => $block_info['delta'],
'theme' => $block_info['theme'],
'status' => $block_info['status'],
'weight' => $block_info['weight'],
'region' => $block_info['region'],
'title' => $block_info['title'],
'pages' => $block_info['pages'],
'cache' => $block_info['cache'],
);
if (module_exists('i18n_block') && isset($block_info['i18n_mode'])) {
$fields['i18n_mode'] = $block_info['i18n_mode'];
}
$query = db_merge('block')
->key(array('module' => $block_info['module'], 'delta' => $block_info['delta'], 'theme' => $block_info['theme']))
->fields($fields)
->execute();
}
/**
* Creates a custom block.
*
* @param array $block_info
* Block properties.
*/
function baseline_api_create_custom_block($block_info) {
baseline_api_create_block($block_info);
db_merge('block_custom')
->key(array('bid' => $block_info['delta']))
->fields(array('bid' => $block_info['delta'],
'body' => $block_info['body'],
'info' => $block_info['info'],
'format' => $block_info['format'],
)
)
->execute();
}
/**
* Calls baseline_setup hook implementations.
*/
function baseline_setup() {
$hook = 'baseline_setup';
foreach (module_implements($hook) as $module) {
module_invoke($module, $hook);
}
}
/**
* Calls baseline_finish hook implementations.
*/
function baseline_finish() {
$hook = 'baseline_finish';
foreach (module_implements($hook) as $module) {
module_invoke($module, $hook);
}
}
/**
* Creates a date format.
*/
function baseline_api_create_date_format($format) {
$query = db_merge('date_formats')
->key(array('format' => $format['format']))
->fields(array(
'format' => $format['format'],
'type' => 'custom',
'locked' => 0))
->execute();
}
/**
* Creates a date type.
*/
function baseline_api_create_date_type($type) {
$query = db_merge('date_format_type')
->key(array('type' => $type['type']))
->fields(array(
'type' => $type['type'],
'title' => $type['title'],
'locked' => $type['locked'],
))
->execute();
$key = 'date_format_' . $type['type'];
variable_set($key, $type['format']);
}
/**
* Creates a basic node.
*
* @param array $node
* Array containing node information:
* - nid
* - title
* - type
* - language
*/
function baseline_api_create_node($node) {
$exists = node_load($node['nid']);
$new_node = new stdClass();
if ($exists !== FALSE) {
$new_node->nid = $node['nid'];
}
$new_node->title = $node['title'];
$new_node->type = $node['type'];
$new_node->language = $node['language'];
$new_node->created = time();
$new_node->changed = time();
$new_node->uid = 1;
$new_node->comment = 0;
$new_node->status = 1;
$new_node->sticky = 0;
node_object_prepare($new_node);
node_save($new_node);
}
/**
* Creates a menu link.
*
* A menu link is deemed existing based on its menu_name and its path. Existing
* menu links will be updated, others will be inserted.
*
* @param array $menu_link_info
* Info about the menu link. Contains:
* - menu_name
* - link_title
* - link_name
* - weight
* - new
* - options: (optional) An array of options, see l() for more.
* - router_path: (optional) The path of the relevant router item (new link)
* - parent: (optional) Info about the parent. Contains:
* - menu_name: Name of the parent menu.
* - path: Path of the parent.
*/
function baseline_api_create_menu_link($menu_link_info) {
// If strict checking is enabled, we also use the link_title to determine if
// a menu link exists or not. If strict checking is disabled only the menu and
// link path are used to determine whether a menu link exists or not.
$link_title = (bool) isset($menu_link_info['strict']) ? $menu_link_info['link_title'] : NULL;
$menu_item = (array) baseline_api_menu_link_get($menu_link_info['menu_name'], $menu_link_info['link_path'], $menu_link_info['link_title']);
// Handle parents. A parent is identified by its menu name and path and
// optionally the link title.
if (isset($menu_link_info['parent']['menu_name'], $menu_link_info['parent']['path'])) {
$parent_link_title = (isset($menu_link_info['parent']['link_title'])) ? $menu_link_info['parent']['link_title'] : NULL;
$parent_menu_item = baseline_api_menu_link_get($menu_link_info['parent']['menu_name'], $menu_link_info['parent']['path'], $parent_link_title);
if ($parent_menu_item) {
$menu_link_info['plid'] = $parent_menu_item['mlid'];
}
}
if (isset($menu_item['mlid'])) {
$menu_link_info['mlid'] = $menu_item['mlid'];
}
$mlid = menu_link_save($menu_link_info, $menu_item);
// If we're creating menu links in an installation context, such as during an
// install profile, menu_rebuild() will get called on first page load, which
// in turn calls _menu_navigation_links_rebuild(). This will remove our menu
// items because they are considered stale. We work around this by flagging
// them customized so that _menu_navigation_links_rebuild() will not delete
// them.
// @todo there has to be a better way to fix this. Also: why are these links
// considered stale?
if ($mlid) {
db_update('menu_links')
->fields(array(
'customized' => 1,
))
->condition('mlid', $mlid)
->execute();
}
}
/**
* Loads a menu link.
*
* A menu link is identified based on its menu_name and its path.
*
* @param string $menu_name
* Menu name.
* @param string $path
* Link path.
*
* @return false|array
* Array containing menu info if the menu link exists, FALSE otherwise.
*/
function baseline_api_menu_link_get($menu_name, $path, $link_title = NULL) {
$query = db_select('menu_links', 'ml');
$query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
$query->fields('ml');
$query->addField('ml', 'weight', 'link_weight');
$query->fields('m');
$query->condition('ml.link_path', $path);
$query->condition('ml.menu_name', $menu_name);
if ($link_title) {
$query->condition('ml.link_title', $link_title);
}
if ($item = $query->execute()->fetchAssoc()) {
$item['weight'] = $item['link_weight'];
_menu_link_translate($item);
return $item;
}
return FALSE;
}
/**
* Gets info for a particular type of config.
*
* This will call a hook baseline_info_$type.
*
* @param string $type
* The type of info.
*
* @return array
* An info array. If no implementations were found the array will be empty.
*/
function baseline_get_info($type) {
$hook = "baseline_info_$type";
$baseline_info = array();
foreach (module_implements($hook) as $module) {
$baseline_info += module_invoke($module, $hook);
}
return $baseline_info;
}