You are here

function module_builder_page_input in Module Builder 7.2

Same name and namespace in other branches
  1. 5 module_builder.module \module_builder_page_input()
  2. 6.2 includes/module_builder.pages.inc \module_builder_page_input()
  3. 7 includes/module_builder.pages.inc \module_builder_page_input()

Form builder for page 1.

1 string reference to 'module_builder_page_input'
module_builder_page in includes/module_builder.pages.inc
Displays module builder interface via a multi-step form. The steps are:

File

includes/module_builder.pages.inc, line 65
Menu callback for main module builder page.

Code

function module_builder_page_input($form, &$form_state) {

  // Get our task handler, which checks hook data is ready, and our generate
  // handler, from which to get the list of properties to collect.
  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;
    }
  }

  // Get the component data info.
  $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);

  // Get the hook names for the presets for our Javascript to work with.
  $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) {

    // Build a reverse array whose keys are hooks and values are arrays
    // of one of more preset names.
    // This allows us further down the line to put classes on each hook
    // checkbox for the presets they belong to: this is what the JS then uses.
    foreach ($hook_preset_data['hooks'] as $hook) {
      $hook_presets_reverse[$hook][] = "preset-{$hook_preset_name}";
    }
  }

  //dsm($hook_presets_reverse);

  // Rework the hooks form element into fieldsets with checkboxes.
  $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,
      //'#theme'  => 'module_builder_hook_list',
      '#tree' => TRUE,
    );
    foreach ($hooks as $hook_name => $hook_info) {

      // Skip callbacks.
      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 this hook belongs to any presets, add the classes to it for the
      // Javascript to find.
      if (isset($hook_presets_reverse[$hook_name])) {
        $form['data']['hooks'][$hook_group][$hook_name]['#attributes'] = array(
          'class' => $hook_presets_reverse[$hook_name],
        );
      }

      // Set some default hooks
      if ($hook_name == 'hook_menu') {
        $form['data']['hooks'][$hook_group][$hook_name]['#default_value'] = 1;
      }
    }

    // Sort list alphabetically
    ksort($form['data']['hooks'][$hook_group]);
  }

  // Add our own (longer) descriptions and defaults.
  // TODO: consider moving some of these to the Module generator.
  $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',
  );

  // Include CSS for formatting
  drupal_add_css(drupal_get_path('module', 'module_builder') . '/theme/module_builder.css');

  // Mark form as fresh to enable JS clearing of fields with sample text.
  // TODO: multiform re-fill may work differently on D7.
  // Failing that, look into ctools?
  $form['#attributes'] = array(
    'class' => 'fresh',
  );
  $form['generate_module'] = array(
    '#type' => 'submit',
    '#name' => 'generate',
    '#value' => t('Generate'),
  );
  $form['#submit'] = array(
    'module_builder_page_input_submit',
  );

  //dsm($form);
  return $form;
}