function generate_module in Module Builder 5
1 call to generate_module()
- module_builder_page_module in ./module_builder.module
- Module form: 'module' step. Generate the module code.
1 string reference to 'generate_module'
- _module_builder_save_old_form_values in ./module_builder.module
- This still needs some work. Set a bunch of check boxes, forward, back, uncheck
the boxes, forward and back and the boxes get turned back on for some reason.
Otherwise this seems pretty good.
File
- ./module_builder.module, line 794
- Builds scaffolding for custom modules.
Code
function generate_module($form_values) {
$header = "<?php\n" . variable_get('module_builder_header', MODULE_BUILDER_HEADER_DEFAULT);
$footer = variable_get('module_builder_footer', '');
$hook_data = array();
$path = drupal_get_path('module', 'module_builder') . '/templates';
if (file_exists("{$path}/hooks-custom.template")) {
$template_file = file_get_contents("{$path}/hooks-custom.template");
}
else {
$template_file = file_get_contents("{$path}/hooks.template");
}
$hook_data = module_builder_parse_template($template_file);
if ($form_values['hooks']['node']['hook_node_info']) {
if (file_exists("{$path}/node_hooks-custom.template")) {
$template_file = file_get_contents("{$path}/node_hooks-custom.template");
}
else {
$template_file = file_get_contents("{$path}/node_hooks.template");
}
$custom_hooks = module_builder_parse_template($template_file);
foreach ($custom_hooks as $hook_name => $hook_template) {
$hook_data[$hook_name] = $hook_template;
}
}
$hook_function_declarations = module_builder_get_hook_data(MODULE_BUILDER_HOOK_DECLARATIONS);
foreach ($hook_function_declarations as $hook_name => $hook_declaration) {
$hook_data[$hook_name]['declaration'] = $hook_declaration;
}
$requested_hooks = array();
foreach ($form_values['hooks'] as $hook_group) {
$requested_hooks += $hook_group;
}
$code = '';
foreach ($hook_data as $hook_name => $hook) {
if (!$requested_hooks[$hook_name]) {
continue;
}
$code .= "\n/**\n * Implementation of {$hook_name}().\n */\n";
$code .= htmlspecialchars_decode(str_replace('hook', $form_values['module_root_name'], $hook['declaration']));
if ($hook['template']) {
if (!variable_get('module_builder_detail', 0)) {
$hook['template'] = preg_replace(MODULE_BUILDER_INFO_PATTERN, '', $hook['template']);
}
$code .= $hook['template'];
}
else {
$code .= "\n\n";
}
$code .= "}\n\n";
}
$variables = array(
'%module' => $form_values['module_root_name'],
'%description' => str_replace("'", "\\'", $form_values['module_short_description']),
'%name' => !empty($form_values['module_readable_name']) ? str_replace("'", "\\'", $form_values['module_readable_name']) : $form_values['module_root_name'],
'%help' => !empty($form_values['module_help_text']) ? str_replace("'", "\\'", $form_values['module_help_text']) : t('TODO: Create admin help text.'),
'%readable' => str_replace("'", "\\'", $form_values['module_readable_name']),
);
$code = strtr($code, $variables);
$code = preg_replace(MODULE_BUILDER_FULL_ID_PATTERN, MODULE_BUILDER_ID_COMMENT, $code);
$code = $header . $code . $footer;
return $code;
}