View source
<?php
use DrupalCodeBuilder\Utility\NestedArray;
function module_builder_page($form, &$form_state) {
module_builder_init_library();
if (empty($form_state['step'])) {
$form_state['step'] = 'input';
$form_state['step_information'] = array(
'input' => 'module_builder_page_input',
'generate' => 'module_builder_page_generate',
);
}
$form_builder_function = $form_state['step_information'][$form_state['step']];
$form = $form_builder_function($form, $form_state);
return $form;
}
function module_builder_page_input($form, &$form_state) {
try {
$mb_task_handler_report = \DrupalCodeBuilder\Factory::getTask('ReportHookData');
$mb_task_handler_generate = \DrupalCodeBuilder\Factory::getTask('Generate', 'module');
} catch (\DrupalCodeBuilder\Exception\SanityException $e) {
if ($e
->getFailedSanityLevel() == 'component_data_processed') {
drupal_set_message(t('No hooks were found. Please check the documentation path specified in the <a href="!settings-url">%administer >> %config >> %develop >> %modulebuilder</a> page.', array(
'!settings-url' => url('admin/config/development/module_builder'),
'%administer' => 'Administer',
'%config' => 'Site configuration',
'%develop' => 'Development',
'%modulebuilder' => "Module builder",
)), 'warning');
return $form;
}
}
$component_data_info = $mb_task_handler_generate
->getRootComponentDataInfo();
$component_data = array();
module_load_include('inc', 'module_builder', 'includes/module_builder.form');
$form_helper = new ModuleBuilderComponentFormBase($mb_task_handler_generate);
$form_state_object = new FormState($form_state);
$form = $form_helper
->componentPropertiesForm($form, $form_state_object);
$mb_task_handler_report_presets = \DrupalCodeBuilder\Factory::getTask('ReportHookPresets');
$hook_presets = $mb_task_handler_report_presets
->getHookPresets();
foreach ($hook_presets as $hook_preset_name => $hook_preset_data) {
foreach ($hook_preset_data['hooks'] as $hook) {
$hook_presets_reverse[$hook][] = "preset-{$hook_preset_name}";
}
}
$hook_groups = $mb_task_handler_report
->listHookOptionsStructured();
$form['data']['hooks'] = array(
'#title' => t('Use the following specific hooks'),
'#tree' => TRUE,
);
foreach ($hook_groups as $hook_group => $hooks) {
$form['data']['hooks'][$hook_group] = array(
'#type' => 'fieldset',
'#title' => $hook_group . ' hooks',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
foreach ($hooks as $hook_name => $hook_info) {
if ($hook_info['type'] == 'callback') {
continue;
}
$form['data']['hooks'][$hook_group][$hook_name] = array(
'#type' => 'checkbox',
'#title' => $hook_name,
'#description' => $hook_info['description'],
'#repopulate' => TRUE,
);
if (isset($hook_presets_reverse[$hook_name])) {
$form['data']['hooks'][$hook_group][$hook_name]['#attributes'] = array(
'class' => $hook_presets_reverse[$hook_name],
);
}
if ($hook_name == 'hook_menu') {
$form['data']['hooks'][$hook_group][$hook_name]['#default_value'] = 1;
}
}
ksort($form['data']['hooks'][$hook_group]);
}
$form['data']['root_name']['#description'] = t('This string is used to name the module files and to prefix all of your functions. This must only contain letters, numbers, and underscores, and may not start with a number.');
$form['data']['readable_name']['#description'] = t('Name of your module as it will appear on the module admin page.');
$form['data']['short_description']['#description'] = t('This text will appear in the module admin list at <a href="!adminmodules">%administer >> %modules</a>.', array(
'!adminmodules' => url('admin/modules'),
'%administer' => 'Administer',
'%modules' => 'Modules',
));
$form['data']['short_description']['#default_value'] = t('Does awesome things. Makes tea. Washes up. Favours of a personal nature.');
$form['data']['module_help_text']['#description'] = t('Help text (HTML) to appear within the system help at <a href="!help">%administer >> %help >> module_name</a>', array(
'!help' => url('admin/help'),
'%administer' => 'Administer',
'%help' => 'Help',
));
$form['data']['module_dependencies']['#description'] = t('Space separated list of other modules that your module requires.');
$form['data']['module_package']['#description'] = t('If your module comes with other modules or is meant to be used exclusively with other modules, enter the package name here. Suggested package names: Audio, Bot, CCK, Chat, E-Commerce, Event, Feed parser, Organic groups, Station, Video, Views and Voting.' . '<br />' . 'For more information on package names, see the <a href="!url-package">documentation on Drupal.org</a>.', array(
'!url-package' => 'http://drupal.org/node/542202#package',
));
$form['#attached']['js'] = array(
drupal_get_path('module', 'module_builder') . '/theme/module_builder.js',
);
drupal_add_css(drupal_get_path('module', 'module_builder') . '/theme/module_builder.css');
$form['#attributes'] = array(
'class' => 'fresh',
);
$form['generate_module'] = array(
'#type' => 'submit',
'#name' => 'generate',
'#value' => t('Generate'),
);
$form['#submit'] = array(
'module_builder_page_input_submit',
);
return $form;
}
function _form_repopulate($form, $form_state) {
foreach (element_children($form) as $key) {
if (isset($form[$key]['#repopulate'])) {
$form[$key]['#default_value'] = $form_state['storage']['input'][$key];
}
$form[$key] = _form_repopulate($form[$key], $form_state);
}
return $form;
}
function module_builder_page_input_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['step'] = 'generate';
$form_state['storage']['input'] = $form_state['values'];
foreach (array(
'generate',
'generate_module',
'form_build_id',
'form_token',
'form_id',
) as $key) {
unset($form_state['storage']['input'][$key]);
}
}
function module_builder_page_generate($form, &$form_state) {
module_load_include('inc', 'module_builder', 'includes/module_builder.form');
$mb_task_handler_generate = \DrupalCodeBuilder\Factory::getTask('Generate', 'module');
$form_helper = new ModuleBuilderComponentFormBase($mb_task_handler_generate);
$form_state_object = new FormState($form_state);
$module_data = $form_helper
->copyFormValuesToEntity($form, $form_state_object);
$hooks = [];
foreach ($form_state['values']['data']['hooks'] as $group_values) {
$group_hooks = array_filter($group_values);
$group_hooks = array_keys($group_hooks);
$hooks = array_merge($group_hooks, $hooks);
}
$module_data['hooks'] = $hooks;
unset($module_data['module_hook_presets']);
$files = $mb_task_handler_generate
->generateComponent($module_data);
foreach ($files as $filename => $code) {
$form['code_instructions_' . $filename] = array(
'#markup' => t('Please copy and paste the following text into a file called !module.', array(
'!module' => $filename,
)),
'#prefix' => '<div class="module-message">',
'#suffix' => '</div>',
);
$form['module_code_' . $filename] = array(
'#type' => 'textarea',
'#title' => t($filename . ' code'),
'#rows' => count(explode("\n", $code)),
'#default_value' => $code,
'#prefix' => '<div class="module-code">',
'#suffix' => '</div>',
);
}
return $form;
}
function module_data_from_form($form, $form_values, $component_data_info) {
$module_data = $form_values;
$module_data['hooks'] = array();
foreach ($form_values['hooks'] as $hook_group) {
$module_data['hooks'] += array_keys(array_filter($hook_group));
}
$module_data['requested_build'] = array(
'code' => TRUE,
'info' => TRUE,
);
foreach ($component_data_info as $property_name => $property_info) {
if ($property_name == 'hooks') {
continue;
}
if ($property_info['format'] == 'array') {
if (isset($property_info['options'])) {
$module_data[$property_name] = array_filter($module_data[$property_name]);
}
else {
$module_data[$property_name] = preg_split("@\\s*\n\\s*@", $module_data[$property_name], -1, PREG_SPLIT_NO_EMPTY);
}
}
}
return $module_data;
}
function mb_addItemSubmit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
module_builder_init_library();
module_load_include('inc', 'module_builder', 'includes/module_builder.form');
$form_state_object = new FormState($form_state);
ModuleBuilderComponentFormBase::addItemSubmit($form, $form_state_object);
}
function mb_removeItemSubmit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
module_builder_init_library();
module_load_include('inc', 'module_builder', 'includes/module_builder.form');
$form_state_object = new FormState($form_state);
ModuleBuilderComponentFormBase::removeItemSubmit($form, $form_state_object);
}
function mb_itemButtonAjax($form, &$form_state) {
return $form['data']['permissions'];
module_builder_init_library();
module_load_include('inc', 'module_builder', 'includes/module_builder.form');
$form_state_object = new FormState($form_state);
return ModuleBuilderComponentFormBase::itemButtonAjax($form, $form_state_object);
}
class FormState implements FormStateInterface {
function __construct(&$form_state_array) {
$this->form_state =& $form_state_array;
if (!isset($this->form_state['storage'])) {
$this->form_state['storage'] = [];
}
}
public function set($address, $value) {
NestedArray::setValue($this->form_state['storage'], $address, $value);
}
public function get($address) {
return NestedArray::getValue($this->form_state['storage'], $address);
}
public function getValues() {
return $this->form_state['values'];
}
public function setRebuild() {
$this->form_state['rebuild'] = TRUE;
}
public function getTriggeringElement() {
return $this->form_state['triggering_element'];
}
}
interface FormStateInterface {
}
class HTML {
public static function getUniqueId($id) {
return drupal_html_id($id);
}
}