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) {
// Grab header from settings
$header = "<?php\n" . variable_get('module_builder_header', MODULE_BUILDER_HEADER_DEFAULT);
// Grab footer from settings
$footer = variable_get('module_builder_footer', '');
// begin assembling data
// build an array $hook_data of all the stuff we know about hooks
// of the form:
// 'hook_foo' => array( 'declaration' => DATA, 'template' => DATA )
$hook_data = array();
// Check for custom functions file, else use default
$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");
}
// Get array of our hook function body templates from our own / custom template files.
// This is not necessarily all hooks that exist -- not all have template entries.
// This array is in the same order as they occur in the files and already in the format wanted.
$hook_data = module_builder_parse_template($template_file);
// Check for node hooks; these will overwrite core hooks if found
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) {
// add or clobber our existing templates
$hook_data[$hook_name] = $hook_template;
}
}
// Get array of the hook function declarations from the downloaded hook data.
// This is a complete list of all hooks that exist.
// In the form: 'hook_foo' => 'declaration'
// This array is the order they are in the files from d.org: alphabetical apparently.
$hook_function_declarations = module_builder_get_hook_data(MODULE_BUILDER_HOOK_DECLARATIONS);
// iterate over the downloaded declarations,
// merge into our data, adding in those not yet in our data array.
foreach ($hook_function_declarations as $hook_name => $hook_declaration) {
// this adds the declaration to the array items already there
// autovivification takes care of creating new array items if not already there
$hook_data[$hook_name]['declaration'] = $hook_declaration;
}
// $hook_data is now a complete representation of all we know about hooks
// now look at form values. These come in two arrays from the sets of checkboes in the UI.
// merge into one array of hook_name => TRUE/FALSE
$requested_hooks = array();
foreach ($form_values['hooks'] as $hook_group) {
$requested_hooks += $hook_group;
}
// Begin code generation
$code = '';
// iterate over our data array, because it's in a pretty order
foreach ($hook_data as $hook_name => $hook) {
if (!$requested_hooks[$hook_name]) {
// don't want this one. skip it
continue;
}
// Display PHP doc
$code .= "\n/**\n * Implementation of {$hook_name}().\n */\n";
// decode html entities and put in the module name
$code .= htmlspecialchars_decode(str_replace('hook', $form_values['module_root_name'], $hook['declaration']));
// See if function bodies exist; if so, use function bodies from template
if ($hook['template']) {
// Strip out INFO: comments for advanced users
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";
}
// Replace variables
$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);
// Replace full-blown Id tag with just starter
// (excuse the weird concatenation stuff; CVS hijacks it otherwise :))
$code = preg_replace(MODULE_BUILDER_FULL_ID_PATTERN, MODULE_BUILDER_ID_COMMENT, $code);
// Prepare final code
$code = $header . $code . $footer;
return $code;
}